像这样调用UDF:
SELECT
product_name,
SUM(quantity) AS SumQty,
SUM(face_value) AS SumFaceValue,
SUM(net_cost)AS SumNetCost,
SUM(face_value - net_cost) AS SumScripRebate,
organization_name
FROM getSalesSummary(@GLSCOrgId, @BeginDate, @EndDate) getSalesSummary
GROUP BY product_name, organization_name
ORDER BY product_name
产量:
"Chili's 1 25.00 22.75 2.25 Sample Organization 1
CVS/pharmacy 1 25.00 23.50 1.50 Sample Organization 1
Macy's 1 100.00 90.00 10.00 Sample Organization 1"
使用UDF逻辑并使用SELECT测试结果:
SELECT
product_name,
SUM(quantity) AS SumQty,
SUM(face_value) AS SumFaceValue,
SUM(net_cost) AS SumNetCost,
SUM(face_value - net_cost) AS SumScripRebate,
organization_name
FROM @ReturnTable
GROUP BY product_name, organization_name
ORDER BY product_name
产量:
"Chili's 4 100.00 91.00 9.00 Sample Organization 1
CVS/pharmacy 1 25.00 23.50 1.50 Sample Organization 1
Macy's 1 100.00 90.00 10.00 Sample Organization 1"
@ReturnTable是UDF返回的表,其创建方式如下:
INSERT INTO @ReturnTable(product_name,
unit_price,
quantity,
face_value,
net_cost,
organization_name)
(select * from @TablePartial UNION select * from @TableClosed)
使用SELECT和变量的测试返回正确的数据,但调用UDF并没有获得其他3个Chili的记录。我使用相同的数据参数。我对UDF很新,我不确定为什么它会返回与SELECT不同的数据。有任何建议和/或答案吗?
答案 0 :(得分:1)
您可能需要UNION ALL
而不是UNION
查看两个结果集,它加起来好像4个Chilli的行都是一样的。
Chili's 1 25.00 22.75 2.25 Sample Organization 1
Chili's 1 25.00 22.75 2.25 Sample Organization 1
Chili's 1 25.00 22.75 2.25 Sample Organization 1
Chili's 1 25.00 22.75 2.25 Sample Organization 1
-------------------------------------------------------------
Chili's 4 100.00 91.00 9.00 Sample Organization 1
使用UNION
将删除重复项,只留下一行。
答案 1 :(得分:0)
我唯一能想到的是UNION
将其更改为UNION ALL
UNION
将消除重复
运行这些查询以查看差异
select 1 as a
union
select 1
union
select 1
select 1 as a
union all
select 1
union all
select 1