0
select kota ,total, totalsum from 
(
SELECT  i.[Antam_Unit] as kota ,count(p.[Id_Pks_Pk])as total FROM [SIMPKBL].[dbo].[Pks_Pk] p join [SIMPKBL].[dbo].[Par_Unit_Antam] i on  i.Id_Unit_Antam = p.Id_Unit_Antam  group by i.[Id_Unit_Antam],i.Antam_Unit
UNION ALL
SELECT  i.[Antam_Unit] as kota , count(p.[Id_Proposal_Pk])as totalsum  FROM [SIMPKBL].[dbo].[Pks_Pk] p join [SIMPKBL].[dbo].[Par_Unit_Antam] i on  i.Id_Unit_Antam = p.Id_Unit_Antam where YEAR(p.Tanggal_Cetak_Pks_Pk) = '2012'   group by i.[Id_Unit_Antam],i.Antam_Unit 
) t
group by kota,total

我想要这样的输出:

kota total totalsum
A     12      4
B     16      5
4

2 回答 2

0

由于查询非常相似,因此它们似乎可以合并为一个select

select i.[Antam_Unit] as kota,
  count(p.[Id_Pks_Pk]) as total,
  count( case when YEAR(p.Tanggal_Cetak_Pks_Pk) = '2012' then p.[Id_Proposal_Pk] else null end ) as totalsum
  FROM [SIMPKBL].[dbo].[Pks_Pk] p join [SIMPKBL].[dbo].[Par_Unit_Antam] i on  i.Id_Unit_Antam = p.Id_Unit_Antam
  group by i.[Id_Unit_Antam], i.Antam_Unit
于 2013-01-08T03:04:18.493 回答
0

如果我正确理解了您的要求,则可以使用以下简单语句来实现您正在寻找的结果:

SELECT  i.[Antam_Unit] as kota, 
count(p.[Id_Pks_Pk]) as total,
SUM(CASE WHEN YEAR(p.Tanggal_Cetak_Pks_Pk) = '2012' AND p.[Id_Proposal_Pk] IS NOT NULL THEN 1 ELSE 0 END) as totalsum  
FROM [SIMPKBL].[dbo].[Pks_Pk] p 
 join [SIMPKBL].[dbo].[Par_Unit_Antam] i 
 on  i.Id_Unit_Antam = p.Id_Unit_Antam 
group by i.[Id_Unit_Antam],i.Antam_Unit 
于 2013-01-08T03:04:36.480 回答