gpt4 book ai didi

sql - 旋转和汇总数据

转载 作者:行者123 更新时间:2023-12-04 15:59:04 25 4
gpt4 key购买 nike

我有以下内容:

declare @PrintJob TABLE (
PageNumber Int,
Copies Int
)

INSERT INTO @PrintJob(PageNumber,Copies) VALUES(1,100)
INSERT INTO @PrintJob(PageNumber,Copies) VALUES(2,100)
INSERT INTO @PrintJob(PageNumber,Copies) VALUES(3,100)
INSERT INTO @PrintJob(PageNumber,Copies) VALUES(4,100)
INSERT INTO @PrintJob(PageNumber,Copies) VALUES(5,50)
INSERT INTO @PrintJob(PageNumber,Copies) VALUES(6,25)

SELECT * FROM @PrintJob

问:有没有办法在 Microsoft SQL Server 2005 中生成以下输出?

Pages 1-4 = 100 Copies, 5-5 = 50 Copies, 6-6 = 25 Copies

最佳答案

假设不会出现间隙,使用:

SELECT CAST(MIN(pj.pagenumber) AS VARCHAR(max)) +'-'+ CAST(MAX(pj.pagenumber) AS VARCHAR(max)) +' = '+ CAST(pj.copies AS VARCHAR(max)) +' Copies' AS pages
FROM PRINTJOB pj
GROUP BY pj.copies
ORDER BY pj.copies DESC

...会给你:

pages
-------
1-4 = 100 Copies
5-5 = 50 Copies
6-6 = 25 Copies

关于sql - 旋转和汇总数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com