简体   繁体   English

SQL Server中一种特殊的交叉表查询

[英]A peculiar crosstab query in SQL Server

I have read a few of these questions and answers when it comes to put a crosstab query into SQL Server. 在将交叉表查询放入SQL Server时,我已经阅读了一些这些问题和答案。 I tried doing this but I got errors. 我试过这样做,但是我遇到了错误。

The simple example is as is I have a barcode on a package of m&m's I want a SQL Server pivot table if you will that will take this information below and turn it into a query that shows the row headers as the barcode, and the column headers as the different colors. 简单的例子是因为我在m&m的包上有一个条形码我想要一个SQL Server数据透视表,如果您将在下面获取此信息并将其转换为显示行标题作为条形码和列标题的查询作为不同的颜色。

I have a qry/table that has 我有一个qry / table

| Package Barcode | Color | SomNmr
| 12345           | BLUE  | 3
| 12345           | RED   | 3
| 12345           | YELL  | 3
| 19999           | BLUE  | 24
| 19999           | BLUE  | 24
| 19999           | PINK  | 24
| 19999           | RED   | 24
| 19999           | RED   | 24

When I run a crosstab query wizard, I run it with the following options: 当我运行交叉表查询向导时,我使用以下选项运行它:

Which Fields values do you want as row headings? 您想要哪些字段值作为行标题?

I select Package Barcode 我选择Package Barcode

Which Fields values do you want as column headings? 您想要哪些字段值作为列标题?

I choose Color 我选择颜色

What number do you want calculated for each column and row intersection? 您希望为每个列和行交叉计算多少个数字?

I choose SomNmr and the Function of COUNT 我选择SomNmrCOUNT的功能

The crosstab pulls exactly what I was looking for: 交叉表完全符合我的要求:

我的交叉表的图片

I looked at the SQL code and paste it into SQL Server Management Studio but I get some errors. 我查看了SQL代码并将其粘贴到SQL Server Management Studio中,但是我遇到了一些错误。

TRANSFORM Count(Table1.[SomNmr]) AS CountOfSomNmr
SELECT Table1.[Package Barcode], Count(Table1.[SomNmr]) AS [Total Of SomNmr]
FROM Table1
GROUP BY Table1.[Package Barcode]
PIVOT Table1.[Color];

How do I go about getting this to work on SQL Server? 我如何让这个在SQL Server上工作? I was looking around and reading about using case when but I can't seem to apply that correctly to make it work. 我正在环顾四周并阅读有关使用案例但我似乎无法正确应用它以使其工作。 Any help appreciated. 任何帮助赞赏。

I made a video in depth explaining what I have if anyone would like to see it I will find out how to post it on here. 我制作了一个视频深度解释我有什么,如果有人想看到它我会找到如何发布在这里。

Assuming you need DYNAMIC 假设你需要DYNAMIC

Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(Color) From YourTable Order by 1 For XML Path('')),1,1,'') 
Select  @SQL = '
 Select [Package Barcode],[Total],' + @SQL + '
  From (
        Select [Package Barcode],B.[Color],B.[Cnt]
         From  YourTable A
         Cross Apply (
                        Select Color=A.Color,Cnt=1
                        Union All
                        Select Color=''Total'',Cnt=1
                     ) B
       ) A
 Pivot (Sum(Cnt) For [Color] in ([Total],' + @SQL + ') ) p'
Exec(@SQL);

Returns 返回

Package Barcode Total   BLUE    PINK    RED YELL
12345           3       1       NULL    1   1
19999           5       2       1       2   NULL

EDIT - If it helps, the SQL Generates looks like this 编辑 - 如果有帮助,SQL Generates看起来像这样

 Select [Package Barcode],[Total],[BLUE],[PINK],[RED],[YELL]
  From (
        Select [Package Barcode],B.[Color],B.[Cnt]
         From  YourTable A
         Cross Apply (
                        Select Color=A.Color,Cnt=1
                        Union All
                        Select Color='Total',Cnt=1
                     ) B
       ) A
 Pivot (Sum(Cnt) For [Color] in ([Total],[BLUE],[PINK],[RED],[YELL]) ) p

EDIT 2 编辑2

Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(Color) From YourTable Order by 1 For XML Path('')),1,1,'') 
Select  @SQL = '
 Select [Package Barcode],[Total],' + @SQL + '
  From (
        Select [Package Barcode],[Color],[Cnt]=case when Sum(Cnt)=0 then ''>>'' else cast(Sum(Cnt) as nvarchar(25)) end
         From  (
                Select [Package Barcode],[Color],[Cnt]=Sum(1) from YourTable Group By [Package Barcode],[Color]
                Union ALL
                Select Distinct [Package Barcode],C.[Color],[Cnt]=0
                 From  YourTable
                 Cross Join (Select Distinct Color From YourTable) C
                Union All
                Select [Package Barcode],[Color]=''Total'',[Cnt]=Sum(1) from YourTable Group By [Package Barcode]
               ) A
        Group By [Package Barcode],[Color]
       ) A
 Pivot (max(Cnt) For [Color] in ([Total],' + @SQL + ') ) p'
Exec(@SQL);

Returns 返回

Package Barcode Total   BLUE    PINK    RED YELL
12345           3       1       >>      1   1
19999           5       2       1       2   >>

Having trouble getting → to display, so I put a >> as a place marker. 无法获得→显示,所以我把>>作为地方标记。

Edit 3 - Conditional Aggregation 编辑3 - 条件聚合

Select [Package Barcode]
      ,[Total] = sum(1)
      ,[BLUE]  = sum(case when color='Blue' then 1 else 0 end)
      ,[Pink]  = sum(case when color='Pink' then 1 else 0 end)
      --.. Add more colors
 From YourTable 
 Group By [Package Barcode]

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

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