2013-08-14 3 views
2

Пожалуйста, помогите мне в SQL Server PIVOT Table. Я получил выход, как показано ниже. Теперь я хочу, чтобы общий подсчет ожидающего ожидания и кодирования ожидался в отдельных столбцах под каждой строкой даты.Сводная таблица в SQL Server 2008

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' 

ответ

3

Попробуйте один -

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 
+0

Вы пропускаете 'где FileSource = 'ebridge' ' – Taryn

+1

@bluefeet спасибо. См. Обновленный ответ. – Devart

+0

@Devart Большое спасибо за ваше решение. Его работы прекрасны. Просто я сменил Pivot (граф (Filestatus) вместо графа (скандала). – Aruna

2

попробовать этот запрос, 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 
Смежные вопросы