Sql where子句中的If语句?
因此,我必须对数据库模式运行查询:,但示例模式如下所示:Sql where子句中的If语句?,sql,sql-server,sqlfiddle,Sql,Sql Server,Sqlfiddle,因此,我必须对数据库模式运行查询:,但示例模式如下所示: Table 1 itemID sale date salesmanID storeID --------------------------------------------------- 1 1/2015 1 1 1 3/2016 1 1 2
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,并且表1中的saleDate介于表4的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;
我如何运行这个?预期结果应如下所示:
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
itemID颜色价格
我想这就是你想要的。由于store@和Sale日期重叠,您将获得太多行。一旦我更改了示例数据表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
我想这就是你想要的。由于store@和Sale日期重叠,您将获得太多行。一旦我更改了示例数据表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
我正在尝试使用MSSQL逻辑。并在SQL中尝试fiddle it运行成功。 你自己试试这段代码,看看这是否是你想要的
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;
我正在尝试使用MSSQL逻辑。并在SQL中尝试fiddle it运行成功。 你自己试试这段代码,看看这是否是你想要的
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;
(x和y之间的SalesmanID或SalesmanID为NULL)和(a和c之间的b或b为NULL)
尝试在where子句中使用case语句可能会对您有所帮助。当表4中的SalesmanID
为零时,我们如何计算出itemID
是什么?(x和y之间的SalesmanID或SalesmanID为NULL)和(a和c或b之间的b为空)
尝试在where子句中使用case语句可能会对您有所帮助。当表4中的salesmanID
为零时,我们如何计算出itemID
是什么?不幸的是,在实际数据中,会有重叠。问题是我需要先查看它是否有salesmanID,如果有,然后从t行中获取颜色/价格他确定了它的日期。如果没有,那么从它所处的那一行中获取它与商店匹配的颜色/价格。这不是设计最好的系统,不幸的是,我没有重新设计的选择,也没有创建系统。如果有重叠,你想做什么,只需随机选择一个记录?让我们知道如果有重叠,优先顺序是什么。我在union中的第一个查询中添加了一个group by,这会产生所需的结果,但如果同一家商店有不同颜色的重叠销售,这并不能解决问题。由于销售的日期范围性质,这可能会变得相当复杂。您可能需要将范围扩展到单个l日期以找到确切的重叠,然后使用诸如row_number之类的函数来确定要保留哪个日期的优先级。不幸的是,在真实数据中,会有重叠。问题是我需要先查看它是否有salesmanID,如果有,然后从它所处的日期行中获取颜色/价格。如果没有,则获取颜色/它与它所属行中的商店匹配的位置的价格。这不是设计最好的系统,不幸的是我没有重新设计的选项,也没有创建系统。如果有重叠,你想做什么,只是随机选择一个记录?如果有重叠,让我们知道优先顺序。我在联合体中的第一个查询会产生所需的结果,但如果同一商店有不同颜色的重叠销售,则无法解决问题。由于销售的日期范围性质,这可能会变得相当复杂。您可能需要将范围扩展到各个日期,以查找准确的重叠,然后使用类似于row的函数_编号,以确定要保留的优先顺序。