Question

I'm trying to construct a select statement that will give me a percentage value for each month and user.

I have a table with rows that looks like this; Table name: pytrans

tTransDate  tArtCode  tTime  tSignature
20120801    DKK       1      30JK
20120801    AD        1.5    30JK
20120802    DKB       3      40AK
20120903    MI        2      45TR
20120904    DKK       2      30JK
20120904    DKB       2      30JK

The select(s) should give me the percentage of how much time spent on tArtCode DKB or DKK for each signature and month of all time spent on all tArtCode. So from the above example the select should give me 40% for signature 30JK in month 8 and 100% for signature 30JK in month 9.

I have tried different kind of sql select statements but It doesn’t seem to get 100% right. This is what I’ve got;

select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignatur,
   (select SUM(tAntal) AS SumPart from pytrans where tSignatur = '30JK' AND (tArtikelkod = 'DKK' OR tArtikelkod = 'DKB'))
    /(select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, SUM(tAntal) AS SumTot from pytrans where tSignatur = '30JK' GROUP BY Year, Month, SumTot) * 100
From pytrans where tSignatur = '30JK' GROUP BY DATEPART(mm, tTransdatum), DATEPART(YY, tTransdatum), tSignatur

This SQL don’t work the error is that I can’t group Year, Month, SumTot. Also i get error; Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

This next sql select give me what i want but I’m to specific what I’m asking for... so to speak. If I use this sql i then need to have a sql query for every signature and every month, and that’s not really creative.

select TOP 1 DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignatur, 
SumProc = (select SUM(tAntal) from pytrans
          where tSignatur = '30JK' AND (tArtikelkod NOT LIKE 'FL' AND tArtikelkod NOT LIKE 'SEM' AND tArtikelkod NOT LIKE 'KOMPIN') AND (tArtikelkod = 'DKK' OR tArtikelkod = 'DKB') AND tTransdatum BETWEEN '20121101' AND '20121131')
          / (select SUM(tAntal) from pytrans 
          where tSignatur = '30JK' AND (tArtikelkod NOT LIKE 'FL' AND tArtikelkod NOT LIKE 'SEM' AND tArtikelkod NOT LIKE 'KOMPIN') AND tTransdatum BETWEEN '20121101' AND '20121131') * 100
from pytrans
Where tSignatur = '30JK' AND tTransdatum BETWEEN '20121101' AND '20121131'
group by tTransdatum, tSignatur

I’ve also tried Union but that doesn’t seem to work for me.

I really need help with this! Thanks!

Was it helpful?

Solution

Create Table #pytrans(tTransdatum datetime,  tArtCode varchar(10), tAntal float,  tSignature varchar(10))

insert into #pytrans Values('20120801',    'DKK',       1      ,'30JK')
insert into #pytrans Values('20120801',    'AD',        1.5    ,'30JK')
insert into #pytrans Values('20120802',    'DKB',       3      ,'40AK')
insert into #pytrans Values('20120903',    'MI',        2      ,'45TR')
insert into #pytrans Values('20120904',    'DKK',       2      ,'30JK')
insert into #pytrans Values('20120904',    'DKB',       2      ,'30JK')

Select a.Year,a.Month,a.tSignature,a.SumPart as aAll,b.SumPart as bAll,Case when b.SumPart =0 then NULL else a.SumPart/b.SumPart * 100 end as [Percent]
 from
(select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignature
,SUM(tAntal) AS SumPart
from #pytrans
where tSignature = '30JK' AND tArtCode in( 'DKK' ,'DKB')
Group by DATEPART(YY, tTransdatum) , DATEPART(mm, tTransdatum) , tSignature) a
Left Join
(select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignature
,SUM(tAntal) AS SumPart
from #pytrans
where tSignature = '30JK' 
Group by DATEPART(YY, tTransdatum) , DATEPART(mm, tTransdatum) , tSignature) b
on a.Month=b.Month and a.Year =b.Year and a.tSignature = b.tSignature
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top