简体   繁体   中英

MySQL: Distinct with count, sum and group by

I have a MySQL Statement which prints the Sales of WeddingDresses from September to September:

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

When a BridalGown (which is Brautkleid in the Screenshot) will be sold, a new entry will put into the database with shop assistant name, item and kind (Brautkleid == BridalGown). If 2 people are involved with the selling of the bridal gown, there are 2 entries (see attached image)

2个售货员参与销售时的2个条目

If only 1 Shop Assistant is involved in a selling, there is only 1 Entry. (see attached image)

如果只涉及一名店员,则只能进入1次

What can I do, to count only the first row? Because right now, if there are 2 people involved, there will also 2 rows counted instead of only 1 dress which is already sold...

Thank you for your feedback!

Kind Regards,

Stefan

You could reduce the rows using a subquery for 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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