TVF UDF 不返回与 SELECT 相同的数据

发布于 2024-09-25 09:15:33 字数 1703 浏览 3 评论 0原文

像这样调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-10-02 09:15:33

您可能需要 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 将删除重复项,留下一行。

You probably need UNION ALL not UNION

Looking at the two result sets it adds up as though the 4 Chilli's rows are all the same.

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

Using UNION will remove the duplicates leaving you with one row.

白色秋天 2024-10-02 09:15:33

我唯一能想到的是 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

The only thing I can think of is the UNION change it to UNION ALL UNION will eliminate dups

Run these queries to see the difference

select 1 as a
union 
select 1
union 
select 1


select 1 as a
union  all
select 1
union all
select 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文