按组的最小值排序

时间:2018-10-23 18:55:30

标签: sql-server tsql sql-order-by

我试图按PODID然后按CYID进行订购,但有时t.ID将具有多个PODID和/或CYID。所有具有相同t.ID的行都应该在一起。

SELECT DISTINCT
    t.ID AS TariffID,
    t.TradeID,
    pod.PODID,
    cy.CYID
FROM 
    TB_Import_Tariff_Tier t (NOLOCK)
INNER JOIN 
    TB_Import_Tariff_Tier_POD_Group_History pod (NOLOCK) ON t.ID = pod.ID 
INNER JOIN 
    TB_Import_Tariff_Tier_CY_Group_History cy (NOLOCK) ON t.ID = cy.TariffID
ORDER BY 
    pod.PODID, cy.CYID

预期结果:

TariffID TradeID PODID  CYID
------------------------------
185       18       1    17939
185       18       1    17941
246       18       1    17939
246       18       1    17941
266       18       1    17939
266       18       1    17941
280       18       1    17939
280       18       1    17941
285       18       1    17939
252       18       1    17945

编辑:样本太小,这是使用t.ID,pod.PODID,cy.CYID排序的完整结果

+----------+---------+-------+-------+
| TariffID | TradeID | PODID | CYID  |
+----------+---------+-------+-------+
|      251 |      18 |     4 | 18091 |
|      252 |      18 |     1 | 17945 |
|      254 |      18 |     4 | 17939 |
|      254 |      18 |     4 | 17941 |
|      255 |      18 |     4 | 17936 |
|      256 |      18 |     8 | 18091 |
|      257 |      18 |     4 | 18093 |
|      261 |      18 |     4 | 17939 |
|      261 |      18 |     4 | 17941 |
|      262 |      18 |     8 | 17936 |
|      263 |      18 |     4 | 18093 |
|      266 |      18 |     1 | 17939 |
|      266 |      18 |     1 | 17941 |
|      267 |      18 |     4 | 17936 |
|      268 |      18 |     4 | 18093 |
|      271 |      18 |     8 | 17939 |
|      271 |      18 |     8 | 17941 |
|      272 |      18 |     1 | 17936 |
|      273 |      18 |     4 | 18093 |
|      274 |      18 |     4 | 18091 |
|      274 |      18 |     4 | 18093 |
|      275 |      18 |     4 | 17939 |
|      275 |      18 |     4 | 17941 |
|      276 |      18 |     8 | 17936 |
|      277 |      18 |     4 | 18091 |
|      279 |      18 |     1 | 18091 |
|      279 |      18 |     1 | 18093 |
|      280 |      18 |     1 | 17939 |
|      280 |      18 |     1 | 17941 |
|      281 |      18 |     4 | 18093 |
|      284 |      18 |     8 | 18093 |
|      285 |      18 |     1 | 17939 |
|      286 |      18 |     1 | 18091 |
|      287 |      18 |     4 | 18093 |
|      290 |      18 |     4 | 18091 |
|      290 |      18 |     4 | 18093 |
|      291 |      18 |     4 | 18091 |
|      292 |      18 |     4 | 18093 |
|      293 |      18 |     4 | 17947 |
|      294 |      18 |     4 | 17945 |
|      295 |      18 |     8 | 17939 |
|      295 |      18 |     8 | 17941 |
|      296 |      18 |     4 | 18091 |
|      296 |      18 |     4 | 18093 |
|      297 |      18 |     1 | 17936 |
|      300 |      18 |     4 | 18093 |
|      301 |      18 |     4 | 17936 |
|      302 |      18 |     8 | 18091 |
|      303 |      18 |     4 | 18093 |
|      306 |      18 |     4 | 18091 |
|      306 |      18 |     4 | 18093 |
|      307 |      18 |     4 | 17936 |
|      308 |      18 |     8 | 18093 |
|      310 |      18 |     8 | 18093 |
|      311 |      18 |     4 | 18093 |
|      312 |      18 |     4 | 17939 |
|      312 |      18 |     4 | 17941 |
|      313 |      18 |     4 | 17936 |
|      316 |      18 |     4 | 18091 |
|      316 |      18 |     4 | 18093 |
|      317 |      18 |     1 | 17936 |
|      318 |      18 |     1 | 18091 |
+----------+---------+-------+-------+

所有PODID = 1应该首先在一起,但是如果同一t.ID有另一个PODID,则它们应该在一起。

如果t.ID在订单末尾仍然是错误的,因为这些ID不在一起

+----------+---------+-------+-------+
| TariffID | TradeID | PODID | CYID  |
+----------+---------+-------+-------+
|      272 |      18 |     1 | 17936 |
|      297 |      18 |     1 | 17936 |
|      317 |      18 |     1 | 17936 |
|      266 |      18 |     1 | 17939 |
|      280 |      18 |     1 | 17939 |
|      285 |      18 |     1 | 17939 |
|      266 |      18 |     1 | 17941 |
|      280 |      18 |     1 | 17941 |
|      252 |      18 |     1 | 17945 |
|      279 |      18 |     1 | 18091 |
|      286 |      18 |     1 | 18091 |
|      318 |      18 |     1 | 18091 |
|      279 |      18 |     1 | 18093 |
|      255 |      18 |     4 | 17936 |
|      267 |      18 |     4 | 17936 |
|      301 |      18 |     4 | 17936 |
|      307 |      18 |     4 | 17936 |
|      313 |      18 |     4 | 17936 |
|      254 |      18 |     4 | 17939 |
|      261 |      18 |     4 | 17939 |
|      275 |      18 |     4 | 17939 |
|      312 |      18 |     4 | 17939 |
|      254 |      18 |     4 | 17941 |
|      261 |      18 |     4 | 17941 |
|      275 |      18 |     4 | 17941 |
|      312 |      18 |     4 | 17941 |
|      294 |      18 |     4 | 17945 |
|      293 |      18 |     4 | 17947 |
|      251 |      18 |     4 | 18091 |
|      274 |      18 |     4 | 18091 |
|      277 |      18 |     4 | 18091 |
|      290 |      18 |     4 | 18091 |
|      291 |      18 |     4 | 18091 |
|      296 |      18 |     4 | 18091 |
|      306 |      18 |     4 | 18091 |
|      316 |      18 |     4 | 18091 |
|      257 |      18 |     4 | 18093 |
|      263 |      18 |     4 | 18093 |
|      268 |      18 |     4 | 18093 |
|      273 |      18 |     4 | 18093 |
|      274 |      18 |     4 | 18093 |
|      281 |      18 |     4 | 18093 |
|      287 |      18 |     4 | 18093 |
|      290 |      18 |     4 | 18093 |
|      292 |      18 |     4 | 18093 |
|      296 |      18 |     4 | 18093 |
|      300 |      18 |     4 | 18093 |
|      303 |      18 |     4 | 18093 |
|      306 |      18 |     4 | 18093 |
|      311 |      18 |     4 | 18093 |
|      316 |      18 |     4 | 18093 |
|      262 |      18 |     8 | 17936 |
|      276 |      18 |     8 | 17936 |
|      271 |      18 |     8 | 17939 |
|      295 |      18 |     8 | 17939 |
|      271 |      18 |     8 | 17941 |
|      295 |      18 |     8 | 17941 |
|      256 |      18 |     8 | 18091 |
|      302 |      18 |     8 | 18091 |
|      284 |      18 |     8 | 18093 |
|      308 |      18 |     8 | 18093 |
|      310 |      18 |     8 | 18093 |
+----------+---------+-------+-------+

2 个答案:

答案 0 :(得分:1)

t.ID子句中添加ORDER BY。你有尝试过吗?还是我有不同的看法?

SELECT DISTINCT
    t.ID AS TariffID,
    t.TradeID,
    pod.PODID,
    cy.CYID
FROM 
    TB_Import_Tariff_Tier t (NOLOCK)
INNER JOIN 
    TB_Import_Tariff_Tier_POD_Group_History pod (NOLOCK) ON t.ID = pod.ID 
INNER JOIN 
    TB_Import_Tariff_Tier_CY_Group_History cy (NOLOCK) ON t.ID = cy.TariffID
ORDER BY 
    t.ID,pod.PODID, cy.CYID

答案 1 :(得分:1)

如果我理解正确,则需要按以下顺序订购数据:

ORDER BY
    MIN(PODID) OVER (PARTITION BY TariffID),
    MIN(CYID) OVER (PARTITION BY TariffID),
    TariffID,
    PODID,
    CYID

这将把具有最小PODID和CYID的TariffID组放在第一位,然后按照各个PODID,CYID在每个TariffID中排序:

| TariffID | TradeID | PODID | CYID  |
|----------|---------|-------|-------|
| 272      | 18      | 1     | 17936 |
| 266      | 18      | 1     | 17939 |
| 266      | 18      | 1     | 17941 |
| 252      | 18      | 1     | 17945 |
| 255      | 18      | 4     | 17936 |
| 267      | 18      | 4     | 17936 |
| 254      | 18      | 4     | 17939 |
| 254      | 18      | 4     | 17941 |
| 261      | 18      | 4     | 17939 |
| 261      | 18      | 4     | 17941 |
| 275      | 18      | 4     | 17939 |
| 275      | 18      | 4     | 17941 |
| 251      | 18      | 4     | 18091 |
| 274      | 18      | 4     | 18091 |
| 274      | 18      | 4     | 18093 |
| 257      | 18      | 4     | 18093 |
| 263      | 18      | 4     | 18093 |
| 268      | 18      | 4     | 18093 |
| 273      | 18      | 4     | 18093 |
| 262      | 18      | 8     | 17936 |
| 271      | 18      | 8     | 17939 |
| 271      | 18      | 8     | 17941 |
| 256      | 18      | 8     | 18091 |

DB Fiddle for ShortLong Data