[英]Pivot table in SQL Server 2008
Please help me out in SQL server PIVOT Table. 请帮助我在SQL Server PIVOT表中。 I got the output like below. 我得到如下输出。 Now I want the total count of allocation pending and coding pending in separate columns under each date row. 现在,我希望在每个日期行下的单独列中分配待处理和编码待处理的总数。
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
I used the following code but got error as 'scandate' is not a valid field. 我使用以下代码,但由于“ scandate”不是有效字段而出现错误。 Please guide me. 请指导我。
select [scandate] from ScanLog
pivot (count(scandate)
for filestatus in ([allocation pending],[coding pending])) as A
where FileSource = 'ebridge'
Try this one - 试试这个-
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
try this query, you may use left outer , right outer join or inner join
depends on how the data is in your table 尝试此查询, you may use left outer , right outer join or inner join
取决于表中数据的方式
SELECT frst.scandate
, frst.filestatus
, frst.filecount
, secnd.filestatus
, secnd.filecount1
FROM
(
SELECT scandate
, filestatus
, COUNT(filestatus) AS filecount
FROM ScanLog
WHERE FileSource = 'ebridge'
AND filestatus = 'Allocation Pending'
GROUP BY
filestatus
, scandate
) frst
LEFT OUTER JOIN
(
SELECT scandate
, filestatus
, COUNT(filestatus) AS filecount1
FROM ScanLog
WHERE FileSource = 'ebridge'
AND filestatus = 'Coding Pending'
GROUP BY
filestatus
, scandate
) secnd ON frst.scandate = secnd.scandate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.