Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何从服务和销售表中返回前30名客户收入组合_Sql Server_Grouping - Fatal编程技术网

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

Sql server 如何从服务和销售表中返回前30名客户收入组合,sql-server,grouping,Sql Server,Grouping,我可以通过这个查询拉取单独的行,但我想将这两个表结合起来,按客户编号按销售人员显示前30名客户 select top 30 sil.[Sell-to Customer No_]as 'Customer No.', cus.[Name], sil.[Responsibility Center], sil.[Amount] as 'Total', 'SALES' F

我可以通过这个查询拉取单独的行,但我想将这两个表结合起来,按客户编号按销售人员显示前30名客户

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]

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

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 by很重要,因此您可以获得查询的正确顶部

编辑:以下是您的总和示例:

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]

所以我应该在子查询中包含我的联接,对吗?是的。。。如果您的查询返回您要查找的60行,只需按原样包装即可。为了简洁起见,我缩短了我的示例我的订单之前的最后一行出现语法错误…Hmmm。您需要为子查询别名。我忘了那部分。让我编辑我的答案。好的。这给了我每个客户从高到低的金额,但不是每个客户的总额,从高到低。