简体   繁体   English

sqlite 上的 SQL Group By 和 COALESCE 问题

[英]Issue with SQL Group By and COALESCE on sqlite

I have a table as below in sqlite database.我在sqlite数据库中有一个如下表。 I want to create a line chart showing usage by product groups.我想创建一个折线图,显示产品组的使用情况。

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

Since I want separate line for each ProductGroup I am using below Query因为我想为每个 ProductGroup 使用单独的行,所以我在 Query 下方使用

SELECT 
      SUM(Qty) as UsedQty, 
      ProductGroup, 
      RecordID 
FROM ProductUsageData 
GROUP BY ProductGroup, RecordID  
ORDER BY RecordID ASC;

While I get three records for A (for each RecordID) I get only 1 record each for B & C as they are not used during each RecordID.虽然我得到了 A 的三个记录(对于每个 RecordID),但我只得到了 B 和 C 的 1 个记录,因为它们在每个 RecordID 中都没有使用。 Problem is when I am putting one line for each ProductGroup in the chart, the points for B & C are shown as per Qty in the first My output is like this问题是当我在图表中为每个 ProductGroup 放置一行时,B & C 的点显示为第一个中的 Qty 我的输出是这样的

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

So the graph looks like this所以图形看起来像这样

在此处输入图片说明

instead of代替

在此处输入图片说明

To fix this I changed the query using COALESCE to get 0 Qty if the ProductGroup is not used during each recording.为了解决这个问题,我使用 COALESCE 更改了查询,以便在每次录制期间未使用 ProductGroup 时获得 0 Qty。

SELECT 
       COALESCE(SUM(Qty), 0) as UsedQty, 
       ProductGroup, 
       RecordID 
FROM ProductUsageData 
GROUP BY ProductGroup, RecordID  
ORDER BY RecordID ASC;

I was expecting output as below我期待输出如下

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

But I am getting same output as first但我得到的输出与第一个相同

Please let me know how can I correct the query to get desired output请让我知道如何更正查询以获得所需的输出

A typical solution is to first cross join two queries that select the distinct product groups and record ids from the table;一个典型的解决方案是首先cross join两个查询,从表中选择不同的产品组和记录 ID; this gives you all possible combinations of productGroup and recordID .这为您提供了productGrouprecordID所有可能组合。

Then, you can bring in the original table with a left join , and aggregate:然后,您可以使用left join引入原始表,并聚合:

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

In the real world, you might have separate referential tables for product groups and records ids, which would make the query simpler and more efficient (since it would avoid the need to select distinct in subqueries).在现实世界中,您可能有单独的产品组和记录 ID 的引用表,这将使查询更简单、更高效(因为它避免了在子查询中select distinct的需要)。

Demo on DB Fiddle : DB Fiddle 上的演示

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

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

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