简体   繁体   English

在SQL Server中使用查询的表中的复选框类似功能

[英]Check box like functionality in table using query in SQL Server

I have a primary table with multiple records for the name but different quantity and colors. 我有一个主表,其中有多个记录,但名称和数量不同。

TblPrimary : current table TblPrimary :当前表

id | name    | color | Quan  |
===+=========+=======+=======+
1  | Apple   | Red   | 10    |
2  | Banana  | Yellow| 5     |
3  | Mango   | Yellow| 8     |
4  | Apple   | Green | 20    |
5  | Banana  | Brown | 15    |
6  | Mango   | Orange| 12    |
7  | Mango   | Green |  5    |

This is my main table and I basically want data from the primary table like this. 这是我的主表,基本上我想要这样的主表中的数据。 So basically the Quan in main table is sum of all individual Quan from the primary table. 因此,基本上,主表中的Quan是主表中所有单独Quan总和。 The colors (Red,Yellow,Brown) in the main table; 主表中的颜色(红色,黄色,棕色); are bits which indicate whether that color is present for the fruit or not in the primary table. 是指示主表中水果是否存在该颜色的位。

TblMain : new expected table TblMain :新的预期表

id | Name    | Quan  | Red | Yellow | Brown | Green | Orange |
===+=========+=======+=====+========+=======+=======+========+
1  | Apple   |  30   |  1  |   0    |  0    |   1   |  0     |
2  | Banana  |  20   |  0  |   1    |  1    |   0   |  0     |
3  | Mango   |  25   |  0  |   1    |  0    |   1   |  1     |

I have got the below query and I have group by and the sum. 我有以下查询,并且有分组依据和总和。 I am not able to get the colors portion of the main table populated. 我无法填充主表的颜色部分。

INSERT INTO TblMain(Name, Quan)
    (SELECT Name, SUM(Quan)
     FROM TblPrimary
     GROUP BY Name)

You can try to use sum windows function in subquery then use condition aggregate function make your expect result. 您可以尝试在子查询中使用sum Windows函数,然后使用条件聚合函数使您获得预期的结果。

select  ROW_NUMBER() OVER(ORDER BY name) id,
        name,
    Quan, 
    SUM(CASE WHEN color = 'Red' THEN 1 ELSE 0 END) 'Red',
    SUM(CASE WHEN color = 'Yellow' THEN 1 ELSE 0 END) 'Yellow',
    SUM(CASE WHEN color = 'Brown' THEN 1 ELSE 0 END) 'Brown',
    SUM(CASE WHEN color = 'Green' THEN 1 ELSE 0 END) 'Green',
    SUM(CASE WHEN color = 'Orange' THEN 1 ELSE 0 END) 'Orange'
from (
    SELECT name,
    color,
    SUM(Quan) OVER(PARTITION BY name ORDER BY name) Quan  
    FROM TblPrimary
) t1
group by name,Quan  

sqlfiddle sqlfiddle

Result 结果

id  name    Quan    Red Yellow  Brown   Green   Orange
1   Apple   30      1   0       0       1       0
2   Banana  20      0   1       1       0       0
3   Mango   25      0   1       0       1       1

You can use PIVOT to convert values to columns. 您可以使用PIVOT将值转换为列。 In your case this becomes a bit cumbersome, because you want the row sum as well as well as the 0/1 flags 在您的情况下,这变得有些麻烦,因为您需要行总和以及0/1标志

SELECT
    name,
    ISNULL(Red,0) + ISNULL(Yellow,0) + ISNULL(Brown,0) + ISNULL(Green,0) + ISNULL(Orange,0) As Quan,
    CASE WHEN Red IS NULL THEN 0 ELSE 1 END AS Red,
    CASE WHEN Yellow IS NULL THEN 0 ELSE 1 END AS Yellow,
    CASE WHEN Brown IS NULL THEN 0 ELSE 1 END AS Brown,
    CASE WHEN Green IS NULL THEN 0 ELSE 1 END AS Green,
    CASE WHEN Orange IS NULL THEN 0 ELSE 1 END AS Orange
FROM  
  (SELECT name, color, Quan  
   FROM dbo.TblPrimary) AS SourceTable  
PIVOT  
(  
   SUM(Quan)
FOR color IN (Red, Yellow, Brown, Green, Orange)  
) AS PivotTable;  

If you display the sums directly for the colors instead of the flags, the query simplifies to: 如果直接显示颜色的总和而不是标志,查询将简化为:

SELECT
    name, Red, Yellow, Brown, Green, Orange
FROM  
  (SELECT name, color, Quan  
   FROM dbo.TblPrimary) AS SourceTable  
PIVOT  
(  
   SUM(Quan)
FOR color IN (Red, Yellow, Brown, Green, Orange)  
) AS PivotTable;

And the result will be: 结果将是:

name     Red  Yellow  Brown  Green  Orange
==========================================
Apple     10   null    null   20     null
Banana   null   5       15   null    null
Mango    null   8      null    5      12

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

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