结合ASP.NET中的行列值

时间:2014-07-01 04:52:58

标签: sql-server stored-procedures

SELECT name, 
       batchno, 
       recievedeggs, 
       settingqnty, 
       CONVERT(VARCHAR(50), settingdate, 103)     AS settingdate, 
       setteroutput, 
       ( 100 * setteroutput / settingqnty )       AS [SetterHatch%], 
       hathersettingqty, 
       hatcheroutput, 
       culls, 
       [hatcherhatch%], 
       ( 100 * hatcheroutput / Sum(settingqnty) ) AS [Hatch%], 
       CONVERT(VARCHAR(50), pulloutdate, 103)     AS pulloutdate, 
       hatcher 
FROM  (SELECT SH.name, 
              MS.batchno, 
              MS.recievedeggs, 
              MS.quantity               AS SettingQnty, 
              MS.settingdate, 
              SD.remainingqnty          AS SetterOutput, 
              MH.settingqntity          AS HatherSettingQty, 
              MH.saleablechicks         AS HatcherOutput, 
              MH.culls, 
              Round(MH.hatchpercent, 2) AS [HatcherHatch%], 
              MH.pulloutdate, 
              SH1.name                  AS Hatcher 
       FROM   k_hm_settergetterallocationdet MS 
              INNER JOIN k_hm_setterdetails SD 
                      ON MS.sno = SD.id 
              INNER JOIN k_hm_hatcherdetails HD 
                      ON SD.sno = HD.id 
              INNER JOIN k_hm_masterhatcherdet MH 
                      ON HD.sno = MH.id 
              INNER JOIN k_hm_gettersetterdet SH 
                      ON MS.name = SH.sno 
              INNER JOIN k_hm_gettersetterdet SH1 
                      ON HD.hatchername = SH1.sno 
       WHERE  settingdate BETWEEN @fromdate AND @todate)a 
GROUP  BY a.settingdate, 
          a.name, 
          a.recievedeggs, 
          a.settingdate, 
          a.setteroutput, 
          a.hathersettingqty, 
          a.hatcheroutput 
ORDER  BY a.settingdate DESC 

使用此输出如下:

 S.No. SetterName SettingDate FlockNo Rec.Eggs SettingEggs SetterO/P Setter% HatcherName HatcherQnty PulloutDate HatcherO/P Culls Hatcher% Total Hatch% 

1 Setter1 01/06/2014 Batch10 2500 2150 2136 99 Hatcher1 2136 22/06/2014 2115 15 99.02 98 
2 Setter1 01/06/2014 Batch10 2500 2355 2341 99 Hatcher1 2341 22/06/2014 2314 21 98.85 98 
3 Setter2 01/06/2014 Batch10 2450 2255 2241 99 Hatcher1 2241 22/06/2014 2221 20 99.11 98 

但我想要这样的输出:

 S.No. SetterName SettingDate FlockNo Rec.Eggs SettingEggs SetterO/P Setter% HatcherName HatcherQnty PulloutDate HatcherO/P Culls Hatcher% Total Hatch%

1 Setter1,Setter3 01/06/2014 Batch10 7450 6760 6781 99 Hatcher1 6781 22/06/2014 6650 15 99.02 98 

我试过这种方式,但不正确。

1 个答案:

答案 0 :(得分:0)

DECLARE @t VARCHAR(Max)

Select @t = ISNULL(@t + ',' + SetterName, SetterName) from hk
where FlockNo in (select FlockNo from hk)

(select @t as setterName,SettingDate,FlockNo,sum(rec) as rec,SUM(SettingEggs)as se,sum([SetterO/P])assop,([Setter%])asse,HatcherName, 
Sum(HatcherQnty)HQ,PulloutDate,sum([HatcherO/P]) as hatchero,min(Culls)as culls,max([Hatcher%])as hat,
(Total)
from hk
group by FlockNo,[Setter%],SettingDate,HatcherName,PulloutDate,Total,FlockNo)

得到结果 Setter1,Setter2,Setter3 2014-01-06 Batch10 7450 6760 6718 99 Hatcher1 6718 2014-06-22 6650 15 99 98