英文:
How do I join 2 tables and calculate 1 new column
问题
以下是你的SQL查询的中文翻译部分,不包括代码部分:
- 第一个新列 = Shipment_Size(在该装运中的itemsID的数量)
- 第二个新列 = Shipment_Ready(整个装运ID已准备好装运。为了使装运ID准备好装运,所有ItemID必须处于“Packed”状态)
- 第三个新列 = Item_Stored(此项目是否至少存储过一次)。如果至少存储过一次,值应为“是”,如果该项目从未至少存储过一次,值应为“否”。
Shipment_Info表:
Shipment_Info有三列ShipmentID、ItemID和Item_Status。ItemID的值始终是唯一的,但ShipmentID会重复,因为不同的物品可以在同一装运中,物品状态可以处于不同的状态(已分配、已填充和已打包)。
Item_Info表:
Item_Info有两列。ItemID、Operation和Op_time。ItemID会重复,因为该表显示了已对该ItemID执行的不同操作及相关时间。
DB表:Shipment_INFO
ShipmentID | ItemID | Item_status |
---|---|---|
10001 | 20001 | Packed |
10002 | 20002 | Allocated |
10002 | 20003 | Packed |
10003 | 20004 | Filled |
10004 | 20005 | Packed |
10004 | 20006 | Packed |
10004 | 20007 | Packed |
10005 | 20008 | Filled |
10005 | 20009 | Packed |
10006 | 20010 | Filled |
DB表:Item_Info
ItemID | Item_status |
---|---|
20001 | Induct |
20001 | Stock |
20002 | Induct |
20002 | Stock |
20002 | Stored |
20002 | Dock |
20003 | Induct |
20003 | Stock |
20003 | Stored |
20004 | Induct |
20004 | Cancelled |
20004 | Stored |
20005 | Induct |
20005 | Stock |
20005 | Stored |
20006 | Induct |
20006 | Reject |
20006 | Induct |
20006 | Stock |
20007 | Induct |
20007 | Stock |
20007 | Stored |
20007 | Stored |
20008 | Induct |
20008 | Stock |
20008 | Reject |
20009 | Induct |
20009 | Stock |
20009 | Induct |
20009 | Stored |
20010 | Induct |
20010 | Stock |
理想输出:
ShipmentID | ItemID | Shipment_Size | Shipment_Ready | Item_Stored |
---|---|---|---|---|
10001 | 20001 | 1 | 是 | 否 |
10002 | 20002 | 2 | 否 | 是 |
10002 | 20003 | 2 | 否 | 是 |
10003 | 20004 | 1 | 否 | 是 |
10004 | 20005 | 3 | 是 | 是 |
10004 | 20006 | 3 | 是 | 否 |
10004 | 20007 | 3 | 是 | 是 |
10005 | 20008 | 2 | 否 | 否 |
10005 | 20009 | 2 | 否 | 是 |
10006 | 20010 | 1 | 否 | 是 |
以下是你提供的SQL查询,该查询获取了前两列,但你遇到了如何连接第二个表并获取第三列的问题。希望这有所帮助。
select ShipmentID, ItemID,
count(ItemID) over (partition by ShipmentID) as Shipment_Size,
case when
sum(case when Item_status='Packed' then 1 else 0 end) OVER (partition by ShipmentID) = count(ItemID) over (partition by ShipmentID)
then '是' else '否' end as Shipment_Ready
from Shipment_INFO
group by ShipmentID, Item_status, ItemID
英文:
I'm need to get 3 new columns with my SQL query. The first 2 columns only requires 1 table (Shipment_Info), but the 3rd column requires the 2nd table (Item_Info).
- 1st new column = Shipment_Size (# of itemsID in that shipment)
- 2nd new column = Shipment_ready (entire shipmentID is ready to be shipped. For an shipmentID to be ready to be shipped all the ItemIDs must be in a "Packed" status)
- 3rd new column = Item_Stored (was this item stored atleast 1 time). If it was stored atleast 1 time value should be yes and if the item was never stored atleast 1 time the value should be no.
Shipment_Info_table:
Shipment_Info has three columns ShipmentID and ItemID and Item_Status. ItemID values are always unique, but ShipmentIDs will repeat because different Items can be in the same shipment, and item status can be in different states (Allocated, Filled and Packed).
Item_Info table:
Item_Info has two columns. ItemID, Operation, Op_time. ItemIDs will repeat because this table shows the different operations that have occurred to this itemID and the associated times.
DB Table: Shipment_INFO
ShipmentID | ItemID | Item_status |
---|---|---|
10001 | 20001 | Packed |
10002 | 20002 | Allocated |
10002 | 20003 | Packed |
10003 | 20004 | Filled |
10004 | 20005 | Packed |
10004 | 20006 | Packed |
10004 | 20007 | Packed |
10005 | 20008 | Filled |
10005 | 20009 | Packed |
10006 | 20010 | Filled |
DB Table: Item_Info
ItemID | Item_status |
---|---|
20001 | Induct |
20001 | Stock |
20002 | Induct |
20002 | Stock |
20002 | Stored |
20002 | Dock |
20003 | Induct |
20003 | Stock |
20003 | Stored |
20004 | Induct |
20004 | Cancelled |
20004 | Stored |
20005 | Induct |
20005 | Stock |
20005 | Stored |
20006 | Induct |
20006 | Reject |
20006 | Induct |
20006 | Stock |
20007 | Induct |
20007 | Stock |
20007 | Stored |
20007 | Stored |
20008 | Induct |
20008 | Stock |
20008 | Reject |
20009 | Induct |
20009 | Stock |
20009 | Induct |
20009 | Stored |
20010 | Induct |
20010 | Stock |
Ideal Output:
ShipmentID | ItemID | Shipment_Size | Shipment_Ready | Item_Stored |
---|---|---|---|---|
10001 | 20001 | 1 | Yes | No |
10002 | 20002 | 2 | No | Yes |
10002 | 20003 | 2 | No | Yes |
10003 | 20004 | 1 | No | Yes |
10004 | 20005 | 3 | Yes | Yes |
10004 | 20006 | 3 | Yes | No |
10004 | 20007 | 3 | Yes | Yes |
10005 | 20008 | 2 | No | No |
10005 | 20009 | 2 | No | Yes |
10006 | 20010 | 1 | No | Yes |
The code below gets me the first two columns, but I'm having trouble on how to join 2nd table and get the 3 column. Any help would be greatly appreciated.
select ShipmentID,ItemID,
count(ItemID) over (partition by ShipmentID) Shipment_Size,
case when
sum(case when Item_status='Packed' then 1 else 0 end) OVER (partition by ShipmentID ) =count(ItemID) over (partition by ShipmentID)
then 'Yes' else 'no' end as Shipment_Ready
from Shipment_INFO
group by ShipmentID,Item_status,ItemID
答案1
得分: 1
以下是代码部分的翻译:
我认为你只是想要一个使用 `exists` 的简单的 `case` 表达式:
选择 ShipmentID、ItemID
、count(ItemID) over (partition by ShipmentID) as Shipment_Size
、case when
sum(case when Item_status='Packed' then 1 else 0 end) over (partition by ShipmentID ) = count(ItemID) over (partition by ShipmentID)
then 'Yes' else 'No' end as Shipment_Ready
、case when exists (select 1 from Item_Info ii where ii.ItemId = si.ItemId and ii.Item_Status = 'Stored') then 'Yes' else 'No' end as Item_Stored
从 Shipment_INFO si
按 ShipmentID、Item_status、ItemID 分组;
英文:
I think you just want a simple case
expression using exists
:
select ShipmentID, ItemID
, count(ItemID) over (partition by ShipmentID) Shipment_Size
, case when
sum(case when Item_status='Packed' then 1 else 0 end) over (partition by ShipmentID ) = count(ItemID) over (partition by ShipmentID)
then 'Yes' else 'No' end as Shipment_Ready
, case when exists (select 1 from Item_Info ii where ii.ItemId = si.ItemId and ii.Item_Status = 'Stored') then 'Yes' else 'No' end as Item_Stored
from Shipment_INFO si
group by ShipmentID, Item_status, ItemID;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论