gpt4 book ai didi

sql - SQL Server 2008 中的数据透视表

转载 作者:行者123 更新时间:2023-12-02 04:54:34 26 4
gpt4 key购买 nike

请帮助我解决 SQL Server PIVOT 表中的问题。我得到如下输出。现在我想要在每个日期行下的单独列中分配挂起和编码挂起的总数。

select ScanDate, filestatus, COUNT(filestatus) as filecount from ScanLog 
where FileSource = 'ebridge'
group by filestatus, ScanDate

scandate filestatus filecount
2013-08-01 Allocation Pending 8
2013-08-01 Coding Pending 1
2013-08-02 Allocation Pending 4
2013-08-02 Coding Pending 1
2013-08-03 Allocation Pending 4
2013-08-04 Allocation Pending 18
2013-08-04 Coding Pending 3
2013-08-05 Allocation Pending 6

我使用了以下代码但出现错误,因为“scandate”不是有效字段。请指导我。

select [scandate] from ScanLog 
pivot (count(scandate)
for filestatus in ([allocation pending],[coding pending])) as A
where FileSource = 'ebridge'

最佳答案

试试这个 -

DECLARE @temp TABLE (
ScanDate DATETIME
, FileSource VARCHAR(10)
, FileStatus VARCHAR(30)
, FileCount INT

)

INSERT INTO @temp
VALUES
('2013-08-01', 'ebridge', 'Allocation Pending', 8),
('2013-08-01', 'ebridge', 'Coding Pending', 1),
('2013-08-02', 'ebridge', 'Allocation Pending', 4),
('2013-08-02', 'ebridge', 'Coding Pending', 1),
('2013-08-03', 'ebridge', 'Allocation Pending', 4),
('2013-08-04', 'ebridge', 'Allocation Pending', 18),
('2013-08-04', 'ebridge', 'Coding Pending', 3),
('2013-08-05', 'ebridge', 'Allocation Pending', 6)

SELECT *
FROM (
SELECT scandate, filestatus
FROM @temp
WHERE FileSource = 'ebridge'
) t
PIVOT (
COUNT(scandate)
FOR filestatus IN ([Allocation Pending], [Coding Pending])
) a

关于sql - SQL Server 2008 中的数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18233456/

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