如何从行SQL中删除0和/或Null

时间:2014-03-18 08:23:23

标签: sql-server-2008 tsql

任务目标:显示客户的ID和姓氏,考试费用总计至少$ 300.00且不超过$ 900.00。

我尝试使用HAVING过滤掉它,但这不起作用。有什么建议吗?

SELECT
    ISNULL(CASE WHEN SUM(ED.ex_fee) >=300 and SUM(ED.ex_fee) < 900 THEN CL.cl_id END,'') AS cl_id,
    ISNULL(CASE WHEN SUM(ED.ex_fee) >=300 and SUM(ED.ex_fee) < 900 THEN CL.cl_name_last END,'') AS cl_name_last
FROM vt_clients as CL
INNER JOIN vt_animals AS AN ON AN.cl_id = CL.cl_id
INNER JOIN vt_exam_headers AS EH ON EH.an_id = AN.an_id
INNER JOIN vt_exam_details AS ED ON ED.ex_id = EH.ex_id
GROUP BY CL.cl_id, cl_name_last
HAVING CL.cl_id <> 0 and CL.cl_name_last <> ''

enter image description here

1 个答案:

答案 0 :(得分:3)

试试这个

SELECT CL.cl_id, cl_name_last
FROM vt_clients as CL
INNER JOIN vt_animals AS AN ON AN.cl_id = CL.cl_id
INNER JOIN vt_exam_headers AS EH ON EH.an_id = AN.an_id
INNER JOIN vt_exam_details AS ED ON ED.ex_id = EH.ex_id
GROUP BY CL.cl_id, cl_name_last
HAVING SUM(ED.ex_fee) >=300 and SUM(ED.ex_fee) < 900