34,590
社区成员
发帖
与我相关
我的任务
分享
--> --> (Roy)生成測試數據
set nocount on;
declare @Qa table([PlanUID] nvarchar(1),[Quantity] int)
Insert @Qa
select N'A',2 union all
select N'A',3 union all
select N'A',1 union all
select N'B',6 union all
select N'B',1 union all
select N'C',8
2000:
Select *,(select sum([Quantity]) from @Qa where [PlanUID]=a.[PlanUID])sumQuantity
from @Qa a
2005:
Select *,sum([Quantity])over(partition by [PlanUID])sumQuantity
from @Qa a
PlanUID Quantity sumQuantity
------- ----------- -----------
A 2 6
A 3 6
A 1 6
B 6 7
B 1 7
C 8 8
--去掉b表的那一列,
SELECT a.UID,a.Quantity,--b.EligibleBatch,
( select sum(quantity) FROM TB_ProductEligible where planuid = a.UID ) as finishedamount
FROM TB_ProductPlan a
SELECT a.UID,a.Quantity,b.EligibleBatch,
( select sum(quantity) FROM TB_ProductEligible where planuid = a.UID ) as finishedamount
FROM TB_ProductPlan a
就是这种感觉,但是我不知道怎么写?
--> --> (Roy)生成測試數據
set nocount on;
declare @T table([Col1] int,[Col2] nvarchar(1),[Col3] int)
Insert @T
select 1,N'A',2 union all
select 2,N'A',3 union all
select 3,N'A',1 union all
select 4,N'B',6 union all
select 4,N'B',1 union all
select 6,N'C',8
Select
[Col1]=(select count(distinct [Col1]) from @T where [Col1]<=t1.[Col1]),
[Col2],
[Col3]=(select sum([Col3]) from @T where [Col2]=t1.[Col2] )
from
@T t1
group by [Col1],[Col2]
Col1 Col2 Col3
----------- ---- -----------
1 A 6
2 A 6
3 A 6
4 B 7
5 C 8
如:
select col1,col2 from t--
select col1,0 from t2--没有的列用0
---
横行显示:
select col1,col2,t2_col1=0 from t1
union all
select 0,0,col1 from t