写一个查询,以便为每一天的前十名客户提供服务

时间:2016-02-25 16:38:54

标签: sql sql-server tsql

我的customer_table包含cust_id,first_name和last_name列,cust_order表包含cust_id,order_id,order_date和amount columns.how以编写查询以便为每个客户提供每天

with the_dates as (
  select to_date('080114','MMDDYY') + level - 1 as the_date
    from dual
 connect by level <= to_date('011716', 'MMDDYY') 
                     - to_date('080114', 'MMDDYY') + 1
         )
select distinct trunc(a.the_date), count(*)
from the_dates a
left outer join TableFoo f on a.the_date = to_date(admit_date, 'MMDDYYYY')
where f.customer_num = 10
group by trunc(a.the_date)
order by trunc(a.the_date);

上面的代码就是我尝试的但是我无法得到所需的,因为我需要每一天的前10个订单

1 个答案:

答案 0 :(得分:0)

WITH TOP10 AS (
    SELECT C.cust_id, C.first_name, O.order_date,O.amount, ROW_NUMBER() 
    over (
        PARTITION BY O.Order_date 
        order by O.Amount desc
    ) AS RowNo 
    FROM [dbo].[customer_table] C  JOIN [dbo].[customer_orders] O on C.Cust_id=O.Cust_id
)
SELECT * FROM TOP10 WHERE RowNo <= 10

有效