我正在尝试以下但是它抛出一个错误,有人能告诉我我做错了吗?
SELECT custType, count(*)
FROM tbl1 a
LEFT JOIN tbl2 b ON a.tbID = b.tbID
AND ((CustType IN ('Apple','IPAD') and date BETWEEN @StartDate AND @EndDate)
or (CustType IN ('Samsung','LCD') and date BETWEEN @StartDateLL AND @EndDateLL))
GROUP BY CustType
答案 0 :(得分:3)
为什么不使用AND/OR
SELECT CustType , count(*)
FROM tbl1 a
LEFT JOIN tbl2 b ON a.tbID = b.tbID
AND ((CustType IN ('Apple','IPAD') and date BETWEEN @StartDate AND @EndDate)
or (CustType IN ('Samsung','LCD') and date BETWEEN @StartDateLL AND @EndDateLL))
group by CustType ;
编辑您无法按CustType和Select *
进行分组答案 1 :(得分:0)
您不能将BETWEEN运算符用于case语句或IIF的THEN部分。 BETWEEN
是转换为真和假的条件。所以它是VALUE BETWEEN A AND B
,并且是案例陈述的WHEN部分的一部分。
case语句也返回一个值,而不是要进一步执行的公式。
我同意@Vercelli的解决方案,但为了向您展示案例陈述如何在您尝试的方法中起作用,您可以执行以下操作:
SELECT *
FROM tbl1 a
LEFT JOIN tbl2 b ON a.tbID = b.tbID
AND (CASE
WHEN CustType IN ('Apple','IPAD') AND [date] ETWEEN @StartDate AND @EndDate THEN 1
WHEN CustType IN ('Samsung','LCD') AND [date] BETWEEN @StartDateLL AND @EndDateLL THEN 1
ELSE 0 END) = 1
再次使用@ vercelli的答案,因为它是编写SQL查询的更合适的方法。