Вопрос : укажите, есть ли какая-либо модель, попавшая в пятерку лучших по количеству одновременно в 2008, 2009 и 2010 годах.

Я хочу извлечь значения, которые являются общими в 3 различных запросах, как показано ниже в одном запросе:

Select Top 5 id_model, Count(Quantity) 
From Fact_Transactions
Where Year(date) = 2008
Group by ID_Model
Order by Count(Quantity) DESC

Select Top 5 id_model, Count(Quantity) 
From Fact_Transactions
Where Year(date) = 2009
Group by ID_Model
Order by Count(Quantity) DESC

Select Top 5 id_model, Count(Quantity) 
From Fact_Transactions
Where Year(date) = 2010
Group by ID_Model
Order by Count(Quantity) DESC

Образец таблицы:

   Id_model         id_cust  id_loc             DATE         Quantity Total_price
    LNVO_A6600P     275262  CA-140858   2007-07-25 00:00:00.000 2   332.00
        Gi_X1       275262  CA-140858   2006-05-06 00:00:00.000 2   492.00
        Gi_SP       275261  CA-104220   2010-08-01 00:00:00.000 3   597.00
        MI_A1       275261  CA-104220   2010-03-20 00:00:00.000 1   144.00
        AC_LX1      275255  CA-120768   2009-05-30 00:00:00.000 5   1160.00
        Gi_SP       275252  US-131149   2009-10-17 00:00:00.000 2   398.00
        AC_LZ6      275250  CA-106075   2003-06-28 00:00:00.000 1   152.00
0
Aman kashyap 23 Фев 2018 в 08:28

6 ответов

Лучший ответ

Вместо 3, попробуйте этот единственный запрос

;WITH CTE
AS
(
    SELECT
       RN = ROW_NUMBER() OVER(PARTITION BY YEAR([date]) ORDER BY COUNT(Quantity) DESC),
       id_model,
       MyYear = YEAR([date]),
       Cnt = COUNT(Quantity)
    FROM Fact_Transactions
       WHERE YEAR(date) IN (2008,2009,2010)
       GROUP BY 
          ID_Model,
          YEAR([date])
)
SELECT
    id_model
    FROM CTE
       WHERE RN <6
          GROUP BY id_model
          HAVING COUNT(1)=3
1
Jayasurya Satheesh 23 Фев 2018 в 05:41

Вы можете использовать операцию INTERSECT:

Select Top 5 id_model,Count(Quantity) from Fact_Transactions
Where Year(date) =2008
Group by ID_Model
Order by Count(Quantity) DESC
INTERSECT
Select Top 5 id_model,Count(Quantity) from Fact_Transactions
Where Year(date) = 2009
Group by ID_Model
Order by Count(Quantity) DESC
INTERSECT
Select Top 5 id_model,Count(Quantity) from Fact_Transactions
Where Year(date) = 2010
Group by ID_Model
Order by Count(Quantity) DESC
1
codeLover 23 Фев 2018 в 05:35

Вы получите правильный ответ с помощью этого ..

select * from (SELECT 
    TOP 5 Manufacturer_name
    FROM Fact_Transactions T1
    LEFT JOIN DIM_Model D1 ON T1.IDModel = D1.IDModel
    LEFT JOIN DIM_MANUFACTURER D2  ON D2.IDManufacturer = D1.IDManufacturer
    Where DATEPART(Year,date)='2008' 
    group by Manufacturer_name, Quantity 
    Order by  SUM(Quantity ) DESC  
    intersect
SELECT  TOP 5 Manufacturer_name
    FROM Fact_Transactions T1
    LEFT JOIN DIM_Model D1 ON T1.IDModel = D1.IDModel
    LEFT JOIN DIM_MANUFACTURER D2  ON D2.IDManufacturer = D1.IDManufacturer
    Where DATEPART(Year,date)='2009' 
    group by Manufacturer_name, Quantity 
    Order by  SUM(Quantity ) DESC  
    intersect
SELECT TOP 5 Manufacturer_name
    FROM Fact_Transactions T1
    LEFT JOIN DIM_Model D1 ON T1.IDModel = D1.IDModel
    LEFT JOIN DIM_MANUFACTURER D2  ON D2.IDManufacturer = D1.IDManufacturer
    Where DATEPART(Year,date)='2010' 
    group by Manufacturer_name, Quantity 
    Order by  SUM(Quantity ) DESC)  as A
0
shashvindu 9 Дек 2019 в 16:29

Выберите * из (выберите топ-5 idmodel из FACT_TRANSACTIONS, где год (дата) = '2008' группа по idmodel заказ по сумме (количеству) desc пересекаются выберите топ-5 idmodel из FACT_TRANSACTIONS, где год (дата) = группа '2009' по порядку idmodel по сумме (количеству) desc пересекаются выберите топ-5 idmodel из FACT_TRANSACTIONS, где год (дата) = группа '2010' по порядку idmodel по сумме (количеству) desc) tbl сильный текст

-1
Parv Sharma 8 Май 2020 в 04:32

Попробуй это

select * from (
Select Top 5 id_model,Count(Quantity) from Fact_Transactions
Where Year(date) =2008
Group by ID_Model
Order by Count(Quantity) DESC
union all
Select Top 5 id_model,Count(Quantity) from Fact_Transactions
Where Year(date) = 2009
Group by ID_Model
Order by Count(Quantity) DESC
union all
Select Top 5 id_model,Count(Quantity) from Fact_Transactions
Where Year(date) = 2010
Group by ID_Model
Order by Count(Quantity) DESC)a
0
Mukesh Kalgude 23 Фев 2018 в 05:41

Простой inner join сделает:

SELECT id_model
FROM
(
    SELECT TOP 5 id_model, COUNT(Quantity)
    FROM Fact_Transactions
    WHERE YEAR(date) = 2008
    GROUP BY ID_Model
    ORDER BY COUNT(Quantity) DESC
) m2008
INNER JOIN
(
    SELECT TOP 5 id_model, COUNT(Quantity)
    FROM Fact_Transactions
    WHERE YEAR(date) = 2009
    GROUP BY ID_Model
    ORDER BY COUNT(Quantity) DESC
) m2009
    ON m2008.id_model = m2009.id_mode
INNER JOIN
(
    SELECT TOP 5 id_model, COUNT(Quantity)
    FROM Fact_Transactions
    WHERE YEAR(date) = 2010
    GROUP BY ID_Model
    ORDER BY COUNT(Quantity) DESC
) m2010
    ON m2008.id_model = m2010.id_model;
0
Tim Biegeleisen 23 Фев 2018 в 05:36