根据条件获取最大值

时间:2019-01-09 23:37:47

标签: sql tsql sql-server-2008 max

我正在尝试获取每个项目的最大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        |
|---------------------|------------------|------------------|

1 个答案:

答案 0 :(得分:2)

您可以尝试使用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;