简体   繁体   English

复杂的多表库存SQL查询

[英]Complex Multiple Table Inventory SQL Query

I have 3 Tables: Assuming "2019-07-19" is the (Current Date) 我有3个表:假设“(当前日期)为“ 2019-07-19”

1.Inventory: 1.库存:

___________________________________________________
|ID| TransactionDate | ItemID | ItemName |Quantity|
+-------------------------------------------------+
|1 |2019-07-18       | 1      |Lemon     |100     |
+-------------------------------------------------+
|2 |2019-07-19       | 2      |Sugar     |100     | 
+-------------------------------------------------+
|3 |2019-07-19       | 2      |Sugar     |100     |
+-------------------------------------------------+
|4 |2019-07-18       | 1      |Lemon     |100     |
+-------------------------------------------------+ 
|5 |2019-07-19       | 1      |Lemon     |100     |
+-------------------------------------------------+
|6 |2019-07-18       | 2      |Sugar     |100     |
+-------------------------------------------------+ 
|7 |2019-07-18       | 1      |Lemon     |100     |
+-------------------------------------------------+

2.ConsumedItems: 2.ConsumedItems:

___________________________________________________ 
|ID| TransactionDate | ItemID | ItemName |Quantity|
+-------------------------------------------------+
|1 |2019-07-18       | 1      |Lemon     |10      |
+-------------------------------------------------+
|2 |2019-07-19       | 2      |Sugar     |10      |
+-------------------------------------------------+
|3 |2019-07-19       | 2      |Sugar     |10      |
+-------------------------------------------------+
|4 |2019-07-18       | 1      |Lemon     |10      |
+-------------------------------------------------+
|5 |2019-07-19       | 1      |Lemon     |10      |
+-------------------------------------------------+
|6 |2019-07-18       | 2      |Sugar     |10      |
+-------------------------------------------------+
|7 |2019-07-18       | 1      |Lemon     |10      |
+-------------------------------------------------+

3.DamagedItems: 3.DamagedItems:

___________________________________________________  
|ID| TransactionDate | ItemID | ItemName |Quantity|
+-------------------------------------------------+
|1 |2019-07-18       | 1      |Lemon     |10      |
+-------------------------------------------------+
|2 |2019-07-19       | 2      |Sugar     |10      |
+-------------------------------------------------+
|3 |2019-07-19       | 2      |Sugar     |10      |
+-------------------------------------------------+
|4 |2019-07-18       | 1      |Lemon     |10      |
+-------------------------------------------------+  
|5 |2019-07-19       | 1      |Lemon     |10      |
+-------------------------------------------------+
|6 |2019-07-18       | 2      |Sugar     |10      |
+-------------------------------------------------+
|7 |2019-07-18       | 1      |Lemon     |10      |
+-------------------------------------------------+

How can I get this Output? 如何获得此输出?

  1. PrevBalance=[Sum Of All Previous Inventories] - ([Sum of all Previous Damaged] + [Sum Of All Consumed]) PrevBalance = [以前所有库存的总和]-([以前所有损坏的总和] + [所有消耗的总和])
  2. DmgToday=[Sum of all Current Damaged] DmgToday = [当前所有损坏的总和]
  3. CnsmdToday=[Sum of all Current Consumed] CnsmdToday = [当前消耗的总和]
  4. DlvrdToday=[Sum of all Items in Inventory Today] DlvrdToday = [今天库存中所有项目的总和]

| ItemID | ItemName |PrevBalance|DlvrdToday|DmgToday|CnsmdToday|CurrentBal
+------------------------------------------------------------------------+
|1       |Lemon     | 240       |100       |10      |10        |320      |
+------------------------------------------------------------------------+
|2       |Sugar     | 80        |200       |20      |20        |240      |
+-------------------------------------------------------------------------+

Working Code: 工作代码:

select Inventory.ItemID, Inventory.ItemName,  
sum(case when Inventory.TransactionDate < CURDATE() then Inventory.Quantity 
else 0 end)-sum(case when ConsumedItems.TransactionDate <CURDATE() then 
ConsumedItems.Quantity else 0 end)-sum(case when 
DamagedItems.TransactionDate<CURDATE() then DamagedItems.Quantity else 0 end) as 'PrevBalance',

sum(case when Inventory.TransactionDate=CURDATE() then Inventory.Quantity else 0 end) as 'DeliveredToday',

sum(case when DamagedItems.TransactionDate=CURDATE() then DamagedItems.Quantity else 0 end) as 'DamagedToday',

sum(case when ConsumedItems.TransactionDate=CURDATE() then ConsumedItems.Quantity else 0 end) as 'ConsumedToday',

sum(case when Inventory.TransactionDate < CURDATE() then Inventory.Quantity else 0 end)-
sum(case when ConsumedItems.TransactionDate <CURDATE() then ConsumedItems.Quantity else 0 end)-
sum(case when DamagedItems.TransactionDate<CURDATE() then DamagedItems.Quantity else 0 end)-
sum(case when ConsumedItems.TransactionDate=CURDATE() then ConsumedItems.Quantity else 0 end)-
sum(case when DamagedItems.TransactionDate=CURDATE() then DamagedItems.Quantity else 0 end)+
sum(case when Inventory.TransactionDate=CURDATE() then Inventory.Quantity else 0 end) as 'Total Balance' 

from Inventory 
join ConsumedItems on ConsumedItems.ID = Inventory.ID 
join DamagedItems on DamagedItems.ID = Inventory.ID
group by  Inventory.ItemID, Inventory.ItemName`

Use the below query 使用以下查询

select Inventory.ItemID, Inventory.ItemName, 
sum(case when Inventory.TransactionDate<currentdate() then Inventory.Quantity else 0 end)+sum(case when ConsumedItems.TransactionDate<currentdate() then ConsumedItems.Quantity else 0 end)-sum(case when DamagedItems.TransactionDate<currentdate() then DamagedItems.Quantity else 0 end) as 'PrevBalance',
sum(case when DamagedItems.TransactionDate=currentdate() then DamagedItems.Quantity else 0 end) as 'DamagedToday',
sum(case when ConsumedItems.TransactionDate=currentdate() then ConsumedItems.Quantity else 0 end) as 'ConsumedToday', 
sum(case when ConsumedItems.TransactionDate=currentdate() then ConsumedItems.Quantity else 0 end)+sum(case when DamagedItems.TransactionDate=currentdate() then DamagedItems.Quantity else 0 end)+sum(case when Inventory.TransactionDate=currentdate() then Inventory.Quantity else 0 end) as 'DeliveredToday' 
from Inventory 
join ConsumedItems on ConsumedItems.ID = Inventory.ID 
join DamagedItems on DamagedItems.ID = Inventory.ID
group by  Inventory.ItemID, Inventory.ItemName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM