Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Group By出现问题,并在sqlite上合并_Sql_Sqlite_Group By - Fatal编程技术网

SQL Group By出现问题,并在sqlite上合并

SQL Group By出现问题,并在sqlite上合并,sql,sqlite,group-by,Sql,Sqlite,Group By,我在sqlite数据库中有一个如下表。我想创建一个折线图,按产品组显示使用情况 Table: ProductUsageData UserID ProductName ProductGroup Qty RecordID 1 A1 A 12 1 2 A1 A 12 1 1 A2 A 15 1 3

我在sqlite数据库中有一个如下表。我想创建一个折线图,按产品组显示使用情况

Table: ProductUsageData

UserID  ProductName ProductGroup    Qty RecordID
1       A1          A               12  1   
2       A1          A               12  1   
1       A2          A               15  1   
3       A1          A               12  2   
2       B1          B               12  2   
5       B2          B               5   2
1       A1          A               12  3   
1       A2          A               15  3   
4       A1          A               12  3   
3       C1          C               12  3   
2       C2          C               15  3
因为我想为我在下面查询中使用的每个ProductGroup使用单独的行

SELECT 
      SUM(Qty) as UsedQty, 
      ProductGroup, 
      RecordID 
FROM ProductUsageData 
GROUP BY ProductGroup, RecordID  
ORDER BY RecordID ASC;
虽然A(每个RecordID)有三条记录,但B&C只有一条记录,因为它们在每个RecordID期间都没有使用。 问题是,当我在图表中为每个产品组放一行时,B&C的分数在第一行中按数量显示 我的输出是这样的

A   39  1
A   12  2
B   17  2
A   39  3
C   27  3   
所以这个图是这样的

A   39  1
A   12  2
B   17  2
A   39  3
C   27  3   

而不是

为了解决这个问题,我使用COALESCE更改了查询,如果每次录制期间未使用ProductGroup,则得到0 Qty

SELECT 
       COALESCE(SUM(Qty), 0) as UsedQty, 
       ProductGroup, 
       RecordID 
FROM ProductUsageData 
GROUP BY ProductGroup, RecordID  
ORDER BY RecordID ASC;
我期望输出如下

A   39  1
B   0   1
C   0   1
A   12  2
B   17  2
C   0   2
A   39  3
B   0   3
C   27  3
但我得到的输出和第一个一样


请告诉我如何更正查询以获得所需的输出

典型的解决方案是首先
交叉连接
两个查询,从表中选择不同的产品组和记录ID;这提供了
productGroup
recordID
的所有可能组合

然后,您可以引入带有
左联接的原始表
,并聚合:

select
    g.productGroup,
    coalesce(sum(p.qty), 0) qty,
    r.recordID
from (select distinct productGroup from productUsageData) g
cross join (select distinct recordID from productUsageData) r
left join productUsageData p
    on  p.productGroup = g.productGroup
    and p.recordID = r.recordID
group by r.recordID, g.productGroup
order by r.recordID, g.productGroup
在现实世界中,产品组和记录ID可能有单独的引用表,这将使查询更简单、更高效(因为这样可以避免在子查询中选择distinct)

productGroup | qty | recordID :----------- | :-- | :------- A | 39 | 1 B | 0 | 1 C | 0 | 1 A | 12 | 2 B | 17 | 2 C | 0 | 2 A | 39 | 3 B | 0 | 3 C | 27 | 3 产品组|数量|记录ID :----------- | :-- | :------- A | 39 | 1 B | 0 | 1 C | 0 | 1 A | 12 | 2 B | 17 | 2 C | 0 | 2 A | 39 | 3 B | 0 | 3 C | 27 | 3