2014-07-24 43 views
0

我有以下兩個查詢查找每個星期瀏覽量和訂單從2014年一月兩個查詢的左外部連接條件?

select productid, 
EXTRACT (week from dt)as WEEK, 
count(productid)as PageViews 
from PageView 
where client = 'XYZ' 
and dt between '2014-06-01' and '2014-06-30' 
GROUP BY WEEK, productid 
ORDER BY WEEK asc; 

select 
count(distinct t.orderid), 
EXTRACT(week from t.dt) AS WEEK 
FROM Transaction t 
where t.client = 'XYZ' 
and t.dt between '2014-01-01' AND '2014-06-30' 
GROUP BY WEEK 
ORDER BY WEEK asc; 

開始爲了得到正確的數據,我需要創造條件與Transaction和PageView Tables的一週相匹配,類似於WeekView from Week = WeekPage from PageView。然而,我不知道這會是什麼樣的語法。

做一個簡單的

PageView pv LEFT OUTER JOIN Transaction t 
ON pv.productid = t.productid 
AND EXTRACT(week from t.dt) = EXTRACT(week from pv.dt) 

不給正確的輸出(即,網頁瀏覽和訂單又顯著高)。可有人請闡明如何將這兩個查詢兩者結合起來獲得期望的輸出是所有從網頁瀏覽表中的產品及相應的訂單從按星期分組

+0

做pageview和trans table之間有什麼關係嗎? – Rahul

+0

productid列 –

+0

這兩個表的主鍵是什麼?這兩個表有哪些備用密鑰(如果有的話)? –

回答

1
select week, pid, pageviews, orders 
from 
    (
     select 
      date_trunc('week', dt) as week, 
      productid as pid, 
      count(productid) as pageviews 
     from pageview 
     where client = 'XYZ' and dt between '2014-06-01' and '2014-06-30' 
     group by 1, 2 
    ) pv 
    full outer join 
    (
     select 
      date_trunc('week', dt) as week, 
      product_id as pid, 
      count(orderid) as orders 
     from transaction 
     where client = 'XYZ' and dt between '2014-01-01' and '2014-06-30' 
     group by 1, 2 
    ) t using (week, pid) 
order by 1, 2 
+0

爲什麼「全外連接」?只是想明白。 – Rahul

+0

@Rahul是否有可能沒有對某個產品ID的意見訂單?問題並不清楚。如果答案是否定的,只要把'full outer'改成'left'即使'full'會正常工作,只是刨牀不能找到最好的計劃。 –

+0

@ClodoaldoNeto謝謝,這似乎工作得很好。然而,date_trunc給出了一個完整的日期和時間戳,而不僅僅是星期數。我想我應該在問題中提到這一點。提取(星期從dt)作爲WEEK更好的選擇? –

0

我認爲這個問題是ON pv.productid = t.productid AND EXTRACT(week from t.dt) = EXTRACT(week from pv.dt)

交易表中的某些光這不能在左連接上。 PV與T之間的ID可能會匹配,但在某些情況下,這些周可能爲空。所以AND會消除左連接。

所以你只需要加入ID的第一個,所以你得到了笛卡爾的結果。然後,您需要過濾出不匹配的星期,但留在T.Null值中。但是,它假定t.dt不能爲null,如果它可以爲null,則計數可能仍然是關閉的,具體取決於您希望如何在交易表日期處理空值。

此外,您可能需要使用內嵌視圖(但我認爲這是矯枉過正)

SELECT * 
FROM 
(select productid, 
    EXTRACT (week from dt)as WEEK, 
    count(productid)as PageViews 
    from PageView 
    where client = 'XYZ' 
    and dt between '2014-06-01' and '2014-06-30' 
    GROUP BY WEEK, productid) PV 
LEFT JOIN (
    SELECT 
    count(distinct t.orderid), 
    EXTRACT(week from t.dt) AS WEEK 
    FROM Transaction t 
    where t.client = 'XYZ' 
    and t.dt between '2014-01-01' AND '2014-06-30' 
    GROUP BY WEEK) T 
ON pv.productid = t.productid --The gold is in this and the next line 
WHERE (T.Week = PV.Week OR T.Week is null) --don't forget this one. 

我不認爲這會工作... ON(PV.ProductID = T.productID和T .Week = PV.Week) OR(PV.ProductID = T.ProductID AND T.Week爲null)

因爲T.Week將不會被計算,因爲笛卡爾不會被生成。因此我認爲它屬於哪裏。

+0

推測表格也必須加入客戶ID或訂單ID? –

+0

是的,但我認爲你應該刪除'PV.Week = T.Week' – Rahul

+0

不確定你爲什麼刪除了你的查詢?每個人都是對的。 – Rahul