MySQL:与count,sum和group by不同

时间:2018-03-27 18:33:39

标签: mysql count sum distinct

我有一个MySQL声明,可以打印9月到9月的WeddingDresses销售情况:

SELECT YEAR( datum + interval 3 month) AS Year,
   SUM( preis ) AS Price, count(*) as Number
   FROM wccrm_prov
   WHERE typ = 'BridalGown'
   GROUP BY Year
   ORDER BY Year ASC

当BridalGown(屏幕截图中为Brautkleid)将被出售时,新的条目将放入数据库中,店员名称,项目和种类(Brautkleid == BridalGown)。如果2人参与婚纱礼服的销售,则有2个参赛作品(见附图)

2 Entries when 2 shop assistants are involved in a selling

如果只有1名店员参与销售,则只有1个条目。 (见附图)

Only 1 Entry if only 1 Shop Assistant is involved

我该怎么办,只计算第一行?因为现在,如果涉及2个人,也会计算2行,而不是只有1件衣服已经售出......

感谢您的反馈!

亲切的问候,

的Stefan

1 个答案:

答案 0 :(得分:0)

您可以使用min shopAssistant的子查询减少行数

SELECT 
    YEAR(datum + interval 3 month) AS Year,
    SUM(preis) AS Price,
    COUNT(*) AS Number
FROM 
    (SELECT 
         datum, preis, typ, MIN(ShopAssistant)
     FROM
         wccrm_prov
     GROUP BY
         datum, preis, typ) T 
WHERE 
    typ = 'BridalGown'
GROUP BY 
    Year
ORDER BY 
    Year ASC