在一个查询中针对一个表进行多次选择

时间:2012-11-16 21:16:56

标签: sql sql-server subquery multiple-select

我正在尝试构建一个select语句,它将为每个月和用户提供一个百分比值。

我有一个表格,其中的行看起来像这样; 表名: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

select(s)应该给出每个签名花费在tArtCode DKB或DKK上花费的时间百分比以及花在所有tArtCode上的所有时间的百分比。因此,从上面的例子中,select应该给我40%的签名30JK在第8个月和100%签名30JK在第9个月。

我尝试过不同类型的sql select语句,但它似乎没有100%正确。 这就是我所拥有的;

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

这个SQL不起作用的错误是我无法分组Year,Month,SumTot。我也得到错误;当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

这下一个sql选择给我我想要的东西,但我要具体说明我要求的......可以这么说。如果我使用这个s i,那么每个签名和每个月都需要有一个sql查询,而且这不是很有创意。

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

我也尝试过Union,但这似乎对我不起作用。

我真的需要帮助!谢谢!

1 个答案:

答案 0 :(得分:1)

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