如何获取对象中所有对象的总和?

时间:2016-06-02 14:00:35

标签: sql

我正在尝试获取容器中所有部件的总和。我现在的方式,总和(重量),只会抓住容器中第一部分的第一个重量。我想抓住容器编号相同的所有部件重量。表中有许多不同的容器编号。我希望该语句使用不同的容器号,并且只在第一次出现的容器号的行中插入值。

http://s33.postimg.org/3t63t83hr/sumweight.png

每个部分都具有上述重量。我想为每个容器编号计算这些权重,并将其总结在第一行,如图所示。

,(case when mu.master_unit_no is null
then c.Gross_weight
when mu.master_unit_no is not null
then sum(c.Gross_weight)+mut.tare_weight
end)
as 'Weight in LBS'

现在我有这个查询,但它只返回第一部分重量+皮重。我想抓住容器所有部件的总和。

/* I-Dashboards Shipping Report */
/* ROTW 11-21-2015 */
select
p.part_no  AS 'Part_Number'
,p.name AS 'Description'
,c.serial_no as 'S#'
,c.quantity AS 'Qty'
,cp.customer_part_No as 'F_NUMBER'
--,cast(mut.length AS varchar) + 'X' + Cast(mut.width as varchar) + 'X' + Cast(mut.Height as varchar) as 'dim MU'
,(CASE when mut.length is null
then 0
else cast(mut.length as int) end) as 'M_LEN' 
,(CASE when mut.width is null
then 0
else cast(mut.width as int) end) As 'M_WD' 
,(CASE when mut.height is null
then 0
else cast(mut.Height as int) end) AS 'M_HT'
,cast(pct.cube_length AS INT) as 'S_LEN'
,cast(pct.cube_width AS INT) AS  'S_WD'
,cast(pct.cube_height AS INT) AS 'S_HT'
,mut.tare_Weight as 'M_Tare_lbs'
,c.Gross_weight as 'Net_Wt_lbs'
,mu.master_unit_no as 'M Number'
,g.Booking_No as 'Booking_HAWB_Num'
,concat(g.cargo_container_no, '-', g.dock_code) as 'Container_ID'
,g.outbound_scac_code AS 'Carrier'

,concat(cast(pct.cube_length as int), 'x', cast(pct.cube_width as int), 'x',     cast(pct.cube_height as int)) as 'BOX_DIMS_INCHES'

,(case when row_number() over (partition by mu.master_unit_no order by mu.master_unit_no) = 1
then concat(cast(mut.length as int), 'x', cast(mut.width as int), 'x',        cast(mut.Height as int))
when mu.master_unit_no is null
then ''
end)
as 'PALLET_DIMS_INCHES'

,(case when g.booking_container_type_key = 6 THEN
'DIRECT'
when g.booking_container_type_key = 5 THEN
'AIR'
else 'CEVA-Ocean'
end) as 'Shipment Type'

,CASE
--WHEN(ROW_NUMBER() OVER (PARTITION BY mu.master_unit_no ORDER BY     mu.master_unit_no)) = 1 
--then (select sum((pct.cube_length*0.0254)*(pct.cube_width*0.0254)*    (pct.cube_height*0.0254))
--from part_v_container c where c.master_unit_key = mu.master_unit_key)
when mu.master_unit_no is null
then (pct.cube_length*0.0254)*(pct.cube_width*0.0254)*  (pct.cube_height*0.0254) 
end as 'CBM'

,select c.*, CASE
       WHEN(ROW_NUMBER() OVER (PARTITION BY mu.master_unit_no ORDER BY     mu.master_unit_no)) = 1 
       THEN **(**select SUM(c.Gross_weight)+mut.tare_weight 
       from part_v_container c where c.master_unit_no =   mu.master_unit_no**)** END AS 'Total Weight'


from part_v_container c

我正在尝试将所有零件总重量的总和以m为单位加上该m数的皮重和商店的总重量。

1 个答案:

答案 0 :(得分:2)

像Siyual所说,添加表格以帮助我们更好地理解。在那之前,我相信我拥有你想要的大部分内容。

你的桌子可能看起来像......

part_id    container_ id    Weight
-------    -------------    ------
1          a                5
2          a                5
3          b                99
4          a                3
5          c                99

你可能想要一个结果(例如使用container_id = a)......

Weight 
------
13

试试这个......

SELECT SUM(Weight) FROM someTable WHERE container_id = someContainer

在结果例子的情况下,我给了我做...

SELECT SUM(Weight) FROM someTable WHERE container_id = 'a'

我不完全确定您的上一部分是什么意思"只在第一次出现的容器编号" 的行中插入值。你为什么要特别想要这个?

编辑1

但最终结果不应该有多个container_id。我做了以下......

我的桌子......

enter image description here

SELECT t1.container_id, SUM(t1.weight) FROM table_1 t1 JOIN table_1 t2 ON t1.part_id = t2.part_id GROUP BY t1.container_id

结果是......

enter image description here

编辑2

我花了一段时间,但我想我明白了:)

表:

enter image description here enter image description here

查询:

SELECT t.*, CASE 
            WHEN(ROW_NUMBER() OVER (PARTITION BY t.Container ORDER BY t.Container)) = 1 
            THEN (SELECT SUM(t2.Weight) FROM table1 t2 WHERE t2.Container = t.Container) 
            ELSE 0 END AS 'Total Weight' 
            FROM table1 t GROUP BY t.Container, t.Part, t.Weight

结果:

enter image description here

编辑3

这是你原来的......

select c.*, CASE
           WHEN(ROW_NUMBER() OVER (PARTITION BY mu.master_unit_no ORDER BY mu.master_unit_no)) = 1 
           THEN select SUM(c.Gross_weight)+mut.tare_weight 
           from part_v_container c where c.master_unit_no = mu.master_unit_no END AS 'Total Weight' 

这就是我要改变的东西(两边都有两个星号EX:** A **)......

select c.*, CASE
           WHEN(ROW_NUMBER() OVER (PARTITION BY mu.master_unit_no ORDER BY mu.master_unit_no)) = 1 
           THEN **(**select SUM(c.Gross_weight)+mut.tare_weight 
           from part_v_container c where c.master_unit_no = mu.master_unit_no**)** END AS 'Total Weight'

您需要括号,因为代码不知道结尾属于哪里。括号允许SQL知道结尾属于case语句。我也不知道亩在哪里。和mut。来自。看起来他们属于一个你从未在这里引用的不同的表?

我不确定您是否添加了它但是在“总重量”之后#39;你错过了

 from someTable group by (all things that are in your select aka things that will be output need to be here...see my previous example for a better understanding)

如果您愿意,可以在原始问题上发布确切表格的屏幕截图(或手动创建),这样我就可以使用您准确使用的名称,让您更容易理解:)

编辑4

/* I-Dashboards Shipping Report */
/* ROTW 11-21-2015 */
select
p.part_no  AS 'Part_Number'
,p.name AS 'Description'
,c.serial_no as 'S#'
,c.quantity AS 'Qty'
,cp.customer_part_No as 'F_NUMBER'
--,cast(mut.length AS varchar) + 'X' + Cast(mut.width as varchar) + 'X' + Cast(mut.Height as varchar) as 'dim MU'
,(CASE when mut.length is null
then 0
else cast(mut.length as int) end) as 'M_LEN' 
,(CASE when mut.width is null
then 0
else cast(mut.width as int) end) As 'M_WD' 
,(CASE when mut.height is null
then 0
else cast(mut.Height as int) end) AS 'M_HT'
,cast(pct.cube_length AS INT) as 'S_LEN'
,cast(pct.cube_width AS INT) AS  'S_WD'
,cast(pct.cube_height AS INT) AS 'S_HT'
,mut.tare_Weight as 'M_Tare_lbs'
,c.Gross_weight as 'Net_Wt_lbs'
,mu.master_unit_no as 'M Number'
,g.Booking_No as 'Booking_HAWB_Num'
,concat(g.cargo_container_no, '-', g.dock_code) as 'Container_ID'
,g.outbound_scac_code AS 'Carrier'

,concat(cast(pct.cube_length as int), 'x', cast(pct.cube_width as int), 'x',     cast(pct.cube_height as int)) as 'BOX_DIMS_INCHES'

,(case when row_number() over (partition by mu.master_unit_no order by mu.master_unit_no) = 1
then concat(cast(mut.length as int), 'x', cast(mut.width as int), 'x',        cast(mut.Height as int))
when mu.master_unit_no is null
then ''
end)
as 'PALLET_DIMS_INCHES'

,(case when g.booking_container_type_key = 6 THEN
'DIRECT'
when g.booking_container_type_key = 5 THEN
'AIR'
else 'CEVA-Ocean'
end) as 'Shipment Type'

,(case when row_number() over (partition by mu.master_unit_no order by     mu.master_unit_no) = 1
then (pct.cube_length*0.0254)*(pct.cube_width*0.0254)*(pct.cube_height*0.0254) 
when mu.master_unit_no is null
then (pct.cube_length*0.0254)*(pct.cube_width*0.0254)*  (pct.cube_height*0.0254) 
end)
as 'CBM'

,CASE
       WHEN(ROW_NUMBER() OVER (PARTITION BY mu.master_unit_no ORDER BY     mu.master_unit_no)) = 1 
       THEN (SELECT SUM(c.Gross_weight) + mut.tare_weight 
       from part_v_container c where c.master_unit_no =   mu.master_unit_no) END AS 'Total Weight'


from part_v_container c

所以这应该解决了我的问题。我确实有额外的评论。你拥有所有这些不同的前缀(p。,c。,mut。,mu。,g。,pct。)。你在哪里提到所有这些?我可以看到你在哪里引用c(它就在决赛之后)。甚至在我的部分你使用mut。但我不知道你是如何引用它的。例如,由于from part_v_container c,c可用。 c代表part_v_container。您可以查看joins以帮助您获取其他表格。如果您愿意,可以编辑原始问题并将所有表格添加到其中(无论它们是实际还是示例)。我只需要知道不同的表名和列名。我不关心实际数据。我希望我个人认识你,因为这会更容易实时xD

编辑5

使用此表...

enter image description here

我使用了这个查询...

;WITH mult AS (SELECT (m.length*0.0254)*(m.width*0.0254)*(m.height*0.0254) AS multiply, m.container FROM measurement m)
, sumMult AS (SELECT SUM((m.length*0.0254)*(m.width*0.0254)*(m.height*0.0254)) AS sumMultiply, m.container FROM measurement m GROUP BY m.container)
, combine AS (SELECT s.sumMultiply AS sumMultiply, m.multiply AS multiply, m.container FROM mult m JOIN sumMult s ON m.container = s.container)
SELECT c.container, CASE WHEN (ROW_NUMBER() OVER (PARTITION BY c.container ORDER BY c.container)) = 1 
   THEN (SELECT c.sumMultiply)

   ELSE (SELECT c.multiply)
END AS 'Cubic Meters'

FROM combine c GROUP BY c.container, c.sumMultiply, c.multiply

它为容器中所有部件的所有卷提供SUMS,并仅在第一行(第一部分)中显示它。剩下的部分都有它们的音量。

我无法完全转换它。我相信,既然您已在我之前的查询中成功完成,那么您可以正确转换它。我尽量保持表格和列的名称尽可能平淡和可识别。它看起来像你想要它的工作方式。如果您不知道;WITH mult....是什么......您可以将其视为一种功能。在你的巨大查询之前放入整个with语句(即mult,sumMult,combine)。您可以在我的查询中看到我的;WITH位于我的SELECT查询的第一位(上方),该查询会产生实际结果。