2016-01-18 172 views
0

我有以下查詢:甲骨文複雜的SQL SELECT查詢

SELECT SUM (UNCOLLECTED) 
      FROM LIQUIDATIONSDETAILS LD 
      WHERE  LD.COMPANYID = L.COMPANYID 
       AND LD.GROUPID = L.GROUPID 
       AND LD.PERIODID = L.PERIODID 
       AND LD.FORMTYPE IN (1, 2, 3) 

我需要的時候L.PERIODID > '2013-2014'那麼LD.FORMTYPE IN (1,2,3,4)其他LD.FORMTYPE IN (1,2,3)

什麼想法?

+1

基於時間段怎麼能比「2013-2014」更大,這將等於「-1」,它必須大於2013年或2014年? –

+0

Mathieu的PERIODID是一個Varchar列,值的例子​​是:'2013-2014' '2014-2015'等 – Giorgos

+0

我不擅長與甲骨文,但會這樣嗎? (LD)是LD.COMPANYID = L.COMPANYID和LD.GROUPID = L.GROUPID AND((LD.PERIODID = L.PERIODID AND LD.FORMTYPE IN(1,2,3,4)AND LD) .PERIODID>'2013-2014')或(LD.PERIODID = L.PERIODID AND LD.FORMTYPE IN(1,2,3)AND LD.PERIODID <'2013-2014'))' – venkatKA

回答

1

這可能是答案

SELECT SUM (UNCOLLECTED) 
     FROM LIQUIDATIONSDETAILS LD 
     WHERE  LD.COMPANYID = L.COMPANYID 
      AND LD.GROUPID = L.GROUPID 
      AND LD.PERIODID = L.PERIODID 
      AND (L.PERIODID > '2013-2014' AND LD.FORMTYPE IN (1, 2, 3)) 
      OR (L.PERIODID <= '2013-2014' AND LD.FORMTYPE IN (1, 2, 3,4)) 
1

您所查詢的格式不正確。您有兩個表別名,LLD,但未定義L

因爲您打算使用JOIN,所以應該使用明確的JOIN語法。一個簡單的規則:從不FROM子句中使用逗號。 始終使用顯式JOIN語法:

SELECT SUM(UNCOLLECTED) 
FROM LIQUIDATIONSDETAILS LD JOIN 
    ??? L  -- I don't know what table "L" refers to 
    ON LD.COMPANYID = L.COMPANYID AND 
     LD.GROUPID = L.GROUPID AND 
     LD.PERIODID = L.PERIODID 
WHERE LD.FORMTYPE IN (1, 2, 3) OR 
     (LD.FORMTYPE = 4 AND L.PERIODID > '2013-2014')