SQL中列表的前1项

时间:2014-12-01 08:58:05

标签: sql sql-server sql-server-2012

我有以下情况

我得到像这样的输出

Month      Product Name  Amount   
Jan-2014   A-Prodcut     50
Jan-2014   B-Product     45
Jan-2014   C-Product     55 
Feb-2014   A-Prodcut     60
Feb-2014   B-Product     48
Feb-2014   C-Product     80

我想要像这样输出:

 Jan-2014   C-Product     55
 Feb-2014   C-Product     80

我想要每个月的顶级产品如何实现这个目标

这是我的查询

        ;With cte as
        (
            SELECT @EndDate AS TheMonth, 0 as [Counter] 
            UNION ALL

            SELECT DATEADD(mm,-[Counter] -1,@EndDate), Counter + 1 AS TheMonth
            from cte
            WHERE DATEADD(mm,-[Counter] -1,@EndDate) >= @StartDate
        )

SELECT                      
    left(DATENAME(MM,TheMonth),3) +'-'+ cast(year(TheMonth) as varchar) AS [Month-Year],
    ISNULL(IM.product_name,'N/A') as 'Product Name',
    isnull(sum((ism.selling_price * siim.qty) + (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) AS Amount
FROM
    cte
    LEFT OUTER JOIN RS_Sell_Order_Master AS SM on MONTH(invoice_date) = MONTH(TheMonth)
                                                                        AND YEAR(invoice_date) = YEAR(TheMonth)   
                                                                        AND sm.is_approved = 1
    LEFT OUTER JOIN RS_Sells_Invoice_Info_Master AS SIIM ON SM.sell_order_no  = SIIM.sell_order_no
    LEFT OUTER JOIN RS_Inventory_Master AS IM ON SIIM.product_id = IM.product_id                
    LEFT OUTER JOIN RS_Tax_Master AS TM ON TM.tax_id = SIIM.tax_id
    LEFT OUTER JOIN RS_Inventory_Selling_Master AS ISM ON ISM.selling_product_id = SIIM.selling_product_id  
GROUP BY
    IM.product_name,
    TheMonth

3 个答案:

答案 0 :(得分:2)

在选择查询中使用Row_number(),即

SELECT MONTH, PRODUCT_NAME, AMOUNT
FROM (
   SELECT MONTH, PRODUCT_NAME, AMOUNT,
   ROW_NUMBER ( ) 
   OVER (PARTITION BY Month order by amount Desc) AS RANK
     )
WHERE RANK = 1

希望这会有所帮助..

答案 1 :(得分:2)

试试这个:

SELECT * 
FROM 
(
    SELECT [Month-Year], [Product Name], Amount, ROW_NUMBER() OVER (Partition BY  [Month-Year] ORDER BY Amount DESC) as rn
    FROM
    (
        SELECT                      
            left(DATENAME(MM,TheMonth),3) +'-'+ cast(year(TheMonth) as varchar) AS [Month-Year],
            ISNULL(IM.product_name,'N/A') as 'Product Name',
            isnull(sum((ism.selling_price * siim.qty) + (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) AS Amount
        FROM
            cte
            LEFT OUTER JOIN RS_Sell_Order_Master AS SM on MONTH(invoice_date) = MONTH(TheMonth)
                                                                                AND YEAR(invoice_date) = YEAR(TheMonth)   
                                                                                AND sm.is_approved = 1
            LEFT OUTER JOIN RS_Sells_Invoice_Info_Master AS SIIM ON SM.sell_order_no  = SIIM.sell_order_no
            LEFT OUTER JOIN RS_Inventory_Master AS IM ON SIIM.product_id = IM.product_id                
            LEFT OUTER JOIN RS_Tax_Master AS TM ON TM.tax_id = SIIM.tax_id
            LEFT OUTER JOIN RS_Inventory_Selling_Master AS ISM ON ISM.selling_product_id = SIIM.selling_product_id  
        GROUP BY
            IM.product_name,
            TheMonth
    ) a
)b
WHERE rn = 1

答案 2 :(得分:0)

通常OUTER APPLYCROSS APPLY是这种情况下的一种方式。

所以你的SELECT会是这样的:

;With cte as
(
    SELECT @EndDate AS TheMonth, 0 as [Counter] 
    UNION ALL

    SELECT DATEADD(mm,-[Counter] -1,@EndDate), Counter + 1 AS TheMonth
    from cte
    WHERE DATEADD(mm,-[Counter] -1,@EndDate) >= @StartDate
)
SELECT                      
    left(DATENAME(MM,TheMonth),3) +'-'+ cast(year(TheMonth) as varchar) AS [Month-Year],
    'Product Name', Amount
FROM
    cte
    OUTER APPLY ( SELECT TOP 1 
                   ISNULL(IM.product_name,'N/A') as 'Product Name',
                   isnull(sum((ism.selling_price * siim.qty) + 
                   (((tm.tax_amount*(ism.selling_price * siim.qty))/100))),0) AS Amount
        FROM  RS_Sell_Order_Master AS SM    
        LEFT OUTER JOIN RS_Sells_Invoice_Info_Master AS SIIM 
                ON SM.sell_order_no  = SIIM.sell_order_no
        LEFT OUTER JOIN RS_Inventory_Master AS IM 
                ON SIIM.product_id = IM.product_id                
        LEFT OUTER JOIN RS_Tax_Master AS TM 
                ON TM.tax_id = SIIM.tax_id
        LEFT OUTER JOIN RS_Inventory_Selling_Master AS ISM 
                ON ISM.selling_product_id = SIIM.selling_product_id  
       WHERE MONTH(invoice_date) = MONTH(TheMonth) AND YEAR(invoice_date) = YEAR(TheMonth) AND sm.is_approved = 1
       ORDER BY Amount DESC
   ) x
GROUP BY
    'Product Name',
    TheMonth