2016-12-30 50 views
-2

我有兩個表MYSQL計數與內部聯接不工作

1)外展

id profile_id url 
------------------------- 
1 2  www.test.com 
2 3  www.google.com 
3 4  www.example.com 
4 2  www.test2.com 
5 2  www.test3.com 
6 2  www.test4.com 

2)。 outreach_links

id outreach_id start_date    created_at  cost status 
----------------------------------------------------------------------- 
1 1   2016-12-01 00:00:00 2016-12-07 00:00:00 100.00 Approved 
2 1   2016-12-02 00:00:00 2016-12-09 00:00:00 120.00 Approved 
3 1   NUll     2016-12-28 00:00:00 20.00 Pending 
4 1   2016-12-05 00:00:00 2016-12-10 00:00:00 35.00 Approved 
5 1   2016-12-07 00:00:00 2016-12-13 00:00:00 10.00 Approved 
6 2   2016-12-10 00:00:00 2016-12-15 00:00:00 10.00 Pending 
7 2   2016-12-13 00:00:00 2016-12-18 00:00:00 10.00 Approved 
8 2   2016-12-01 00:00:00 2016-12-28 00:00:00 10.00 Pending 
9 2   2016-12-04 00:00:00 2016-12-21 00:00:00 10.00 Approved 
10 2   2016-12-09 00:00:00 2016-12-22 00:00:00 15.00 Pending 

我試圖做的月/年計,我認爲它的工作,但我認爲它不工作,因爲「PROFILE_ID」這裏的問題是我的查詢:

select monthname(date) as Month, year(date) as Year, month(date) as Mn, UNIX_TIMESTAMP(CONCAT(year(date),"-",month(date),"-","01")) as tt, 
(select count(*) from outreach_links where year(outreach_links.created_at) = year and month(outreach_links.created_at) = month and status = "Pending" and created_at>="2016-12-01 00:00:00" and created_at<="2016-12-31 00:00:00") as pp, 
(select count(*) from outreach_links where year(outreach_links.start_date) = year and month(outreach_links.start_date) = month and status = "Approved" and start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00") as aa, 
(select sum(cost) from outreach_links where year(outreach_links.start_date) = year and month(outreach_links.start_date) = month and status = "Approved" and start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00") as cc 
from 
(select year(outreach_links.created_at) as year, month(outreach_links.created_at) as month, outreach_links.created_at as date 
from outreach_links 
inner join outreach on outreach.id = outreach_links.outreach_id 
where outreach_links.created_at>="2016-12-01 00:00:00" and outreach_links.created_at<="2016-12-31 00:00:00" and outreach.profile_id=2 
union 
select year(outreach_links.start_date) as year, month(outreach_links.start_date) as month, outreach_links.start_date as date 
from outreach_links 
inner join outreach on outreach.id = outreach_id 
where start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00" and outreach.profile_id=2) t1 
group by year, month 
order by date 

所以我我所做的日期範圍從「2016-12-01 00:00:00」到「2016-12-31 00:00:00」,這些日期範圍可以是用戶輸入的任何日期範圍,並嘗試根據outreach.profile_id = 2,我的輸出是錯誤的,它計算所有的profile_ids的一切,我不知道爲什麼

注:這只是一個表的樣本,可能有更多的記錄和用戶inputed日期範圍到可能是不同的,我想他們按月/年

這裏是我的輸出: (其計算所有記錄)

array:1 [▼ 
    0 => {#394 ▼ 
    +"Month": "December" 
    +"Year": "2016" 
    +"Mn": "12" 
    +"tt": "1480568400.000000" 
    +"pp": "4" 
    +"aa": "6" 
    +"cc": "285.00" 
    } 
] 

哪項是錯誤的,應該= 2,這裏所需要的輸出我想只爲PROFILE_ID數:

array:1 [▼ 
    0 => {#394 ▼ 
    +"Month": "December" 
    +"Year": "2016" 
    +"Mn": "12" 
    +"tt": "1480568400.000000" 
    +"pp": "1" 
    +"aa": "4" 
    +"cc": "265.00" 
    } 
] 

正如你可以看到3個計數是錯誤的,他們假設是: 「頁」: 「1」 「AA」: 「4」 「CC」: 「265.00」

這裏就是我尋找:

1). **"pp" is Total Pending** Count when status="Pending" based on created_at 
2). **"aa" is Total Approved** Count when status="Approved" based on start_date 
3). **"cc" is Total Cost** Sum of All cost when Status="Approved" and based on start_date 
4). Group by Month & Year of the user imputed Date Range 

這裏是一個SQLFIDDLE >>http://sqlfiddle.com/#!9/87dfa8/1

你能幫我解決嗎?

感謝

+0

你能用簡單的話來描述你想要計算,聚合等嗎?僅僅看着查詢就很難理解。 –

+0

3件事要計算:1.如果在start_date之間,則批准的總數2.基於created_date範圍的總計掛起3。總成本只有在狀態獲得批准並且在輸入範圍和所有這些之間的開始日期才需要僅針對輸入的profile_id時,請注意日期範圍可以由給定用戶更改 – user3150060

+0

我想按月和年分組它們,與輸出陣列。現在更清楚了嗎? – user3150060

回答

-1

我想你想是這樣的:

SELECT MONTHNAME(d.date) AS Month 
     , YEAR(d.date)  AS Year 
     , MONTH(d.date)  AS Mn 
     , SUM(IF(l.status = 'Pending' AND l.created_at >= d.date AND l.created_at < d.date + INTERVAL 1 MONTH ,1  ,0)) AS pp 
     , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.date + INTERVAL 1 MONTH ,1  ,0)) AS aa 
     , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.date + INTERVAL 1 MONTH ,l.cost,0)) AS cc 
    FROM (SELECT '2016-12-01' + INTERVAL 0 MONTH AS date) d 
    JOIN outreach o 
    ON o.profile_id = 2 
    LEFT 
    JOIN outreach_links l 
    ON l.outreach_id = o.id 
    AND ( ( l.start_date >= d.date + INTERVAL 0 MONTH 
      AND l.start_date < d.date + INTERVAL 1 MONTH 
      ) 
     OR ( l.created_at >= d.date + INTERVAL 0 MONTH 
      AND l.created_at < d.date + INTERVAL 1 MONTH 
      ) 
     ) 
    GROUP BY d.date 

編輯

要使用的起始日期,內嵌沿着指定範圍的結束日期查看d可以返回兩個日期值。並且外部查詢可以引用第二個日期值,在此示例中爲d.end_date,以及範圍d.date的開始。

SELECT MONTHNAME(d.date) AS Month 
     , YEAR(d.date)  AS Year 
     , MONTH(d.date)  AS Mn 
     , SUM(IF(l.status = 'Pending' AND l.created_at >= d.date AND l.created_at < d.end_date,1  ,0)) AS pp 
     , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.end_date,1  ,0)) AS aa 
     , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.end_date,l.cost,0)) AS cc 
    FROM (SELECT '2016-12-01' + INTERVAL 0 MONTH AS date 
       , '2016-12-16' + INTERVAL 0 MONTH AS end_date 
     ) d 
    JOIN outreach o 
    ON o.profile_id = 2 
    LEFT 
    JOIN outreach_links l 
    ON l.outreach_id = o.id 
    AND ( ( l.start_date >= d.date 
      AND l.start_date < d.end_date 
      ) 
     OR ( l.created_at >= d.date 
      AND l.created_at < d.end_date 
      ) 
     ) 
    GROUP BY d.date, d.end_date 
+0

區間是做什麼的? – user3150060

+0

我需要保持這兩個日期inputed bc這些可能會改變和不需要一個月我可以輸入2016-12-01至2016-12-15或任何其他日期,它會計數 – user3150060

+0

我包括第二個版本,它允許結束日期提供。外部查詢引用內聯視圖'd'返回的值,因此這兩個日期值僅在查詢中提供一次,而不是多次。 http://sqlfiddle.com/#!9/c1f7053/11 – spencer7593