1

unionallを使用して2つのテーブルを結合しました。ただし、生成されたデータには繰り返しがあります。リピートに参加する方法はありますか?

たとえば、すべてを結合した後の結果のテーブルはt次のようになります。

__DAY__.....................__COUNT__
monday....................1
tuesday...................2
tuesday...................3
wednesday..............4
wednesday..............1
wednesday..............5

私はそれをこのように見せたいです:

__DAY__.................__COUNT__
monday....................1
tuesday....................5
wednesday..............10

また、「group by t.day、t.count」を試しましたが、機能しません。また、「....」は、ある種のインデントを付けるために、フォーマットのみを目的としています。

クエリは次のとおりです。

SELECT   t1.hour, t1.day, t1.count
FROM
(
  (SELECT hour(time(FROM_UNIXTIME( timecode)))  AS hour,
   date(FROM_UNIXTIME( timecode))  AS day,
  (FROM_UNIXTIME( timecode)) AS original, COUNT(1)  as 'count'
   FROM sampleData
  WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
   GROUP BY DAY)
 union all
  (SELECT hour(time(FROM_UNIXTIME( timecode))) as hour,
   date(FROM_UNIXTIME( timecode-86400) ) AS day,
   (FROM_UNIXTIME( timecode)) AS original, COUNT(1)  as 'count'
  FROM sampleData
  WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
   GROUP BY DAY)
)  t1
GROUP BY  t1.day, t1.count
HAVING COUNT(*)>0
ORDER BY t1.day
4

3 に答える 3

5

この簡単なクエリを試してください:

SELECT day,sum(count) FROM Table1
Group by day

このSQLFiddleを参照してください

編集:あなたの質問のためにこれを試してください:

SELECT   t1.hour, t1.day, sum(t1.count)
FROM 
(
  (SELECT hour(time(FROM_UNIXTIME( timecode)))  AS hour,  
   date(FROM_UNIXTIME( timecode))  AS day, 
  (FROM_UNIXTIME( timecode)) AS original, COUNT(1)  as 'count'  
   FROM sampleData  
  WHERE hour(time(FROM_UNIXTIME( timecode)))>=14  
   GROUP BY DAY)   
 union all   
  (SELECT hour(time(FROM_UNIXTIME( timecode))) as hour,  
   date(FROM_UNIXTIME( timecode-86400) ) AS day,  
   (FROM_UNIXTIME( timecode)) AS original, COUNT(1)  as 'count'  
  FROM sampleData   
  WHERE hour(time(FROM_UNIXTIME( timecode)))< 14  
   GROUP BY DAY)  
)  t1  
GROUP BY  t1.day
HAVING COUNT(*)>0  
ORDER BY t1.day  
于 2012-08-14T06:12:52.307 に答える
0

グループ化を試すことができます:

select DAY,sum(COUNT) as COUNT
from
( select DAY,COUNT from table1
  union all
 select DAY,COUNT from table1)a
group by a.DAY

編集

あなたの質問のために、

SELECT    t1.day, sum( t1.count )

.....
.....
GROUP BY  t1.day
HAVING COUNT(*)>0
ORDER BY t1.day
于 2012-08-14T06:12:00.270 に答える
0

カウントを合計し、日ごとにグループ化するだけです。

SELECT   t1.hour, t1.day, sum(t1.count)   
    FROM 
    (
      (SELECT hour(time(FROM_UNIXTIME( timecode)))  AS hour,  
       date(FROM_UNIXTIME( timecode))  AS day, 
      (FROM_UNIXTIME( timecode)) AS original, COUNT(1)  as 'count'  
       FROM sampleData  
      WHERE hour(time(FROM_UNIXTIME( timecode)))>=14  
       GROUP BY DAY)   
     union all   
      (SELECT hour(time(FROM_UNIXTIME( timecode))) as hour,  
       date(FROM_UNIXTIME( timecode-86400) ) AS day,  
       (FROM_UNIXTIME( timecode)) AS original, COUNT(1)  as 'count'  
      FROM sampleData   
      WHERE hour(time(FROM_UNIXTIME( timecode)))< 14  
       GROUP BY DAY)  
    )  t1  
    GROUP BY  t1.day
    HAVING COUNT(*)>0  
    ORDER BY t1.day  
于 2012-08-14T06:14:28.747 に答える