Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Case Statement in Join

Hi every one i want to use case statement in join using this query and got error

Select CONVERT(VARCHAR(10), SII.SIDATE,103)DATE,SII.SALEID,SII.ItemName,SI.TenancyID

FROM F_SALESINVOICEITEM SII
INNER JOIN F_SALESINVOICE SI ON  SI.SALEID=SII.SALEID 
INNER JOIN #TempTableSearch ts ON CASE
 WHEN ts.ACCOUNTTYPE = '1' THEN ts.ACCOUNTID=SI.TENANCYID
  WHEN ts.ACCOUNTTYPE='2' THEN ts.ACCOUNTID=SI.EMPLOYEEID
   WHEN ts.ACCOUNTTYPE='3' THEN ts.ACCOUNTID=SI.SUPPLIERID
    WHEN ts.ACCOUNTTYPE='4' THEN ts.ACCOUNTID=SI.SALESCUSTOMERID

Error

Incorrect syntax near '='.

Please help me to solve this error.

like image 686
MindFresher Avatar asked Sep 24 '13 10:09

MindFresher


People also ask

Can you use or statements in joins?

If you have an OR condition in the JOIN - and there is no possibility that the values in the OR statement overlap...then you can convert it to a UNION ALL. If the values overlap it would require a UNION which may not improve performance over the JOIN.

Can we use WHERE clause after join?

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.

Can we use case in join condition in Oracle?

Using static SQL, you can join all the tables, and use CASE expressions to get the exact rows (if any) that you want from those tables.

Can a case statement be used in a join?

There are plenty of ways to resolve for this: a subquery with a CASE statement in the join statement for the table you are joining in, a CASE statement in a temp table where all values are changed to match, or this handy little trick of using a CASE statement in the JOIN's ON clause.


3 Answers

IT should be,

ON 
ts.ACCOUNTID =  CASE
                    WHEN ts.ACCOUNTTYPE = '1' THEN SI.TENANCYID
                    WHEN ts.ACCOUNTTYPE = '2' THEN SI.EMPLOYEEID
                    WHEN ts.ACCOUNTTYPE = '3' THEN SI.SUPPLIERID
                    WHEN ts.ACCOUNTTYPE = '4' THEN SI.SALESCUSTOMERID
                END
like image 130
John Woo Avatar answered Sep 23 '22 14:09

John Woo


Instead of using CASE, I'd much rather do this:

Select CONVERT(VARCHAR(10), SII.SIDATE,103)DATE,SII.SALEID,SII.ItemName,SI.TenancyID
FROM F_SALESINVOICEITEM SII
INNER JOIN F_SALESINVOICE SI ON  SI.SALEID=SII.SALEID 
INNER JOIN #TempTableSearch ts ON
       (ts.ACCOUNTTYPE='1' AND ts.ACCOUNTID=SI.TENANCYID)
    OR (ts.ACCOUNTTYPE='2' AND ts.ACCOUNTID=SI.EMPLOYEEID)
    OR (ts.ACCOUNTTYPE='3' AND ts.ACCOUNTID=SI.SUPPLIERID)
    OR (ts.ACCOUNTTYPE='4' AND ts.ACCOUNTID=SI.SALESCUSTOMERID)

To explain why the query didn't work for you: the syntax of the CASE requires an END at the end of the clause. It would work, as the other solutions proposed suggest, but I find this version to be more convenient to understand - although this part is highly subjective.

like image 25
ppeterka Avatar answered Sep 26 '22 14:09

ppeterka


you can do this, so you have no chance to misspell something (note that ACCOUNTTYPE and ACCOUNTID used only when needed, you don't have to copy-paste it)

select
    convert(varchar(10), SII.SIDATE,103) as DATE,
    SII.SALEID, SII.ItemName, SI.TenancyID
from F_SALESINVOICEITEM as SII
    inner join F_SALESINVOICE as SI on SI.SALEID = SII.SALEID 
    outer apply (
        '1', SI.TENANCYID
        '2', SI.EMPLOYEEID
        '3', SI.SUPPLIERID
        '4', SI.SALESCUSTOMERID
    ) as C(ACCOUNTTYPE, ACCOUNTID)
    inner join #TempTableSearch as ts on
        ts.ACCOUNTTYPE = C.ACCOUNTTYPE and ts.ACCOUNTID = C.ACCOUNTID
like image 21
Roman Pekar Avatar answered Sep 24 '22 14:09

Roman Pekar