查询显示比#temp表

时间:2016-03-02 18:40:55

标签: sql-server stored-procedures sql-server-2000

所以让我们说#temp有4行,输出为3(应为4),如果有1,则输出为0。

我不太清楚发生了什么事。想知道是否有人可以通过查看查询来判断。

SELECT TH.TnnNumber,
    CASE WHEN COUNT(DISTINCT TL.DiscountCodeID) > 1 THEN 'Varies, View Tnn' ELSE CAST(MAX(DC.Value) AS NVARCHAR(50)) END AS Discount,
    CASE WHEN TS.SpinID > 4 THEN 'Has Specifics, View Tnn' ELSE TS.Value END AS Spin,
    CASE WHEN COUNT(DISTINCT TL.Commission_HMM) > 1 THEN 'Varies, View Tnn' ELSE CAST(MAX(ISNULL(str(TL.Commission_HMM, 12), 'Default Comm')) AS NVARCHAR(50)) END AS Commission_HMM,
    CASE WHEN COUNT(DISTINCT TL.Commission) > 1 THEN 'Varies, View Tnn' ELSE CAST(MAX(ISNULL(str(TL.Commission, 12), 'Default Comm')) AS NVARCHAR(50)) END AS Commission,
    TL.TnnID
FROM [DBSERV].Tnn.DBO.Tnn_Header AS TH
LEFT JOIN [DBSERV].Tnn.DBO.Tnn_LINE AS TL
    ON TH.TnnID = TL.TnnID
LEFT JOIN [DBSERV].Tnn.DBO.Tnn_Spin AS TS
    ON TH.SpinID = TS.SpinID
LEFT JOIN [DBSERV].Tnn.DBO.Tnn_DiscountCode AS DC
    ON TL.DiscountCodeID = DC.DiscountCodeID
INNER JOIN #temp AS T
    ON T.Tnn = TH.TnnNumber
GROUP BY TH.TnnNumber,
    TS.SpinID,
    TS.Value,
    TL.TnnID

必需的输出:

+-----------+----------+-------------------------+----------------+--------------+-------+
| TnnNumber | Discount |          Spin           | Commission_HMM |  Commission  | TnnID |
+-----------+----------+-------------------------+----------------+--------------+-------+
|    902054 | 50-20-9  | Has Specifics, View Tnn | Default Comm   | Default Comm |  5855 |
|    907616 | 50-20-20 | Half                    | Default Comm   |            2 |  6111 |
|    910019 | 50-20-9  | Half                    | Default Comm   | Default Comm |  7015 |
|    915919 | 50-20-9  | Half                    | Default Comm   | Default Comm |  7015 |
+-----------+----------+-------------------------+----------------+--------------+-------+

实际输出:

+-----------+----------+-------------------------+----------------+--------------+-------+
| TnnNumber | Discount |          Spin           | Commission_HMM |  Commission  | TnnID |
+-----------+----------+-------------------------+----------------+--------------+-------+
|    902054 | 50-20-9  | Has Specifics, View Tnn | Default Comm   | Default Comm |  5855 |
|    907616 | 50-20-20 | Half                    | Default Comm   |            2 |  6111 |
|    910019 | 50-20-9  | Half                    | Default Comm   | Default Comm |  7015 |
+-----------+----------+-------------------------+----------------+--------------+-------+

#temp

中的值
Tnn
902054
907616
910019
915919

右连接:

+-----------+----------+-------------------------+----------------+--------------+-------+
| TnnNumber | Discount |          Spin           | Commission_HMM |  Commission  | TnnID |
+-----------+----------+-------------------------+----------------+--------------+-------+
| NULL      | NULL     | NULL                    | Default Comm   | Default Comm | NULL  |
| 902054    | 50-20-9  | Has Specifics, View Tnn | Default Comm   | Default Comm | 5855  |
| 907616    | 50-20-20 | Half                    | Default Comm   |            2 | 6111  |
| 910019    | 50-20-9  | Half                    | Default Comm   | Default Comm | 7015  |
+-----------+----------+-------------------------+----------------+--------------+-------+

1 个答案:

答案 0 :(得分:2)

当您的RIGHT JOIN查询显示

[DBSERV].Tnn.DBO.Tnn_Header AS TH

没有ID = 915919行,这就是RIGHT JOIN返回NULLINNER JOIN返回的行

SELECT *
FROM [DBSERV].Tnn.DBO.Tnn_Header AS TH
WHERE TH.TnnNumber = 915919

现在您必须检查如何创建#temp并验证您使用与ID相关的Tnn_Header