2

現在、ここでの私のコードはそのような結果を生み出します:

 SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS  MONTH, COUNT(*) AS TOTAL
 FROM news
 GROUP BY MONTH 

 UNION ALL 

 SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
 FROM equipment
 GROUP BY MONTH 

 RESULTS:
 YEAR MONTH TOTAL
 2013 FEB    1 (news table)
 2013 JAN   12 (news table)
 2013 FEB    1 (equipment table)
 2013 JAN   11 (equipment table)

以下のみを表示できるようにSQLクエリを編集するにはどうすればよいですか。

 RESULTS:
 YEAR MONTH TOTAL
 2013 FEB    2 (both news and equipment table)
 2013 JAN   23 (both news and equipment table)

助けてくれてありがとう!

4

4 に答える 4

2

試す :

SELECT YEAR, MONTH, SUM(TOTAL) AS TOTAL
FROM (SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS  MONTH, COUNT(*) AS TOTAL
 FROM news
 GROUP BY MONTH 

 UNION ALL 

 SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
 FROM equipment
 GROUP BY MONTH) x
GROUP BY YEAR, MONTH
于 2013-01-14T14:01:47.103 に答える
1

試す

Select year, month, sum(total) from
(
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS  MONTH, COUNT(*) AS TOTAL
 FROM news
 GROUP BY MONTH 

 UNION ALL 

 SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
 FROM equipment
 GROUP BY MONTH 
)
group by year, month
于 2013-01-14T14:03:07.447 に答える
0

これを試して:

SELECT YEAR, MONTH, SUM(total) total 
FROM (SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH, COUNT(*) AS TOTAL 
      FROM news 
      GROUP BY MONTH
      UNION ALL
      SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL 
      FROM equipment 
      GROUP BY MONTH
     ) AS A 
GROUP BY YEAR, MONTH
于 2013-01-14T14:04:26.883 に答える
0
select x.year, x.month, sum(x.total) from x(
 SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS  MONTH, COUNT(*) AS TOTAL
 FROM news
 GROUP BY MONTH 

 UNION ALL 

 SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
 FROM equipment
 GROUP BY MONTH ) x
group by x.month, x.year
于 2013-01-14T14:05:52.273 に答える