34,873
社区成员
发帖
与我相关
我的任务
分享Select
c.bh,
c.[qm],
max(a.jsr) AS jsr,
a.type,
a.status,
sum(b.sl) AS sl,
CAST(sum(b.je)*1.0/case when sum(b.sl)=0 then 1 else sum(b.sl) end AS DECIMAL(18,2)) AS dj,
sum(b.je) as je
from (#A AS a
INNER JOIN #B AS b ON a.id=b.djbh)
right JOIN #C AS c ON c.bh=b.bh
GROUP BY a.type,a.status,c.bh,c.[qm]
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([id] int,[jsr] nvarchar(3),[type] int,[status] int)
Insert #A
select 313,N'001',1,0 union all
select 314,N'002',1,0 union all
select 315,N'001',1,0
Go
--> -->
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([id] int,[djbh] int,[bh] nvarchar(10),[dj] int,[sl] int,[je] DECIMAL(18,2))
Insert #B
select 1,313,N'0000000001',5,1,5 union all
select 2,313,N'0000000002',5,1,5 union all
select 3,314,N'0000000001',4,1,4.5 union all
select 4,315,N'0000000001',3,1,3
Go
if not object_id(N'Tempdb..#C') is null
drop table #C
Go
Create table #C([bh] nvarchar(10),[qm] nvarchar(2))
Insert #C
select N'0000000001',N'花生' union all
select N'0000000002',N'牛奶'
go
Select
c.bh,
c.[qm],
max(a.jsr) AS jsr,
a.type,
a.status,
sum(b.sl) AS sl,
CAST(sum(b.je)*1.0/sum(b.sl) AS DECIMAL(18,2)) AS dj,
sum(b.je) as je
from (#A AS a
INNER JOIN #B AS b ON a.id=b.djbh)
right JOIN #C AS c ON c.bh=b.bh
GROUP BY a.type,a.status,c.bh,c.[qm]
/*
bh qm jsr type status sl dj je
0000000001 花生 002 1 0 3 4.17 12.50
0000000002 牛奶 001 1 0 1 5.00 5.00
*/
Select
c.bh,
c.[qm],
max(a.jsr) AS jsr,
a.type,
a.status,
sum(b.sl) AS sl,
AVG(b.dj) AS dj,
sum(b.je) as je
from (#A AS a
INNER JOIN #B AS b ON a.id=b.djbh)
right JOIN #C AS c ON c.bh=b.bh
GROUP BY a.type,a.status,c.bh,c.[qm]
/*
bh qm jsr type status sl dj je
0000000001 花生 002 1 0 3 4 12
0000000002 牛奶 001 1 0 1 5 5
*/Select
c.bh,
c.[qm],
max(a.jsr) AS jsr,
a.type,
a.status,
sum(b.sl) AS sl,
sum(b.je) as je
from (#A AS a
INNER JOIN #B AS b ON a.id=b.djbh)
right JOIN #C AS c ON c.bh=b.bh
GROUP BY a.type,a.status,c.bh,c.[qm]use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([id] int,[jsr] nvarchar(3),[type] int,[status] int)
Insert #A
select 313,N'001',1,0 union all
select 314,N'002',1,0 union all
select 315,N'001',1,0
Go
--> -->
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([id] int,[djbh] int,[bh] nvarchar(10),[dj] int,[sl] int,[je] int)
Insert #B
select 1,313,N'0000000001',5,1,5 union all
select 2,313,N'0000000002',5,1,5 union all
select 3,314,N'0000000001',4,1,4 union all
select 4,315,N'0000000001',3,1,3
Go
if not object_id(N'Tempdb..#C') is null
drop table #C
Go
Create table #C([bh] nvarchar(10),[qm] nvarchar(2))
Insert #C
select N'0000000001',N'花生' union all
select N'0000000002',N'牛奶'
Go
Select
c.bh,
c.[qm],
max(a.jsr) AS jsr,
a.type,
a.status,
sum(b.sl) AS sl,
sum(b.je) as je
from (#A AS a
INNER JOIN #B AS b ON a.id=b.djbh)
right JOIN #C AS c ON c.bh=b.bh
GROUP BY a.type,a.status,c.bh,c.[qm]
/*
bh qm jsr type status sl je
0000000001 花生 002 1 0 3 12
0000000002 牛奶 001 1 0 1 5
*/Select
c.bh,
c.[qm],
a.jsr,
a.type,
a.status,
sum(b.sl) AS sl,
sum(b.je) as je
from (#A AS a
INNER JOIN #B AS b ON a.id=b.djbh)
right JOIN #C AS c ON c.bh=b.bh
GROUP BY a.jsr,a.type,a.status,c.bh,c.[qm]

use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([id] int,[jsr] nvarchar(3),[type] int,[status] int)
Insert #A
select 313,N'001',1,0 union all
select 314,N'002',1,0 union all
select 315,N'001',1,0
Go
--> -->
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([id] int,[djbh] int,[bh] nvarchar(10),[dj] int,[sl] int,[je] int)
Insert #B
select 1,313,N'0000000001',5,1,5 union all
select 2,313,N'0000000002',5,1,5 union all
select 3,314,N'0000000001',4,1,4 union all
select 4,315,N'0000000001',3,1,3
Go
if not object_id(N'Tempdb..#C') is null
drop table #C
Go
Create table #C([bh] nvarchar(10),[qm] nvarchar(2))
Insert #C
select N'0000000001',N'花生' union all
select N'0000000002',N'牛奶'
Go
Select a.jsr,a.type,a.status,b.bh,c.[qm],sum(b.sl) AS sl,sum(b.je) as je
from #A AS a
INNER JOIN #B AS b ON a.id=b.djbh
INNER JOIN #C AS c ON c.bh=b.bh
GROUP BY a.jsr,a.type,a.status,b.bh,c.[qm]
/*
jsr type status bh qm sl je
001 1 0 0000000001 花生 2 8
001 1 0 0000000002 牛奶 1 5
002 1 0 0000000001 花生 1 4
*/