简体   繁体   中英

sql distinct + count

What I want to do is count specific instances of the same column for each user.

example:
vagt_type might have the value of "timeloen" 10 times where usr = 1 and the value of "prov" 2 times for that user. I need the counts in separate columns and I'm using DISTINCT to only get each usr once.

Below is what I've worked out so far but, that counts all the instances of vagt_type and for some reason is not affected by the cast from date to date:

$test = $wpdb->get_results("SELECT distinct m.usr, CONCAT(n1.meta_value, ' ', n2.meta_value) AS fl_name, count(m1.vagt_type) as timeloen
FROM $main_table as m
LEFT JOIN Lausten_usermeta n1 ON m.usr=n1.user_id and n1.meta_key = 'first_name'
LEFT JOIN Lausten_usermeta n2 ON m.usr=n2.user_id and n2.meta_key = 'last_name'
LEFT JOIN $main_table m1 ON m.usr=m1.usr and m1.vagt_type = 'timeloen'
WHERE m.vagtDato BETWEEN CAST('$start' AS DATE) AND CAST('$end' AS DATE)
", ARRAY_A);

Edit: example of my table:

 id | usr | vagtDato   |  vagt_type
 13 |   1 | 2015-09-05 |    kursus
 16 |   1 | 2015-09-01 |    kursus
 11 |   1 | 2015-09-03 |    trappetur
 10 |   1 | 2015-09-02 |    provision
 9  |   1 | 2015-09-01 |    timeloen    
 15 |   1 | 2015-09-04 |    sygedag
 17 |   1 | 2015-09-02 |    timeloen    
 18 |   29| 2015-09-18 |    timeloen    
 19 |   1 | 2015-10-01 |    timeloen    

the other table there is just a join to the user table and its not important in this case as I only use it to CONCAT the fullname of the user.

Expected results:

usr | timeloen | provision | sygedag
1   | 3        | 1         | 1
29  | 1        | 0         | 0

EDIT: -- hope this helps someone else :) What ended up being my total solution:

$test = $wpdb->get_results("SELECT a.usr, a.vagtDato, b.timeloen, c.provision, d.kursus, e.trappetur, f.sygedag
FROM $main_table a
LEFT JOIN (SELECT usr, count(vagt_type) as timeloen
FROM $main_table  WHERE vagt_type = 'timeloen'
AND vagtDato between DATE('$start') AND DATE('$end')
GROUP BY usr ) b on b.usr=a.usr

LEFT JOIN (SELECT usr, count(vagt_type) as provision
FROM $main_table  WHERE vagt_type = 'provision'
AND vagtDato between DATE('$start') AND DATE('$end')
GROUP BY usr ) c on c.usr=a.usr

LEFT JOIN (SELECT usr, count(vagt_type) as kursus
FROM $main_table  WHERE vagt_type = 'kursus'
AND vagtDato between DATE('$start') AND DATE('$end')
GROUP BY usr ) d on d.usr=a.usr

LEFT JOIN (SELECT usr, count(vagt_type) as trappetur
FROM $main_table  WHERE vagt_type = 'trappetur'
AND vagtDato between DATE('$start') AND DATE('$end')
GROUP BY usr ) e on e.usr=a.usr

LEFT JOIN (SELECT usr, count(vagt_type) as sygedag
FROM $main_table  WHERE vagt_type = 'sygedag'
AND vagtDato between DATE('$start') AND DATE('$end')
GROUP BY usr ) f on f.usr=a.usr

WHERE a.vagtDato between DATE('$start') AND DATE('$end')
GROUP BY a.usr
", ARRAY_A);

I would believe you have to put something like:

SELECT
    usr
    , ( SELECT( count( * ) FROM $TABLE where vagt_type = 'timeloen' ) AS timeloen
    , ( SELECT( count( * ) FROM $TABLE where vagt_type = 'provision ' ) AS provision 
    , ( SELECT( count( * ) FROM $TABLE where vagt_type = 'sygedag' ) AS sygedag
FROM ( $main_table )
    ( $LEFTJOINS )
GROUP BY usr --to get the distincts users

or

SELECT
    usr
    , count( * )
FROM ( $main_table )
    ( $LEFTJOINS )
GROUP BY 1, 2 --to get the distincts users

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