SQL:什么是适合的查询?

时间:2016-12-16 18:51:52

标签: sql

问。列出最近12个月内交易中没有特色的产品名称。

交易表

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

检查以下内容。 /     选择     Product_id,Product_name,制造商     从产品昏暗     哪里     product_id不在(     选择product_id     从     交易     哪里     deal_end_date之间         Deal_end_dare&gt; DateAdd(mm,-12,GetDate())