如何在SQL Server中查询

时间:2016-06-03 05:43:46

标签: sql-server

我有3张桌子如下所示

tbMaster
Branch_code Sale_No Amount  Type_Code
-------------------------------------
0           1222    421.00  9
0           1227    323.00  9
0           1228    1186.00 9
0           1223    119.00  2

tType 
Type_Code   Type_Name
1              NTB
2              TABLE SERVICE
3              A/C TABLE  SERVICE
4              TAKE AWAY
5              CAR SERVICE
6              HOME DELIVERY
7              SELF SERVICE

最后

tFree
Type_Code   Branch_code    Amount
-----------------------------------
1           0               261.00
1           0               112.20
1           0               336.60

现在我得到输出,例如

Branch_code  TypeTcount SaleValue   Type_Name
NULL            0        0.00          NTB
0               1        910.00        TABLE SERVICE
NULL            0        0.00          A/C TABLE  SERVICE
NULL            0        0.00          TAKE AWAY
NULL            0        0.00          CAR SERVICE
NULL            0        0.00          HOME DELIVERY
NULL            0        0.00          SELF SERVICE

现在我的查询是

SELECT
    sm.Branch_code,
    COUNT(sm.Sale_No) TypeTcount,
    ISNULL(SUM(sm.Amount), 0) SaleValue,
    t.Type_Name
FROM [tMaster] sm
RIGHT JOIN [tType] t
    ON sm.Type_Code = t.Type_Code
    AND sm.Branch_code = 0
GROUP BY
    t.Type_Name,
    t.Type_Code
ORDER BY t.Type_Code

但我希望从输出Type_code中的表tFree中获取TypeTCount的计数。

预期结果:

  Branch_code    TypeTcount SaleValue   Type_Name
    NULL            3        709.80        NTB
    0               1        910.00        TABLE SERVICE
    NULL            0        0.00          A/C TABLE  SERVICE
    NULL            0        0.00          TAKE AWAY
    NULL            0        0.00          CAR SERVICE
    NULL            0        0.00          HOME DELIVERY
    NULL            0        0.00          SELF SERVICE

只需显示tFree

附近的NTB表记录

2 个答案:

答案 0 :(得分:0)

在不影响查询的其余部分的情况下,您可以尝试使用correlated subquery来计算此数:

SELECT
    sm.Branch_code,
    -- Correlated subquery to count rows:
    (select count(*) from tFree where Type_Code = t.Type_Code) TypeTcount,
    ISNULL(SUM(sm.Amount), 0) SaleValue,
    t.Type_Name
FROM [tMaster] sm
RIGHT JOIN [tType] t
    ON sm.Type_Code = t.Type_Code
    AND sm.Branch_code = 0
GROUP BY
    t.Type_Name,
    t.Type_Code
ORDER BY t.Type_Code

请注意,与连接相比,这通常会导致性能损失,因为它可能会将计数作为嵌套循环运行。这是一个可能的联接:

SELECT
    sm.Branch_code,
    COUNT(t.Type_Code) TypeTcount,
    ISNULL(SUM(sm.Amount), 0) SaleValue,
    t.Type_Name
FROM [tMaster] sm
RIGHT JOIN [tType] t
    ON sm.Type_Code = t.Type_Code
    AND sm.Branch_code = 0
-- Join table to count rows:
LEFT JOIN [tFree] f
    ON t.Type_Code = f.Type_Code
GROUP BY
    t.Type_Name,
    t.Type_Code
ORDER BY t.Type_Code

最后要提到的是,我没有更正任何与Branch_Code或您的示例查询有关的问题。我还不完全信任您的示例数据,所以我将剩下的查询单独留下,虽然它看起来会引发异常。

答案 1 :(得分:0)

SELECT
    sm.Branch_code,
    COUNT(sm.Sale_No) TypeTcount,
    ISNULL(SUM(sm.Amount), 0) SaleValue,
    t.Type_Name,
    (SELECT Count(Type_code) FROM tFree  WHERE tFree.Type_Code = t.Type_Code AND tFree.Branch_code = sm.Branch_code) AS Count_of_Type_code
FROM [tMaster] sm
RIGHT JOIN [tType] t
    ON sm.Type_Code = t.Type_Code
    AND sm.Branch_code = 0
GROUP BY
    sm.Branch_code
    t.Type_Name,
    t.Type_Code
ORDER BY t.Type_Code