TVF UDF 不返回与 SELECT 相同的数据
像这样调用 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 不同的数据。有什么建议和/或答案吗?
Calling the UDF like so:
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
yields:
"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"
Using the UDF logic and testing the results with 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
yields:
"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 is the table returned by the UDF and is created like so:
INSERT INTO @ReturnTable(product_name,
unit_price,
quantity,
face_value,
net_cost,
organization_name)
(select * from @TablePartial UNION select * from @TableClosed)
The test with the SELECT and variables is returning the correct data, but calling the UDF is not getting those other 3 Chili's records. I am using the same data for parameters. I'm quite new to UDFs and I'm not sure why it would return different data than what the SELECT does. Any suggestions and/or answers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要
UNION ALL
而不是UNION
查看两个结果集,将其加起来就好像 4 个 Chilli 的行都是相同的。
使用
UNION
将删除重复项,留下一行。You probably need
UNION ALL
notUNION
Looking at the two result sets it adds up as though the 4 Chilli's rows are all the same.
Using
UNION
will remove the duplicates leaving you with one row.我唯一能想到的是
UNION
将其更改为UNION ALL
UNION
将消除重复运行这些查询以查看差异
The only thing I can think of is the
UNION
change it toUNION ALL
UNION
will eliminate dupsRun these queries to see the difference