SQL Server查询一些特定条件

时间:2014-01-14 09:21:17

标签: sql-server-2008

SELECT distinct t1.vssyspackageid,CA.rAmount as Amount,Curr.vsShortCode AS Currency       


 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

如果哪个包分配给承包商奖明细则,那么它将显示在一列中。

示例:GWSSP / 01,GWSSP / 02之后它显示下一列中的总包。

1 个答案:

答案 0 :(得分:0)

我不确定你想要什么,因为你的问题有点模糊,但我猜你想要一个指定Package的行,它将显示Amount列的总和。无论如何,这是我能从你的问题中理解的!

SELECT DISTINCT t1.vssyspackageid, 
                SUM(CA.rAmount) AS TotalAmount,
                Curr.vsShortCode AS Currency       
FROM            tblPrograms         
INNER JOIN      tblProgramsAndPackages ON tblPrograms.vsSysProgramId = 
                                          tblProgramsAndPackages.vsSysProgramId         
INNER JOIN      tblPackages AS 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' 
     AND        t1.[YOUR_COLUMN_NAME_HERE] = 'GWSSP/01'
GROUP BY        t1.vssyspackageid,
                Curr.vsShortCode

只需注意几点:

  1. 我不知道tblPackages表中的列名称,例如'GWSSP / 01'所属的值,所以我留给你填写。
  2. 如果您的软件包有多种货币,您仍会获得多行,如果是这种情况,则必须从Select和Group By子句中删除Curr.vsShortCode
  3. 我的代码也因为它非常混乱而难以理解,以及使用多种标准和捷径!