查询#1:
SELECT a.*
INTO #TempTable1
FROM
(SELECT
Vendor,
CASE WHEN CONVERT(varchar(50), DateModified, 101)='01/01/1900' THEN '' ELSE CONVERT(varchar(50), DateModified, 101) END AS 'Date of Last Check',
CASE WHEN CONVERT(varchar(50), PaycommissionDate, 101)='01/01/1900' THEN '' ELSE CONVERT(varchar(50), PaycommissionDate, 101) END AS 'Date of check for month',
SUM([Original $ Total]) 'Amount'
FROM
Tbl_Commission_Reconcilation
WHERE
Vendor IS NOT NULL
AND MONTH([Order Date]) = MONTH (GETDATE())
GROUP BY
Tbl_Commission_Reconcilation.Vendor,
CONVERT(varchar(50), DateModified, 101),
[Sales Rep], PaycommissionDate) a
输出:
Vendor Date of Last Check Date of check for month Amount
-----------------------------------------------------------------------------
Boston Warehouse 12/12/2017 12/12/2017 919.00
Woodlink 12/12/2017 12/12/2017 979.86
查询#2:
SELECT b.*
INTO #TempTable2
FROM
(SELECT
[Sales Rep],
SUM([Commission $ paid]) 'Commission $ paid'
FROM
Tbl_Commission_Reconcilation
WHERE
Vendor IS NOT NULL
AND MONTH([Order Date]) = MONTH (GETDATE())
GROUP BY
[Sales Rep]) b
输出2:
Sales Rep Commission $ paid
---------------------------------
Tammy Hanson 379.77
最终查询:
select *
from #TempTable1, #TempTable2
最终输出:
Vendor DateofLastCheck Dateofcheckformonth Amount Sales Rep Commpaid
Boston
Warehouse 12/12/2017 12/12/2017 919.00 Tammy Hanson 379.77
Woodlink 12/12/2017 12/12/2017 979.86 Tammy Hanson 379.77
输出2显示只有一个佣金为Tammy Hanson支付379.77。但在最终输出显示2次佣金支付379.77。如何纠正这个问题? 如何只显示最终输出中支付的佣金?请任何人帮助我。
答案 0 :(得分:2)
您将获得重复值,因为您在此处使用交叉连接(笛卡尔积)。请指定至少1个条件以匹配两个表中的记录,以便仅显示所需记录的值。
根据您当前的查询,它将返回两个表中所有可能的行组合。像这样改变它
select * from #TempTable1 T1,#TempTable2 T2 WHERE T1.ColumnName = T2.ColumnName
或者你也可以说
select * from #TempTable1 T1
inner join #TempTable2 T2
WHERE T1.ColumnName = T2.ColumnName
- 如果您想要在两个表上都有匹配的记录
或
select * from #TempTable1 T1
left join #TempTable2 T2
WHERE T1.ColumnName = T2.ColumnName
- 如果你想要来自#TempTable1的记录,如果没有匹配的记录则显示为TempTable2的空白
答案 1 :(得分:2)
看来你可以只做一个查询。较大的查询已经在group by子句中有[sales rep],那么为什么不只是将佣金加起来?
SELECT
a.* INTO #TempTable1
FROM (
SELECT
Vendor
, CASE WHEN CONVERT(varchar(50), DateModified, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar(50), DateModified, 101) END
AS 'Date of Last Check'
, CASE WHEN CONVERT(varchar(50), PaycommissionDate, 101) = '01/01/1900' THEN '' ELSE CONVERT(varchar(50), PaycommissionDate, 101) END
AS 'Date of check for month'
, SUM([Original $ Total]) 'Amount'
, [Sales Rep]
, SUM([Commission $ paid]) 'Commission $ paid'
FROM Tbl_Commission_Reconcilation
WHERE Vendor IS NOT NULL
AND MONTH([Order Date]) = MONTH(GETDATE())
GROUP BY
Tbl_Commission_Reconcilation.Vendor
, CONVERT(varchar(50), DateModified, 101)
, [Sales Rep]
, PaycommissionDate
) a
答案 2 :(得分:1)
您还应该在查询中添加row_number
表1
SELECT a.*
INTO #TempTable1
FROM
(SELECT
Vendor,
CASE WHEN CONVERT(varchar(50), DateModified, 101)='01/01/1900' THEN '' ELSE CONVERT(varchar(50), DateModified, 101) END AS 'Date of Last Check',
CASE WHEN CONVERT(varchar(50), PaycommissionDate, 101)='01/01/1900' THEN '' ELSE CONVERT(varchar(50), PaycommissionDate, 101) END AS 'Date of check for month',
SUM([Original $ Total]) 'Amount',
ROW_NUMBER() OVER(ORDER BY Vendor, DateModified, [Sales Rep]) RN
FROM
Tbl_Commission_Reconcilation
WHERE
Vendor IS NOT NULL
AND MONTH([Order Date]) = MONTH (GETDATE())
GROUP BY
Tbl_Commission_Reconcilation.Vendor,
CONVERT(varchar(50), DateModified, 101),
[Sales Rep], PaycommissionDate) a
表2
SELECT b.* INTO #TempTable2
from
(SELECT [Sales Rep],SUM([Commission $ paid]) 'Commission $ paid',
ROW_NUMBER() OVER(ORDER BY [Sales Rep]) RN
from Tbl_Commission_Reconcilation where Vendor is not null and Month([Order Date])= MONTH (Getdate())
group by [Sales Rep])b
并且还使用此RN列连接table1和table2
select
*
from #TempTable1 t1
LEFT JOIN #TempTable2 t2
ON t1.RN = t2.RN