[英]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. vagt_type
的“ timeloen”值可能是该用户的10倍,其中usr = 1
,而“ prov”的值是该用户的2倍。 I need the counts in separate columns and I'm using DISTINCT
to only get each usr once. 我需要在单独的列中计数,而我使用
DISTINCT
只能使每个usr一次。
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: 下面是我到目前为止已经制定了,但是,计数的所有实例
vagt_type
,由于某种原因没有受到cast
从日期到日期:
$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. 其他表存在的仅仅是
join
到用户表及其在这种情况下并不重要,我只用它来CONCAT
用户的全名。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.