简体   繁体   English

SQL查询报告

[英]SQL Query for report

I am working on a aggregate report and I have got so far with the data but I am unsure how to proceed from here. 我正在编写汇总报告,到目前为止我已经掌握了数据,但我不确定如何从这里开始。 I get by with SQL but my skills are limited 我接受SQL但我的技能有限

I have the follow in a temp table 我在临时表中有以下内容

ID   Count  Action
2   23      Installed
2   12      Uninstalled
2   36      Unchanged
3   12      Installed
3   25      Unchanged
4   35      Installed
4   25      Unchanged

I want to convert this into this format 我想将其转换为这种格式

ID  Installed   Uninstalled Unchanged
2   23          12          36
3   12          0           36
4   35          0           25

I have no idea where to go or even how to start to achieve this and cannot find anything to point me in the right direction (Im sure its there somewhere) 我不知道去哪里甚至如何开始实现这一目标,找不到任何指示我正确方向的东西(我确定它在某处)

Any help would be appriciated 任何帮助都会得到满足

It's called a pivot 它被称为枢轴

Here's the documentation about it 这是关于它的文档

The syntax is 语法是

SELECT <non-pivoted column>,
    [first pivoted column] AS <column name>,
    [second pivoted column] AS <column name>,

    ...

    [last pivoted column] AS <column name>
FROM
    (<SELECT query that produces the data>)
    AS <alias for the source query>
PIVOT
( 
    <aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
    IN ( [first pivoted column], [second pivoted column],
    ... [last pivoted column])

) AS <alias for the pivot table>

<optional ORDER BY clause>;

Try this: 尝试这个:

SELECT
  ID, 
  MAX(CASE WHEN "Action" = 'Installed' THEN Count END) AS 'Installed',
  MAX(CASE WHEN "Action" = 'Uninstalled ' THEN Count END) AS 'Uninstalled',
  MAX(CASE WHEN "Action" = 'Unchanged' THEN Count END) AS 'Unchanged'
FROM Table
GROUP BY ID;

Or using the SQL Server PIVOT table operator like so: 或者使用SQL Server PIVOT表运算符,如下所示:

SELECT *
FROM 
(
   SELECT * FROM table
)t
PIVOT
(
   MAX("Count") FOR "Action" IN([Installed], [Uninstalled], [Unchanged])
) p

SQL Fiddle Demo for both 这两个SQL小提琴演示

However, for unknown number of Action s, you will have to select them dynamically like so: 但是,对于未知数量的Action ,您必须动态选择它们,如下所示:

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Action) 
                    from Table1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'');

SET @query = 'SELECT ID,  ' + @cols + ' from 
             (
                SELECT * FROM Table1
             ) x
             PIVOT 
             (
                MAX(count)
                FOR action IN (' + @cols + ')
             ) p ';

EXECUTE(@query);

SQL Fiddle Demo(Dynamic Version) SQL小提琴演示(动态版)

Pivots are good, but very slow at times. 枢轴很好,但有时很慢。 Try it without one. 没有一个尝试。

SELECT ID
    , SUM(CASE WHEN [Action] = 'Installed' THEN [Count] ELSE 0 END) AS Installed
    , SUM(CASE WHEN [Action] = 'Uninstalled' THEN [Count] ELSE 0 END) AS Uninstalled
    , SUM(CASE WHEN [Action] = 'Unchanged' THEN [Count] ELSE 0 END) AS Unchanged
FROM <table>
GROUP BY ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM