具体分为2列

时间:2014-08-12 09:14:09

标签: sql sql-server group-by

这是我的疑问:

SELECT 
    SUM(TranAmount * -1) [RecVal],
    ISNULL(SC.CountryName,
        (SELECT tblSysCountry.CountryName 
         FROM tblIdcBranchMaster 
         INNER JOIN tblSysCountry ON tblIDCBranchMaster.countryID = tblSysCountry.CountryId 
         WHERE tblIDCBranchMaster.idcBranchID = TM.idcBranchID)
    ) [Country]
FROM 
    tblDebtorTransactionMaster TM
LEFT JOIN 
    tblIDCEmployeeMaster EM ON EM.employeeID = TM.employeeWorkedBy
LEFT JOIN 
    tblIDCBranchMaster BM ON EM.homeBranch = BM.idcBranchCode
LEFT JOIN 
    tblSysCountry SC ON SC.CountryId = BM.countryID
WHERE 
    TM.tranTypeID IN (2, 6, 7, 18, 9, 11, 51)
    AND TM.documentDate BETWEEN dbo.default_date_format('**INPUT DATE**') 
    AND dbo.default_date_format('**INPUT DATE**')
GROUP BY 
    SC.CountryName,TM.idcBranchID

出于安全目的删除了日期

问题是我希望能够通过第二列完全分组,而不仅仅是按国家名称。

目前,我将国家/地区链接到与处理交易的员工相关联的分支机构,但如果交易是自动处理的,那么我需要检查帐户链接到哪个分支(每个交易链接到分支)。

如果来自其他分支的员工捕获该交易,则员工从事务链接到的分支链接到 CAN DIFFER 的分支。

当我运行上面的查询时,我的结果如下:我需要最终看到每个国家收集了多少钱(不是分支机构)

RecVal      +   Country
80090.42    +   SOUTHAFRICA
181248.10   +   SOUTHAFRICA
140989.16   +   SOUTHAFRICA
21854.01    +   SOUTHAFRICA
53037.25    +   SOUTHAFRICA
84241.62    +   SOUTHAFRICA
49598.07    +   SOUTHAFRICA
200.00      +   SOUTHAFRICA
75559.36    +   SOUTHAFRICA
12.91       +   SOUTHAFRICA
35784.91    +   SOUTHAFRICA
28627.04    +   SOUTHAFRICA
500.00      +   SOUTHAFRICA
22688.04    +   SOUTHAFRICA
38443.02    +   SOUTHAFRICA
34313.16    +   SOUTHAFRICA
62419.27    +   SOUTHAFRICA
50.00       +   SOUTHAFRICA
73598.95    +   SOUTHAFRICA
86741.78    +   SOUTHAFRICA
21553.98    +   SOUTHAFRICA
12.40       +   SOUTHAFRICA
86673.27    +   SOUTHAFRICA
48021.07    +   SOUTHAFRICA
22586.55    +   SOUTHAFRICA
11027.50    +   SOUTHAFRICA
14897.55    +   SOUTHAFRICA
49672.86    +   SOUTHAFRICA
14910.98    +   SOUTHAFRICA
6152.10     +   SOUTHAFRICA
91878.14    +   SOUTHAFRICA
47132.15    +   SOUTHAFRICA
24957.49    +   SOUTHAFRICA
93.25       +   SOUTHAFRICA
54657.67    +   SOUTHAFRICA
71687.30    +   SWAZILAND
740.00      +   SOUTHAFRICA
607419.67   +   NAMIBIA
150.00      +   SOUTHAFRICA
160.49      +   SOUTHAFRICA
522544.70   +   SOUTHAFRICA
300.00      +   SOUTHAFRICA
13207.29    +   SOUTHAFRICA
47090.91    +   SOUTHAFRICA
51591.77    +   SOUTHAFRICA
600.00      +   SOUTHAFRICA
45605.71    +   SOUTHAFRICA
5642.54     +   SOUTHAFRICA
1470.00     +   NAMIBIA 
--------------------------------

1 个答案:

答案 0 :(得分:1)

您可以将代码包装在CTE(公用表表达式)中。你需要先用前面开始;如果你有预先存在的sql代码。我看到你有很多南非交易。 Saffa在这里。

;WITH Base AS (SELECT SUM(TranAmount * -1) [RecVal],
    ISNULL(SC.CountryName,
    (
        SELECT tblSysCountry.CountryName 
        FROM tblIdcBranchMaster 
            INNER JOIN tblSysCountry ON tblIDCBranchMaster.countryID =  tblSysCountry.CountryId 
        WHERE tblIDCBranchMaster.idcBranchID = TM.idcBranchID
    )
) [Country]
FROM tblDebtorTransactionMaster TM
LEFT JOIN tblIDCEmployeeMaster EM ON EM.employeeID = TM.employeeWorkedBy
LEFT JOIN tblIDCBranchMaster BM ON EM.homeBranch = BM.idcBranchCode
LEFT JOIN tblSysCountry SC ON SC.CountryId = BM.countryID
WHERE TM.tranTypeID IN (2, 6, 7, 18, 9, 11, 51)
AND TM.documentDate BETWEEN dbo.default_date_format('**INPUT DATE**') 
AND dbo.default_date_format('**INPUT DATE**')
GROUP BY SC.CountryName,TM.idcBranchID
) 

SELECT Sum(RecVal) as SumRecVal,Country FROM Base GROUP BY Country