如何向现有表格中显示两个计算列

huangapple go评论47阅读模式
英文:

How to show two calculated columns to an existing table

问题

我想要提取一个报告,该报告将在现有表(Shipment_Info)的前两列中添加两列。Shipment_Info具有三列ShipmentID和ItemID和Item_Status。ItemID的值始终是唯一的,但ShipmentID会重复,因为不同的物品可以在同一批次中,并且物品状态可以处于不同的状态(分配、已装满和已打包)

我想运行一个查询,将在现有表(前两列)中添加两列。第一列(Shipment_Size)将显示特定ShipmentID中有多少个唯一物品,第二列(Shipment_Ready)将显示整个ShipmentID是否准备好发货。为了使ShipmentID准备好,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

理想输出。

ShipmentID ItemID Shipment_Size Shipment_Ready
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

有人帮助我使用此查询获取Shipment_Size,但我很难弄清如何将Shipment_Ready列集成到下面的查询中。

select s.shipmentID, s.ItemID, i.Shipment_Size 
from Shipment_INFO s 
  inner join (select shipmentID, count(*) as Shipment_Size 
  from Shipment_INFO 
  group by shipmentID) i on i.shipmentID = s.shipmentID

请注意,上面的查询只提供了Shipment_Size,您需要将Shipment_Ready整合到查询中。此处是一个示例查询,可以获得Shipment_Ready:

select s.shipmentID, s.ItemID, i.Shipment_Size, 
  case 
    when p.Item_Status = 'Packed' then 'Yes' 
    else 'No' 
  end as Shipment_Ready 
from Shipment_INFO s 
inner join (
  select shipmentID, count(*) as Shipment_Size 
  from Shipment_INFO 
  group by shipmentID
) i on i.shipmentID = s.shipmentID
left join (
  select shipmentID, max(Item_Status) as Item_Status 
  from Shipment_INFO 
  group by shipmentID
) p on p.shipmentID = s.shipmentID

这将在结果中添加Shipment_Ready列,表示是否整个ShipmentID准备好发货。

英文:

I would like to pull a report that will add two columns to the first two columns of an existing table (Shipment_Info). 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)

I want to run a query that will add two column to the existing table (first two columns). The first new column (Shipment_Size) will show how many unique items there are in that specific shipmentID, and the 2nd new column (Shipment_ready) will show if the entire shipmentID is ready to be shipped. For an shipmentID to be ready to be ItemIDs must be in a "Packed" status. Any help would be greatly appreciated.

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

Ideal output.

ShipmentID ItemID Shipment_Size Shipment_Ready
10001 20001 1 Yes
10002 20002 2 No
10002 20003 2 No
10003 20004 1 No
10004 20005 3 Yes
10004 20006 3 Yes
10004 20007 3 Yes
10005 20008 2 No
10005 20009 2 No
10006 20010 1 No

Someone help me with getting the Shipment_Size using this query, but I'm struggling to figure out how to integrate the the Shipment_Packed column to the query below.

select s.shipmentID, s.ItemID, i.Shipment_Size 
from Shipment_INFO s 
  inner join ( select shipmentID, count(*) as Shipment_Size 
  from Shipment_INFO 
  group by shipmentID ) i on i.shipmentID=s.shipmentID`

答案1

得分: 1

以下是代码的翻译部分:

select
    shipmentid,
    itemid,
    count(*) over (partition by shipmentid) as shipment_size,
    case
        when (
            select count(*) from shipment_info si2
            where si2.shipmentid = si.shipmentid
            and si2.item_status = 'Packed'
        ) = count(*) over (partition by shipmentid)
        then 'Yes'
        else 'No'
    end as shipment_ready
from
    shipment_info si

或者:

with
    shipment_status as (
        select
            shipmentid,
            count(1) as shipment_size,
            sum(case item_status when 'Packed' then 1 else 0 end) as packed_count
        from
            shipment_info
        group by
            shipmentid
    )
select
    si.shipmentid,
    si.itemid,
    ss.shipment_size,
    case ss.packed_count
        when ss.shipment_size then 'Yes'
        else 'No'
    as shipment_ready
from
    shipment_info si
    join
    shipment_status ss on ss.shipmentid = si.shipmentid
英文:

Something like this should work

select
    shipmentid,
    itemid,
    count(*) over (partition by shipmentid) as shipment_size,
    case
        when (
            select count(*) from shipment_info si2
            where si2.shipmentid = si.shipmentid
            and si2.item_status = 'Packed'
        ) = count(*) over (partition by shipmentid)
        then 'Yes'
        else 'No'
    end as shipment_ready
from
    shipment_info si

or:

with
    shipment_status as (
        select
            shipmentid,
            count(1) as shipment_size,
            sum(case item_status when 'Packed' then 1 else 0 end) as packed_count
        from
            shipment_info
        group by
            shipmentid
    )
select
    si.shipmentid,
    si.itemid,
    ss.shipment_size,
    case ss.packed_count
        when ss.shipment_size then 'Yes'
        else 'No'
    as shipment_ready
from
    shipment_info si
    join
    shipment_status ss on ss.shipmentid = si.shipmentid

</details>



# 答案2
**得分**: 0

以下是您要翻译的代码部分:

```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 'Yes' else 'no' end as Shipment_Ready
from Shipment_INFO
group by ShipmentID, Item_status, ItemID

请注意,上面的代码部分已经翻译为中文。

英文:

example for you :

  select ShipmentID,ItemID,
           count(ItemID) over (partition by ShipmentID) Shipment_Size,
          case when 
      sum(case when Item_status=&#39;Packed&#39; then 1 else 0 end) OVER (partition by ShipmentID ) =count(ItemID) over (partition by ShipmentID)
                   then &#39;Yes&#39; else &#39;no&#39; end as Shipment_Ready
      from Shipment_INFO
    group by ShipmentID,Item_status,ItemID

如何向现有表格中显示两个计算列
demo :https://dbfiddle.uk/80HA0Njd

答案3

得分: 0

你想选择所有行,但对于每一行,要查看整个装运情况。为此,请使用带有OVER (PARTITION BY shipmentid)的分析函数:

select
  shipmentid,
  itemid,
  count(*) over (partition by shipmentid) as shipment_size,
  min(item_status) over (partition by shipmentid) = 'Packed' as shipment_ready
from shipment_info
order by itemid;

我在这里使用了一个小技巧。'Packed' 是按字母顺序的最后状态。因此,如果装运的最低状态是'Packed',则表示其所有物品都已打包好,装运准备就绪。

英文:

You want to select all rows, but for each row look at its whole shipment. To do this use analytic funtions with OVER (PARTITION BY shipmentid):

select
  shipmentid,
  itemid,
  count(*) over (partition by shipmentid) as shipment_size,
  min(item_status) over (partition by shipmentid) = &#39;Packed&#39; as shipment_ready
from shipment_info
order by itemid;

I am using a trick here. 'Packed' is the last status in alphabetical order. So if the minimum status for a shipment is 'Packed', then all its items are packed and the shipment is ready.

huangapple
  • 本文由 发表于 2023年2月10日 04:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定