【问题标题】:Get max value based on condition根据条件获取最大值
【发布时间】:2019-01-09 23:37:46
【问题描述】:

我正在尝试获取每个项目的最大 noteId,但返回已首先固定的笔记,而不是返回未固定在同一项目中的笔记。

select refId as projectId, max(noteId) as noteId
    from notes 
    where noteType='project' 
    and ((noteCat='critical project' and pinned=1) or noteCat='critical 
project') 
    group by refId, pinned
    order by pinned desc

样本数据:

|---------|-----------|--------------|------------------|----------|
| noteId  | projectId |   noteType   |      noteCat     |  pinned  |
|---------|-----------|--------------|------------------|----------|
| 306586  |   34256   |   project    | critical project |     1    |
|---------|-----------|--------------|------------------|----------|
| 298183  |    972    |   project    | critical project |     0    |
|---------|-----------|--------------|------------------|----------|
| 296114  |   32549   |   project    | critical project |     0    |
|---------|-----------|--------------|------------------|----------|
| 295668  |    972    |   project    | critical project |     0    |
|---------|-----------|--------------|------------------|----------|
| 295463  |    972    |   project    | critical project |     1    |
|---------|-----------|--------------|------------------|----------|

预期结果应该是:

|---------------------|------------------|------------------|
|      Project Id     |      Note Id     |       Pinned     |
|---------------------|------------------|------------------|
|          972        |       295463     |         1        |
|---------------------|------------------|------------------|
|         34256       |       306586     |         0        |
|---------------------|------------------|------------------|
|         32549       |       296114     |         1        |
|---------------------|------------------|------------------|

实际结果是:

|---------------------|------------------|------------------|
|      Project Id     |      Note Id     |       Pinned     |
|---------------------|------------------|------------------|
|          972        |       295463     |         1        | -- Older noteId but pinned.
|---------------------|------------------|------------------|
|         34256       |       306586     |         0        |
|---------------------|------------------|------------------|
|          972        |       298183     |         0        | --This should not be returned.
|---------------------|------------------|------------------|
|         32549       |       296114     |         1        |
|---------------------|------------------|------------------|

【问题讨论】:

  • *仅显示示例的固定列。
  • 您忘记发布输入样本数据。
  • @stickybit 我现在已经添加了示例数据。

标签: sql tsql sql-server-2008 max


【解决方案1】:

您可以尝试使用row_number() 窗口函数为每个项目生成一个行号,将固定在未固定之前的注释将较高的 ID 放在较低的 ID 之前。

SELECT x.projectid,
       x.noteid,
       x.pinned
       FROM (SELECT n.projectid,
                    n.noteid,
                    n.pinned,
                    row_number() OVER (PARTITION BY n.projectid
                                       ORDER BY n.pinned DESC,
                                                n.noteid DESC) rn
                    FROM notes n) x
       WHERE x.rn = 1;

【讨论】:

  • 感谢@stickybit,但是当我对我的数据运行查询时,没有返回未固定的项目。
猜你喜欢
  • 2022-06-28
  • 2021-09-18
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
相关资源
最近更新 更多