具有某些特定条件的sql server查询

时间:2014-01-14 10:08:58

标签: sql-server-2008

根据我的查询,所有包信息都显示在一列中,并带有逗号分隔符。 我希望如果该列有五个包,那么它将显示在下一列中。 意味着包裹将被计算。(包裹数量) 我想显示所有包的数量(例如,如果package1有100个rs,而包2有200个rs,那么另一个列将显示300个ruppes),

列是:SUM(CA.rAmount) AS TotalAmount我可以添加的方式。

DECLARE @result VARCHAR(max) 

SET @result = '' 

SELECT @result = @result + t1.vssyspackageid + ',' 
FROM   tblprograms 
       INNER JOIN tblprogramsandpackages 
               ON tblprograms.vssysprogramid = 
                  tblprogramsandpackages.vssysprogramid 
       INNER JOIN tblpackages t1 
               ON tblprogramsandpackages.ipackageid = t1.ipackageid 
       RIGHT JOIN tblpkgcontractawarddetails AS CA 
               ON CA.ipackageid = t1.ipackageid 
       JOIN tblcurrencies AS Curr 
         ON CA.icurrency = Curr.icurrencyid 
WHERE  tblprograms.vssysprogramid = 'JICA' 
GROUP  BY t1.vssyspackageid, 
          CA.ramount, 
          Curr.vsshortcode 

IF @result <> '' 
  SET @result = LEFT(@result, Len(@result) - 1) 

PRINT @result 

0 个答案:

没有答案