我嘗試在我的子查詢表中計算總員工數。假設計數結果將返回0,但它一直返回1.COUNT(DISTINCT())返回假值
如果我嘗試只返回employee_id和month在一起,我沒有得到may的任何返回值,這是正確的,但每次我嘗試計數(不同的),我會得到1作爲我的返回值。這是我的SQL
SELECT
count (distinct(CASE WHEN (x.month =5 and x.employee_id <> 0) THEN
x.employee_id
ELSE 0 END)) as test_may
FROM(
(
SELECT
h.month,
h.employee_id,
eb.employee_no,
ee.company_code,
h.amount,
h.year,
h.trx_type,
h.trx_code,
v.trx_desc,
h.frequency,
h.run_sequence
FROM
v_employee h,
v_trans v,
employee_emp ee,
employee eb
WHERE
(h.year = 2014) AND
(h.employee_id = ee.employee_id) AND
(ee.employee_id = eb.employee_id) AND
(h.employee_no = eb.employee_no) AND
(h.trx_code = v.trx_code) AND
(h.trx_type = v.trx_type) AND
(v.company_code = ee.company_code OR v.company_code is NULL) AND
(h.trx_type IN ('S','B','N','O','A','D','L'))
)
)x,
employee_emp ee,
employee eb
WHERE
(x.employee_id = ee.employee_id) AND
(ee.employee_id = eb.employee_id) AND
(x.employee_no = eb.employee_no) AND
(x.year = 2014)
Mysql不是SQL-Server,所以我刪除了標記 – Jens
COUNT DISTINCT沒有被破壞,SQL也沒有被破壞。你的數據是什麼,你期望什麼,你得到了什麼?如果返回1行,結果將是1 –
結果僅取決於您的第二個查詢。要更好地檢查它自己。 –