【问题标题】:Pivot table in SQL Server 2008SQL Server 2008 中的数据透视表
【发布时间】:2013-08-16 12:10:33
【问题描述】:

请在 SQL Server PIVOT Table 中帮助我。我得到了如下所示的输出。现在我想要在每个日期行下的单独列中分配待处理和编码待处理的总数。

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'

【问题讨论】:

    标签: sql sql-server tsql pivot


    【解决方案1】:

    试试这个 -

    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
    

    【讨论】:

    • 你错过了where FileSource = 'ebridge'
    • @Devart 非常感谢您的解决方案。它工作正常。只是我改变了 Pivot(Count(Filestatus) 而不是 count(scandate)。
    • 如果此解决方案完全适合您,请批准。
    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多