所以让我们说#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 |
+-----------+----------+-------------------------+----------------+--------------+-------+
答案 0 :(得分:2)
当您的RIGHT JOIN
查询显示
[DBSERV].Tnn.DBO.Tnn_Header AS TH
没有ID = 915919
行,这就是RIGHT JOIN
返回NULL
而INNER JOIN
返回的行
试
SELECT *
FROM [DBSERV].Tnn.DBO.Tnn_Header AS TH
WHERE TH.TnnNumber = 915919
现在您必须检查如何创建#temp
并验证您使用与ID
相关的Tnn_Header