从另一个查询的结果查询

时间:2018-04-13 19:05:30

标签: sql-server tsql

我正在尝试创建一个查询,根据销售额$和每种产品的前10位客户列出前150种产品。我可以创建两个查询,第一个是基于sales $的前150个产品,另一个是按产品划分的每个客户的总销售额,但是如何将它们组合成一个查询,以便结果将有1500条记录,展示了前150名产品中每一个的前10名客户?

首先是

的客户销售排名
SELECT     
    product_ID, custno, Total_Sales, 
    RANK() OVER (ORDER BY Total_Sales DESC) AS Sale_Rank_byCust
FROM  
    (SELECT  
         product_ID, custno, SUM(Sales$) AS Total_Sales
     FROM    
         dbo.SalesDetails
     WHERE   
         invdte >= GETDATE() - 1095
     GROUP BY 
         product_ID, custno) AS B

第二个是按销售额计算的前150名产品

SELECT TOP 150 
    product_ID, T_Extprice, 
    RANK() OVER (ORDER BY T_Extprice DESC) AS SalesRank
FROM  
    (SELECT 
         product_ID, SUM(Sales$) AS T_Extprice, product
     FROM  
         dbo.SalesDetails AS SD
     GROUP BY 
         product_ID) AS A

4 个答案:

答案 0 :(得分:1)

    select * from (
SELECT     
    product_ID, custno, Total_Sales, 
    RANK() OVER (ORDER BY Total_Sales DESC) AS Sale_Rank_byCust
FROM  
    (SELECT  
         product_ID, custno, SUM(Sales$) AS Total_Sales
     FROM    
         dbo.SalesDetails
     WHERE   
         invdte >= GETDATE() - 1095
     GROUP BY 
         product_ID, custno) AS B
    ) sales1

    LEFT OUTER JOIN

    select * from (
    SELECT TOP 150 
        product_ID, T_Extprice, 
        RANK() OVER (ORDER BY T_Extprice DESC) AS SalesRank
    FROM  
        (SELECT 
             product_ID, SUM(Sales$) AS T_Extprice, product
         FROM  
             dbo.SalesDetails AS SD
         GROUP BY 
             product_ID) AS A
        ) sales2 
          ON sales1.product_ID = sales2.product_ID

答案 1 :(得分:1)

你应该可以使用外部应用这样做。

select      *
from        (
                select      top 150 
                            product_ID, 
                            SUM(Sales$) AS Total_Sales
                from        dbo.SalesDetails sd1
                group by    product_ID
                order by    Total_Sales desc
            ) p
outer apply (
                select      top 10
                            custno,
                            SUM(Sales$) AS Total_Sales
                from        dbo.SalesDetails sd2
                where       sd2.product_ID = p.product_ID
                and         sd2.invdte >= GETDATE() - 1095
                group by    custno
                order by    Total_Sales desc

            ) c

您可以使用单个派生表执行此操作,并以不同方式对2个排名进行分区。

答案 2 :(得分:0)

我转向窗口函数来做这样的事情。这是一种方法:

select pc.*
from (select pc.*,
             dense_rank() over (order by product_sales desc, product_id) as product_rank
      from (select sd.product_id, sd.custno, sum(sd.sales$) as total_sales,
                   row_number() over (partition by sd.product_id order by sum(sd.sales$) as cust_within_product_rank,
                   sum(sum(sd.sales$)) over (partition by sd.product_id) as product_sales
            from salesdetails sd
            group by sd.product_id, sd.custno
           ) pc
      ) pc
where product_rank <= 150 and cust_within_product_rank <= 10;

答案 3 :(得分:0)

您可以尝试交叉加入。

select top 105 * from salesTable where <as you wish>
cross join 
select top 10 * from customerTable where <as you wish>