2016-12-16 56 views
-3

Q.列出在最近的12个月内没有成交的产品的名称。SQL:什么是适当的查询?

优惠表

i. Deal_id 
ii. Product_id 
iii. Deal_start_date 
iv. Deal_end_date 

产品的维度表

i. Product_id 
ii. Product_name 
iii. Manufacturer 

我的回答是:

Select Product_name, Rank() over(partition by year(Deal_start_date) order by Deal_start_date desc) as Deal_date 
From Product Dim Table 
Right join Deals Table on Product Dim Table.Product_id=Deals Table.Product_id 
where Deal_date > 1 
Group by Product_name 
Order by Deal_date desc 
+0

我已经删除了不兼容的数据库产品 - 请重新使用您实际使用的DBMS。 – Siyual

+0

'从不存在的产品P中选择*(从D选择“1”,其中D.Product_Id = P.Product_ID和D.Deal_End_Date> =“今天-12个月”,其中今天-12个月特定于RDBMS。 – xQbert

回答

1

试试这个:

Select p.Product_name 
From Product p 
Where not exists 
    (Select * from Deals 
    where Product_id = p.Product_id 
     and Deal_start_date <= @today 
     and Deal_end_date >= start) 
0

检查以下内容。/ 选择 PRODUCT_ID,PRODUCT_NAME,制造商 从产品暗淡 其中 PRODUCT_ID未在( 选择PRODUCT_ID 从 特卖 其中 Deal_end_dare>使用DateAdd(毫米,-12之间 deal_end_date,GETDATE())