Faiz Faiz - 5 months ago 19
SQL Question

Using BETWEEN operator in CASE (THEN) statement

I am trying the following but its throwing an error, can someone tell me what I am doing wrong?

SELECT *
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

Answer

Why don't use 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 ;

EDIT You can't group by CustType and Select *