2013-04-26 36 views
0

我正在運行第一個查詢以獲取總計,然後將這些總計存儲在名爲@total和@ots的兩個變量中。我希望稍後在另一個查詢中使用這些變量來計算一些百分比值,但是您可以在圖片中看到我在結果集中以零結尾。無法使用變量中的值計算總和?

create table #temp (
count int, 
ots int 
) 

insert into #temp 
select 
count(i.ID) as 'total count', 
sum(mc.ots) as 'Imps' 

from 
Profiles P 
INNER JOIN ProfileResults PR ON P.ID = PR.ProfileID 
INNER JOIN Items i ON PR.ItemID = I.ID 
inner join batches b on b.ID = i.BatchID 
left outer join BatchActionHistory bah on b.ID=bah.batchid 
inner join ItemOrganisations ito (nolock) on i.ID= ito.ItemID 
inner join Lookup_MediaChannels mc on mc.id = i.MediaChannelID 

where p.ID = 41 
and b.StatusID IN (6,7) 
and bah.BatchActionID = 6 
and i.StatusID = 2 
and i.IsRelevant = 1 


declare @total int 
declare @ots int 

select @total = sum(count) from #temp 
select @ots = sum(ots) from #temp 

select c.Name, 
count(i.ID) as 'total count', 
sum(mc.ots) as 'Imps', 
sum(case when ito.rating <50 then 1 else 0 end) as 'unfav count', 
sum(case when ito.Rating =50 then 1 else 0 end) as 'neu count', 
sum(case when ito.Rating >50 then 1 else 0 end) as 'fav count', 

(sum(case when ito.rating < 50 then 1.0 else 0.0 end)/count(i.ID) * 100) as 'unfav %', 
(sum(case when ito.Rating =50 then 1.0 else 0.0 end)/count(i.ID) * 100) as 'neu %', 
(sum(case when ito.Rating >50 then 1.0 else 0.0 end)/count(i.ID) * 100) as 'fav %', 

CONVERT(decimal(4,2),avg(ito.Rating)) as 'Av Rating %', 

---------------------------------------------------------------------------- 
--problem encountered here 
    CONVERT(decimal(4,2),(count(i.ID)/ @total * 100)) as '% Total ', 
    CONVERT(decimal(4,2),(sum(mc.ots)/ @ots * 100)) as '% Imps' 
--------------------------------------------------------------------------- 


from 
Profiles P 
INNER JOIN ProfileResults PR ON P.ID = PR.ProfileID 
INNER JOIN Items i ON PR.ItemID = I.ID 
inner join batches b on b.ID = i.BatchID 
left outer join BatchActionHistory bah on b.ID=bah.batchid 
inner join Lookup_Countries c (nolock)on b.CountryID = c.ID 
inner join ItemOrganisations ito (nolock) on i.ID= ito.ItemID 
inner join Lookup_ItemStatus lis (nolock) on lis.ID = i.StatusID 
inner join Lookup_BatchStatus lbs (nolock) on lbs.ID = b.StatusID 
inner join Lookup_BatchTypes bt on bt.id = b.Typeid 
inner join Lookup_MediaChannels mc on mc.id = i.MediaChannelID 

where p.ID = 41 
and b.StatusID IN (6,7) 
and bah.BatchActionID = 6 
and i.StatusID = 2 
and i.IsRelevant = 1 

Group By c.Name 

enter image description here

+0

是否使用SQL服務器2012?如果是這樣,您可以使用count(i.ID)over(by c.Name分區)/ count(i.ID)over()或沿着這些行的某些內容,而不是運行相同的查詢兩次。 – 2013-04-26 12:12:48

+0

@DavidSöderlund:自「SQL Server 2005」以來,您可以做到這一點。 – Quassnoi 2013-04-26 12:13:54

+0

可以請你提供,例如 – Xerxes 2013-04-26 12:15:15

回答

3

用整數,SQL Server只做整數除法。這將返回0:

DECLARE @sum INT = 99 
DECLARE @total INT = 100 

SELECT CONVERT(decimal(4,2),(@sum/@total * 100)) 

你應該先轉換爲浮動/定點類型則分爲:

DECLARE @sum INT = 99 
DECLARE @total INT = 100 

SELECT CONVERT(decimal(4,2),(@sum * 100.00/@total)) 

您還可以使用窗口功能:

SELECT SUM(ms_otc) * 100.00/SUM(SUM(ms_otc)) OVER() AS [% Imps], 
     ... 
FROM Profiles P 
     ... 
GROUP BY 
     c.name 
+0

感謝它很好 – Xerxes 2013-04-26 12:31:08