如果where子句中的语句?

时间:2016-05-27 14:19:13

标签: sql sql-server sqlfiddle

所以我必须针对数据库模式运行查询:http://sqlfiddle.com/#!9/d0b643,但示例模式如下:

Table 1
itemID          sale date     salesmanID   storeID
---------------------------------------------------
1                 1/2015        1             1
1                 3/2016        1             1
2                 5/2016        2             1
2                 1/2015        4             1

Table 2
itemID           colorID           price
--------------------------------------
1                 1                23
1                 2                10
1                 3                13
2                 1                11
2                 2                14
2                 3                18

Table 3
ColorID       color
---------------------------------------
 1             Red
 2             Blue
 3             Green

Table 4
SaleBegin       SaleEnd      ColorID      salesmanID     storeID
----------------------------------------------------------------
1/1/2014        12/31/2014      1            0             1
1/1/2015        12/31/2015      2            0             1
1/1/2016        12/31/2016      3            0             1
1/1/2014        12/31/2014      3            2             1
1/1/2015        12/31/2016      2            2             1

我需要在where子句中有一些内容,如果有一个SalesmanID,并且Table1中的saleDate落在Table4的StartDate和Enddate之间,请使用该颜色。否则,如果没有salesmanID,请使用StoreID(在此示例中,它们都是1,但它们可能不同)。

我添加的当前查询是:

select t1.itemID,t3.color,t2.price
from table_1 t1
LEFT JOIN table_2 t2
ON t1.itemID = t2.itemID
LEFT JOIN table_3 t3
ON t2.colorID = t3.colorID
LEFT JOIN table_4 t4
ON t3.colorID = t4.colorID
WHERE t1.sale_date BETWEEN saleBegin and saleEnd;

我该怎么办?预期结果应如下所示:

itemID颜色价格

1           Blue        10
1           Green       13
2           Blue        14
2           Blue        14

2 个答案:

答案 0 :(得分:0)

我认为这就是你想要的。由于商店@和销售日期之间的重叠,您将获得太多行。一旦我更改了样本数据表4,使最后两行成为商店#2,我就会得到您发布的结果。

1/1/2014        12/31/2014      3            2             2 
1/1/2015        12/31/2016      2            2             2 

Select t1.itemID,t3.color,t2.price
    From table_1 t1
    LEFT JOIN table_4 t4 ON t1.sale_date BETWEEN t4.saleBegin And t4.saleEnd And t1.SalesmanID = t4.SalesmanID
    LEFT JOIN table_4 t4a ON t1.sale_date BETWEEN t4a.saleBegin And t4a.saleEnd And t1.StoreID = t4a.StoreID
    LEFT JOIN table_2 t2 ON t1.itemID = t2.itemID And t2.ColorID = t4a.ColorID
    LEFT JOIN table_3 t3 ON t2.colorID = t3.colorID
    Where t4.SalesmanID Is Null
    Group By t1.itemID,t3.color,t2.price
Union All
Select t1.itemID,t3.color,t2.price
    From table_1 t1
    LEFT JOIN table_4 t4 ON t1.sale_date BETWEEN t4.saleBegin And t4.saleEnd And t1.SalesmanID = t4.SalesmanID
    LEFT JOIN table_2 t2 ON t1.itemID = t2.itemID And t2.ColorID = t4.ColorID
    LEFT JOIN table_3 t3 ON t2.colorID = t3.colorID
    Where t4.SalesmanID Is Not Null

itemID  color   price
1       Blue    10.000
1       Green   13.000
2       Blue    14.000
2       Blue    14.000

答案 1 :(得分:0)

我正在尝试使用MSSQL逻辑。并尝试在SQL中调整它运行成功。 只需自己尝试使用此代码,看看这是否是您想要的。

select t1.itemID,t3.color,t2.price from table_1 t1 
LEFT JOIN table_4 t41 
on t1.sale_date BETWEEN t41.saleBegin and t41.saleEnd 
and t41.salesmanID!=0 and t1.salesmanID=t41.salesmanID
LEFT JOIN table_4 t42 
on t1.sale_date BETWEEN t42.saleBegin and t42.saleEnd 
and t42.salesmanID=0 and t1.storeID=t42.storeID
INNER JOIN table_2 t2
ON t1.itemID = t2.itemID
INNER JOIN table_3 t3
ON t2.colorID = t3.colorID and t2.colorID=IF(ISNULL(t41.colorID),t42.colorID,t41.colorID)
order by t1.itemID,t3.color,t2.price;