2

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

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

选择应该给我每个签名在 tArtCode DKB 或 DKK 上花费的时间百分比以及在所有 tArtCode 上花费的所有时间的月份。所以从上面的例子中,选择应该在第 8 个月给我 40% 的签名 30JK 和 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 不起作用的错误是我无法对年、月、总和。我也得到错误;当不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式。

下一个 sql select 给了我我想要的东西,但我要具体说明我的要求......可以这么说。如果我使用这个 sql,那么我需要对每个签名和每个月都有一个 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,但这似乎对我不起作用。

我真的需要帮助!谢谢!

4

1 回答 1

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
于 2012-11-16T21:45:08.413 回答