SQL:Store Produce同列数据相加

下面是SP源码,数据源在#Fee中,实现这个只需要一句话,看*号注释的那句话,最后效果看附图。

ALTER PROCEDURE [dbo].[Report_Loading_List]
@Flag nvarchar(10),
@Document_NO nvarchar(50)
AS
BEGIN
//同列相加的数据源
create table #Fee(CNO nvarchar(50),name nvarchar(50),amount nvarchar(20))
insert into #Fee(CNO,name,amount)
select C_ID_STR,TUC_SHIPPING_FEE.SUBJECT,TUC_SHIPPING_FEE.MONEY
from TUC_SHIPPING_FEE where C_ID_STR in (select C_ID_STR from TUC_Container_Header where Document_NO=@Document_NO)
//源码
create table #Container(C_ID_STR varchar(50),whse char(4),Ship_NO varchar(50),Container_Type varchar(50),
Total_CUFT float,Vessel_NO varchar(50),Shipping_Date datetime,Closing_Date datetime,Closing_Port nvarchar(50),
ETA datetime,Destination nvarchar(100),Loading_Place nvarchar(100),QC_By nvarchar(50),Vessel_Name nvarchar(50),
Carrier nvarchar(50),Vendor_NO nvarchar(50),Vendor_Name nvarchar(50),
QTY_Shipped float,Item_NO varchar(30),Item_Name nvarchar(50),weight float,CUFT float,Carton_Size nvarchar(50),Gross_Weight float,
Net_Weight float,Carton_NO nvarchar(50),Length float,Width float,Height float,Detail_Vendor_NO nvarchar(50)
,Detail_Vendor_Name nvarchar(50),Price float,Amount float,PONO nvarchar(50),Fee nvarchar(500),Vessel_Company nvarchar(20),shipping_vendor nvarchar(30),Total_Fee float)

if @Flag='1'
insert into #Container(C_ID_STR,whse,Ship_NO ,Container_Type ,Total_CUFT,Vessel_NO,Shipping_Date,
Closing_Date,Closing_Port,ETA,Destination,Loading_Place,QC_By,Vessel_Name,Carrier,Vendor_NO,Vendor_Name,
QTY_Shipped,Item_NO,Item_Name,weight,CUFT,Carton_Size,Gross_Weight,Net_Weight,Carton_NO,Length,Width,Height
,Detail_Vendor_NO,Price,Amount,PONO,shipping_vendor,Fee,Total_Fee)
select
H.C_ID_STR,isnull(H.whse,''),isnull(H.Ship_NO,''),isnull(H.Container_Type,''),isnull(H.Total_CUFT,0),
isnull(H.Vessel_NO,''),isnull(H.Shipping_Date,''),isnull(H.Closing_Date,''),
Closing_Port=(select closing_port from tuc_shipping_document where document_no=@Document_NO)
,isnull(H.ETA,'')
,Destination=isnull(H.Destination,'')
,isnull(H.Loading_Place,''),
isnull(H.QC_By,''),isnull(H.Vessel_Name,''),isnull(H.Carrier,''),isnull(H.Vendor_NO,''),
Vendor_Name=(select Vendor_Name from EX_VENDOR_INFO where Vendor_No=h.Vendor_NO),

isnull(D.QTY_Shipped,0),isnull(D.Item_NO,''),
Item_Name=(select top 1 Descrip from EX_ITEM_INFO where Item_No=d.Item_NO)
,isnull(D.weight,0),isnull(D.CUFT,0),
D.Carton_Size,isnull(D.Gross_Weight,0),isnull(D.Net_Weight,0) ,isnull(D.Carton_NO,''),
isnull(D.Length,0),isnull(D.Width,0),isnull(D.Height,0)
,Detail_Vendor_NO=(select CUST_ID from TUC_PO_HEADER where PO_ID_STR=D.PO_NO)
,Price=isnull((select price from TUC_PO_DETAIL where PO_ID_STR=d.PO_NO and line_ID=d.PO_LINE_ID),0)
,Amount=isnull(D.QTY_Shipped,0)*isnull((select price from TUC_PO_DETAIL where PO_ID_STR=d.PO_NO and line_ID=d.PO_LINE_ID),0)
,PONO=pcd.ITEM_MEMO
,shipping_vendor=vendor.Vendor_Name
//同列相加*************************************************************************************************
,Fee=(select top 1 stuff((select ',     '+ltrim(name+': '+amount) from #Fee where CNO=b.CNO for xml path('')),1,1,'') from #Fee b where b.CNO=H.C_ID_STR)
//*******************************************************************************************************

,Total_Fee=isnull((select sum(convert(float,amount)) from #Fee where CNO=H.C_ID_STR),0)

from TUC_Container_Header H inner join TUC_Container_Detail D
on h.C_ID_STR = D.C_ID_STR
left join TUC_PC_DISTRIBUTEREQUEST_PO_LOG l on l.PO_ID_STR=D.PO_NO and l.PO_LINE_NUM=D.PO_LINE_ID and l.BRANCH<>l.VENDOR and l.ORIGNAL_QTY>0
left join TUC_PC_DETAIL pcd on pcd.PC_ID_STR=l.PC_ID_STR and pcd.LINE_ID=l.PC_LINE_NUM
left join TUC_Container_Share_Vendor share on share.Shipping_Ref=H.C_ID_STR
left join EX_VENDOR_INFO vendor on vendor.Vendor_No=share.Shipping_Vendor
where h.Document_NO=@Document_NO

select * from #Container

数据源与结果呈现图:

原文地址:https://www.cnblogs.com/hiflora/p/2434730.html