通过具有多个记录的门票销售来选择前5个客户

时间:2016-12-06 07:43:22

标签: sql

select client#,
       SUM(ticketpurchase.totalamount) value
from ops$yyang00.ticketpurchase,
     ops$yyang00.performance,
     ops$yyang00.production
where ticketpurchase.per# = performance.per# and
      performance.P#=production.P# and
      season#=1
group by client#
order by value desc; 

如何通过售票价值选择每个季节中的前五名客户一个值可以包括多个客户..

2 个答案:

答案 0 :(得分:0)

请为sql server尝试此代码。

Select * from (
    select client#,SUM(ticketpurchase.totalamount) value,
    row_number() over(order by SUM(ticketpurchase.totalamount)) rowNo
    from ops$yyang00.ticketpurchase,
         ops$yyang00.performance,
         ops$yyang00.production
    where ticketpurchase.per# = performance.per# and
          performance.P#=production.P# and
          season#=1
    group by client#
) as t
where rowNo <=5 

答案 1 :(得分:0)

试试这个:如果您使用的是SQL Server

select top 5 from (
                    select client#,
                           SUM(ticketpurchase.totalamount) value
                    from ops$yyang00.ticketpurchase,
                         ops$yyang00.performance,
                         ops$yyang00.production
                    where ticketpurchase.per# = performance.per# and
                          performance.P#=production.P# and
                          season#=1
                    group by client# )
order by value desc

注意: 如果您想要至少5条记录 {删除 desc {1}}