SQL Server:针对具有多个行项目的订单仅输出一个邮资成本(SQL Server : output only one Postage Cost Against an Order with Mul

编程入门 行业动态 更新时间:2024-10-14 12:21:15
SQL Server:针对具有多个行项目的订单仅输出一个邮资成本(SQL Server : output only one Postage Cost Against an Order with Multiple Line Items)

我在查询订单;

Order# Customer Item Postage 11111 Customer1 Item1 £3.99 11112 Customer2 Item1 £3.99 11112 Customer2 Item2 £3.99 11112 Customer2 Item3 £3.99

我的问题是每个订单我只想输出一个邮费。 在上面的例子中,11112输出三个邮资费用。

期望的结果;

Order# Customer Item Postage 11111 Customer1 Item1 £3.99 11112 Customer2 Item1 11112 Customer2 Item2 11112 Customer2 Item3 £3.99

任何人都可以指导我一些文档或提供样本SELECT查询来处理这个问题吗?

我简化了问题和输出,对于那些想要查看实际数据库结构的人,我附上了订单表(Linnworks)的图像,忽略了与该问题无关的邮政服务表。

I am querying orders;

Order# Customer Item Postage 11111 Customer1 Item1 £3.99 11112 Customer2 Item1 £3.99 11112 Customer2 Item2 £3.99 11112 Customer2 Item3 £3.99

My problem is for each order I only want to output one postage cost. In the above example, 11112 is outputting three postage costs.

Desired result;

Order# Customer Item Postage 11111 Customer1 Item1 £3.99 11112 Customer2 Item1 11112 Customer2 Item2 11112 Customer2 Item3 £3.99

Can anyone direct me to some documentation or provide from sample SELECT queries for dealing with this problem?

I have simplified the question and output, for those that would like to look at the actual Database structure, I have attached an image of the order table (Linnworks), ignore the Postal Services table it has no relevance to the question.

最满意答案

我认为你可以用row_number()做你想做的事:

with q as ( <your query here> ) select OrderNum, Customer, Item, (case when seqnum = 1 then postage end) as Postage from (select q.*, row_number() over (partition by ordernum order by (select null)) as seqnum from q ) q;

I think you can do what you want with row_number():

with q as ( <your query here> ) select OrderNum, Customer, Item, (case when seqnum = 1 then postage end) as Postage from (select q.*, row_number() over (partition by ordernum order by (select null)) as seqnum from q ) q;

更多推荐

本文发布于:2023-08-07 17:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   邮资   订单   成本   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!