0

2 つのテーブルから SUM/Total レコードを 1 か月に 1 日に取得したいのですが、1 つのテーブルでクエリを実行すると機能しますが、commulative が機能しません。

select DATE_FORMAT(date(dateadded),'%m/%d/%Y') as dateadded, count(id) as tot_rent
from crm_rentals WHERE YEAR(dateadded) = YEAR(CURRENT_DATE)
AND MONTH(dateadded) = MONTH(CURRENT_DATE)
group by date(dateadded)

これは 2 番目のテーブル用です。これも問題ありません。

select DATE_FORMAT(date(dateadded),'%m/%d/%Y') as dateadded, count(id) as tot_rent
from crm_sales WHERE YEAR(dateadded) = YEAR(CURRENT_DATE)
AND MONTH(dateadded) = MONTH(CURRENT_DATE)
group by date(dateadded)

問題は、結果の毎日の合計を見つけたいときですが、正しく機能していません..ここに私の最終的なクエリがあります

select dateadded,count(tot_rent) as tot_rent
from ( select DATE_FORMAT(date(dateadded),'%m/%d/%Y') as dateadded, count(id) as tot_rent
from crm_rentals WHERE YEAR(dateadded) = YEAR(CURRENT_DATE)
AND MONTH(dateadded) = MONTH(CURRENT_DATE)
group by date(dateadded)
union
select DATE_FORMAT(date(dateadded),'%m/%d/%Y') as dateadded, count(id) as tot_rent
from crm_sales WHERE YEAR(dateadded) = YEAR(CURRENT_DATE)
AND MONTH(dateadded) = MONTH(CURRENT_DATE)
group by date(dateadded)) s group by date(dateadded)

crm_rentals のテーブル構造 (最初のテーブル、日付 = Ymd)

id -----dateadded
15------2013-04-02
16------2013-04-03
17------2013-04-03

表 2 crm_sales

id------dateadded
 7------2013-04-02

結果は

dateadded-----tot_rent
04/02/2013 ----2
04/03/2013  ---2 //this line is not shown
4

1 に答える 1

0

最初のクエリが与える

id -----dateadded
1------2013-04-02
2------2013-04-03

2番目のクエリが与える

id -----dateadded
1------2013-04-02

だから組合は与える

1------2013-04-02
2------2013-04-03
1------2013-04-02

そのグループが行います

2------2013-04-02
1------2013-04-03

正しいアプローチは

15------2013-04-02
16------2013-04-03
17------2013-04-03

連合

 7------2013-04-02

その後、グループは

2------2013-04-02
2------2013-04-03

これを試して:

select dateadded,count(tot_rent) as tot_rent
from ( select DATE_FORMAT(date(dateadded),'%m/%d/%Y') as dateadded, id as tot_rent
from crm_rentals WHERE YEAR(dateadded) = YEAR(CURRENT_DATE)
AND MONTH(dateadded) = MONTH(CURRENT_DATE)
union
select DATE_FORMAT(date(dateadded),'%m/%d/%Y') as dateadded, id as tot_rent
from crm_sales WHERE YEAR(dateadded) = YEAR(CURRENT_DATE)
AND MONTH(dateadded) = MONTH(CURRENT_DATE)
) s group by dateadded
于 2013-04-11T11:55:11.847 に答える