首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从服务和销售表中返回前30名客户收入组合

如何从服务和销售表中返回前30名客户收入组合
EN

Stack Overflow用户
提问于 2014-03-04 20:07:16
回答 1查看 685关注 0票数 0

我可以使用此查询提取各个行,但希望将这两个表组合起来,并按客户编号显示按销售人员分列的前30位客户。

代码语言:javascript
复制
select top 30   sil.[Sell-to Customer No_]as 'Customer No.',
                cus.[Name], 
                sil.[Responsibility Center], 
                sil.[Amount] as 'Total',
                'SALES'
FROM            [Sales Invoice Line] sil left outer join [Customer]cus
on              sil.[Sell-to Customer No_] = cus.[No_]  
where           sil.[Amount] > 0
and             sil.[Responsibility Center] != 'cis'
and             sil.[Posting Date] between '10-01-13' and (current_timestamp)

group by        sil.[Amount], sil.[Sell-to Customer No_], sil.[Responsibility Center], cus.[Name]


union all


select top 30   sil.[Customer No_],
                cus.[Name], 
                sil.[Responsibility Center], 
                sil.[Amount] as 'Total', 
                'SERVICE'
FROM            [Service Invoice Line] sil left outer join [Customer]cus
on              sil.[Customer No_] = cus.[No_]
where           sil.[Amount] > 0
and             sil.[Responsibility Center] != 'cis'
and             sil.[Posting Date] between '10-01-13' and (current_timestamp)

group by        sil.[Amount], sil.[Customer No_], sil.[Responsibility Center], cus.[Name]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 20:13:28

用一个查询包装返回60行的整个查询,以获得前30行。

代码语言:javascript
复制
select top 30 x, y, z 
from (
    select top 30 x, y, z from a where blah
    union all
    select top 30 x, y, z from b where blah
) x --here's the alias for the subquery
order by topcriteria

order很重要,所以您可以得到适当的查询“顶部”。

编辑:下面是您的求和示例:

代码语言:javascript
复制
select top 30   sil.[Sell-to Customer No_]as 'Customer No.',
                cus.[Name], 
                sil.[Responsibility Center], 
                sum(sil.[Amount]) as 'Total',
                'SALES'
FROM            [Sales Invoice Line] sil left outer join [Customer]cus
on              sil.[Sell-to Customer No_] = cus.[No_]  
where           sil.[Amount] > 0
and             sil.[Responsibility Center] != 'cis'
and             sil.[Posting Date] between '10-01-13' and (current_timestamp)
group by        sil.[Sell-to Customer No_], sil.[Responsibility Center], cus.[Name]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22181888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档