2016-12-16 54 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())