简体   繁体   English

选择/按多列分组,但仅计算一列的值

[英]Select / group by multiple columns but count just the values of one column

I have 2 tables. 我有2张桌子。

Table #1: orders 表#1:订单

order_id    | crit_1 | crit_2 | crit_3     | other
01      | A00    | GER    | 49er       | x    
02      | A00    | GER    | 49er       | x    
03      | A00    | USA    | 49er       | x    
04      | C80    | DEN    | 66er       | x    
05      | B50    | GER    | 99er       | x    

The table orders has 3 important criteria but doesn't have the criterion_4 . 表格orders具有3个重要条件,但不具有criterion_4 There is another table with the order_positions which contains multiple criterion_4 entries for each order_id . 还有另一个带有order_positions表,其中每个order_id包含多个criterion_4条目。

Table #2: classifications 表#2:分类

crit_1 | crit_2 | crit_3 | crit_4 | class_1 | class_2
A00    | GER    | 49er   | 4711   | A       | 11
A00    | GER    | 49er   | 4712   | A       | 21
A00    | USA    | 49er   | 4711   | D       | 12
A00    | USA    | 49er   | 4712   | D       | 21
B50    | GER    | 99er   | 4801   | B       | 12
B50    | GER    | 99er   | 4802   | B       | 12
B50    | GER    | 99er   | 4803   | B       | 14
C80    | DEN    | 66er   | 4904   | C       | 22
C80    | DEN    | 66er   | 4905   | C       | 21

The table classifications contains classifications for: 表格classifications包含以下分类:

  • orders = class_1 = combination of crit_1, crit_2 & crit_3
  • order_positions = class_2 = combination of crit_1, crit_2, crit_3 & crit_4

I have a query where I join classifications.class_1 on the table orders to create a list of all orders and their respective classification . 我有一个查询,在其中我在表orders上加入classifications.class_1 ,以创建所有orders及其各自classification的列表。

select 
orders.order_id,
orders.crit_1,
orders.crit_2,
orders.crit_3,
classifications.class_1

from 
orders

left join
classifications
on
orders.crit_1=classifications.crit_1 and
orders.crit_2=classifications.crit_2 and
orders.crit_3=classifications.crit_3

where
orders.others = "..."

group by 
orders.order_id,
orders.crit_1,
orders.crit_2,
orders.crit_3,
classifications.class_1

I need a GROUP BY at the end since the table classifications contains multiple entries with the combination of crit_1 , crit_2 and crit_3 . 最后,我需要一个GROUP BY,因为表分类包含多个条目,其中包含crit_1crit_2crit_3的组合。 But this isn't a problem since the needed classification_1 is always the same for each combination of crit_1, crit_2 and crit_3 . 但由于所需的,这是没有问题的classification_1总是对的每个组合相同crit_1, crit_2crit_3

Now I want to create another query where I count just the number of each classification_1 for the orders. 现在,我想创建另一个查询,在该查询中我仅计算订单的每个classification_1 _1的数量。 Something like this: 像这样:

class_1 | number
A       | 12
B       | 5
C       | 18
.       | .

But I don't know how without the whole selection of orders.order_id, orders.crit_1 , orders.crit_2 , orders.crit_3 and classifications.class_1 但是我不知道没有全部选择的orders.order_id, orders.crit_1orders.crit_2orders.crit_3classifications.class_1

I just want to count the class_1 classifications for the query above. 我只想计算上面查询的class_1分类。

Any suggestions? 有什么建议么?


edit 编辑

I tried it like suggested by Kaushik Nayak: 我像考希克·纳亚克(Kaushik Nayak)建议的那样尝试了它:

select 
--orders.order_id,
--orders.crit_1,
--orders.crit_2,
--orders.crit_3,
classifications.class_1,
count(*)

from 
orders

left join
classifications
on
orders.crit_1=classifications.crit_1 and
orders.crit_2=classifications.crit_2 and
orders.crit_3=classifications.crit_3

where
orders.others = "..."

group by 
--orders.order_id,
--orders.crit_1,
--orders.crit_2,
--orders.crit_3,
classifications.class_1

But the results are not correct and I have no idea how to reproduce those numbers. 但是结果不正确,我也不知道如何重现这些数字。

A few examples: 一些例子:

| class_1 | query w/ | query w/o | query    |
|         | group by | group by  | count(*) |
---------------------------------------------
| A       | 654      | 2179      | 1024     |   
| B       | 371      | 1940      |  667     |   
| C       |  94      |  238      |  247     |   

When I use my query with group by then I get 654 entries for class_1 = A. When I make my query without group by then I get 2179 entries for class_1 = A. And when I try the query with Count(*) then I get 1024 entries for class_1 = A. 当我对group by使用查询时,我会收到654个class_1 = A的条目。当我对group by不进行查询时group by我会得到2179 class_1 = A的条目。当我尝试使用Count(*)查询时,我得到了class_1 = A的1024个条目。

The last one is definitely not correct. 最后一个绝对不正确。

Just use GROUP BY class_1 for your classifications table and add an EXISTS condition to check if there is an order. 只需对您的classifications表使用GROUP BY class_1并添加EXISTS条件以检查是否有订单。

SELECT
    c.class_1,
    COUNT(c.class_1) "number"
FROM
    classifications c
WHERE
    EXISTS (
        SELECT
            1
        FROM
            orders o
        WHERE
            o.crit_1 = c.crit_1
            AND   o.crit_2 = c.crit_2
            AND   o.crit_3 = c.crit_3
    )
GROUP BY
    c.class_1
ORDER BY
    1;

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

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