Order By Clause 在 SQL 服务器中无法用于排序日期值
Order By Clause not working in SQL Server for Sorting date value
我有两个表,一个叫做 Shelf Table,它有 Shelf Number
SEQ
SHELF_NUMBER
QUANTITY
1
ID001
100
2
ID002
20
3
ID003
55
4
ID004
200
5
ID005
38
我的另一个 Table 是 Shelf_Data
SEQ
SHELF_NUMBER_FK
FIELD
VALUE
1
ID001
BATCH
100006
2
ID001
SHELF_DATE
08/20/2023
3
ID002
BATCH
110009
4
ID003
BATCH
565644
5
ID003
SHELF_DATE
02/10/2024
6
ID004
BATCH
222389
7
ID004
SHELF_DATE
10/25/2023
8
ID005
BATCH
181865
我已经编写了一个基于 SHELF_DATE 的排序查询,如下所示
SELECT s.SHELF_NUMBER, SUM(s.QUANTITY), sd.VALUE AS BATCH
FROM SHELF s
LEFT JOIN SHELF_DATA sd
ON s.SHELF_NUMBER = sd.SHELF_NUMBER_FK
AND sd.FIELD IN ('BATCH')
WHERE s.QUANTITY > 0
GROUP BY s.SHELF_NUMBER, sd.VALUE
ORDER BY (CASE
WHEN sd.FIELD = 'SHELF_DATE' THEN
sd.VALUE
END) ASC
所以,我想从上面的查询中完成的是获取有效数量的货架批号,这些批号按货架日期升序排列。
但是 Order By Clause 没有使用 Shelf_Date 值对数据进行排序。
期望输出::
SHELF_NUMBER
SUM_QUANTITY
BATCH
ID001
100
100006
ID004
200
222389
ID003
55
565644
ID002
20
110009
ID005
38
181865
真的卡在这里..提前致谢
您可以将其编写为 2 个单独的查询。
;with cte as
(SELECT s.SHELF_NUMBER, SUM(s.QUANTITY) qty, sd.VALUE AS BATCH
FROM SHELF s
LEFT JOIN SHELF_DATA sd
ON s.SHELF_NUMBER = sd.SHELF_NUMBER_FK
AND sd.FIELD IN ('BATCH')
WHERE s.QUANTITY > 0
GROUP BY s.SHELF_NUMBER, sd.VALUE
)
select c.* from cte c
inner join SHELF_DATA sd
ON s.SHELF_NUMBER = sd.SHELF_NUMBER_FK
where sd.FIELD = 'shelf_date'
order by sd.value
提供(SHELF_NUMBER_FK,FIELD)是SHELF_DATA的唯一键table你可以加入它两次。分组时不确定你想要哪个日期,假设最大。
SELECT s.SHELF_NUMBER, SUM(s.QUANTITY), sd1.VALUE AS BATCH, max(Convert(DATE, sd2.VALUE, 101) s_date
FROM SHELF s
LEFT JOIN SHELF_DATA sd1
ON s.SHELF_NUMBER = sd1.SHELF_NUMBER_FK
AND sd1.FIELD ='BATCH'
LEFT JOIN SHELF_DATA sd2
ON s.SHELF_NUMBER = sd2.SHELF_NUMBER_FK
AND sd2.FIELD IN ='SHELF_DATE'
WHERE s.QUANTITY > 0
GROUP BY s.SHELF_NUMBER, sd1.VALUE
ORDER BY max(Convert(DATE, sd2.VALUE, 101)) ASC
我有两个表,一个叫做 Shelf Table,它有 Shelf Number
SEQ | SHELF_NUMBER | QUANTITY |
---|---|---|
1 | ID001 | 100 |
2 | ID002 | 20 |
3 | ID003 | 55 |
4 | ID004 | 200 |
5 | ID005 | 38 |
我的另一个 Table 是 Shelf_Data
SEQ | SHELF_NUMBER_FK | FIELD | VALUE |
---|---|---|---|
1 | ID001 | BATCH | 100006 |
2 | ID001 | SHELF_DATE | 08/20/2023 |
3 | ID002 | BATCH | 110009 |
4 | ID003 | BATCH | 565644 |
5 | ID003 | SHELF_DATE | 02/10/2024 |
6 | ID004 | BATCH | 222389 |
7 | ID004 | SHELF_DATE | 10/25/2023 |
8 | ID005 | BATCH | 181865 |
我已经编写了一个基于 SHELF_DATE 的排序查询,如下所示
SELECT s.SHELF_NUMBER, SUM(s.QUANTITY), sd.VALUE AS BATCH
FROM SHELF s
LEFT JOIN SHELF_DATA sd
ON s.SHELF_NUMBER = sd.SHELF_NUMBER_FK
AND sd.FIELD IN ('BATCH')
WHERE s.QUANTITY > 0
GROUP BY s.SHELF_NUMBER, sd.VALUE
ORDER BY (CASE
WHEN sd.FIELD = 'SHELF_DATE' THEN
sd.VALUE
END) ASC
所以,我想从上面的查询中完成的是获取有效数量的货架批号,这些批号按货架日期升序排列。 但是 Order By Clause 没有使用 Shelf_Date 值对数据进行排序。
期望输出::
SHELF_NUMBER | SUM_QUANTITY | BATCH |
---|---|---|
ID001 | 100 | 100006 |
ID004 | 200 | 222389 |
ID003 | 55 | 565644 |
ID002 | 20 | 110009 |
ID005 | 38 | 181865 |
真的卡在这里..提前致谢
您可以将其编写为 2 个单独的查询。
;with cte as
(SELECT s.SHELF_NUMBER, SUM(s.QUANTITY) qty, sd.VALUE AS BATCH
FROM SHELF s
LEFT JOIN SHELF_DATA sd
ON s.SHELF_NUMBER = sd.SHELF_NUMBER_FK
AND sd.FIELD IN ('BATCH')
WHERE s.QUANTITY > 0
GROUP BY s.SHELF_NUMBER, sd.VALUE
)
select c.* from cte c
inner join SHELF_DATA sd
ON s.SHELF_NUMBER = sd.SHELF_NUMBER_FK
where sd.FIELD = 'shelf_date'
order by sd.value
提供(SHELF_NUMBER_FK,FIELD)是SHELF_DATA的唯一键table你可以加入它两次。分组时不确定你想要哪个日期,假设最大。
SELECT s.SHELF_NUMBER, SUM(s.QUANTITY), sd1.VALUE AS BATCH, max(Convert(DATE, sd2.VALUE, 101) s_date
FROM SHELF s
LEFT JOIN SHELF_DATA sd1
ON s.SHELF_NUMBER = sd1.SHELF_NUMBER_FK
AND sd1.FIELD ='BATCH'
LEFT JOIN SHELF_DATA sd2
ON s.SHELF_NUMBER = sd2.SHELF_NUMBER_FK
AND sd2.FIELD IN ='SHELF_DATE'
WHERE s.QUANTITY > 0
GROUP BY s.SHELF_NUMBER, sd1.VALUE
ORDER BY max(Convert(DATE, sd2.VALUE, 101)) ASC