1

At the bottom of the post you can find a simplify query of what I am trying to do

システム内のすべてのユーザーの生産性を示すクエリを作成しようとしていますTRAC。彼らが入力したすべてのコメント(1週間あたりのチケットごとに一意)、タッチされたチケットの数、閉じられたチケットの数、およびリビジョンの数を知る必要があります(次に、そのようなリビジョンのファイルの数も追加しますノードをチェックします)。その週に少なくともチケットに触れている限り、ほぼ必要に応じて機能するクエリを作成しました。 チケットを参照せずにリビジョンをコミットしているユーザーがいます(間違っていますが、それらもキャッチする必要があります) 。MySQLがサポートしていないことをエミュレートするためにRIGHT JOINとを追加しようとしましたが、このエラーは発生しません。UNIONOUTER JOIN

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't LEFT JOIN ( SELECT CONCAT( ' at line 65

私がそれの2つの部分のうちの1つをコメントアウトするならば、UNIONそれは働きます。

私はすでにテーブルに異なるエイリアスを与えようとしました

SELECT
   t.`author` __group__,
   t.`month-week`,
   t.changes,
   t.tickets,
   t.closed,
   r.`revisions`
FROM
(
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(tc.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(tc.`time`/1000000))
         ) `month-week`,
         COUNT(IF(tc.`field` = "comment",tc.ticket,NULL)) `changes`,
         COUNT(DISTINCT tc.`ticket`) `tickets`,
         COUNT(DISTINCT IF(tc.`field` = "status" AND tc.`newvalue` = "closed",tc.`ticket`,NULL)) `closed`,
         tc.`author`
      FROM `ticket_change` tc
      WHERE
         tc.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         tc.`author`,
         YEARWEEK(FROM_UNIXTIME(tc.`time`/1000000))
   ) `t`
   LEFT JOIN
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(r.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(r.`time`/1000000))
         ) `month-week`,
         COUNT(DISTINCT r.`rev`) `revisions`,
         r.`author`
      FROM `revision` r
      WHERE
         r.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         r.`author`,
         YEARWEEK(FROM_UNIXTIME(r.`time`/1000000))
   ) `r` ON `t`.`month-week` = `r`.`month-week` AND `t`.`author` = `r`.`author`
)
UNION
(
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(tc.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(tc.`time`/1000000))
         ) `month-week`,
         COUNT(IF(tc.`field` = "comment",tc.ticket,NULL)) `changes`,
         COUNT(DISTINCT tc.`ticket`) `tickets`,
         COUNT(DISTINCT IF(tc.`field` = "status" AND tc.`newvalue` = "closed",tc.`ticket`,NULL)) `closed`,
         tc.`author`
      FROM `ticket_change` tc
      WHERE
         tc.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         tc.`author`,
         YEARWEEK(FROM_UNIXTIME(tc.`time`/1000000))
   ) `t`
   LEFT JOIN
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(r.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(r.`time`/1000000))
         ) `month-week`,
         COUNT(DISTINCT r.`rev`) `revisions`,
         r.`author`
      FROM `revision` r
      WHERE
         r.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         r.`author`,
         YEARWEEK(FROM_UNIXTIME(r.`time`/1000000))
   ) `r` ON `t`.`month-week` = `r`.`month-week` AND `t`.`author` = `r`.`author`
)

WHERE r.revisions > 0 OR t.closed > 0
ORDER BY
   t.`author`,
   t.`month-week`

MySQLサーバー/クライアントバージョン:5.0.33

-

SELECT
   t.fields,
   r.fields
FROM
(
   (
      SELECT    tc.fields
      FROM      tc
      WHERE     tc.field > NOW() - INTERVAL 2 MONTH
      GROUP BY  tc.field
   ) t
   LEFT JOIN
   (
      SELECT    rev.fields
      FROM      rev
      WHERE     rev.field > NOW() - INTERVAL 2 MONTH
      GROUP BY  rev.field
   ) r ON t.field = rev.field
)
UNION
(
   (
      SELECT    tc.fields
      FROM      tc
      WHERE     tc.field > NOW() - INTERVAL 2 MONTH
      GROUP BY  tc.field
   ) t
   RIGHT JOIN
   (
      SELECT    rev.fields
      FROM      rev
      WHERE     rev.field > NOW() - INTERVAL 2 MONTH
      GROUP BY  rev.field
   ) r ON t.field = rev.field
)
WHERE t.field > 0 OR r.field > 0
ORDER BY t.field

それが役に立てば幸い


助けてくれてありがとう、私はテーブルpstの周りを追加するのを忘れましたSELECT * FROMUNION

これは、を使用してユーザーの生産性を追跡したい人のための実用的なクエリですTRAC

SELECT
   `author` __group__,
   `month-week`,
   changes,
   tickets,
   closed,
   `revisions`
FROM
(
(
   SELECT
      t.`author`,
      t.`month-week`,
      t.changes,
      t.tickets,
      t.closed,
      r.`revisions`
   FROM
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(tc.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(tc.`time`/1000000))
         ) `month-week`,
         COUNT(IF(tc.`field` = "comment",tc.ticket,NULL)) `changes`,
         COUNT(DISTINCT tc.`ticket`) `tickets`,
         COUNT(DISTINCT IF(tc.`field` = "status" AND tc.`newvalue` = "closed",tc.`ticket`,NULL)) `closed`,
         tc.`author`
      FROM `ticket_change` tc
      WHERE
         tc.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         tc.`author`,
         YEARWEEK(FROM_UNIXTIME(tc.`time`/1000000))
      ORDER BY NULL
   ) `t`
   LEFT JOIN
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(r.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(r.`time`/1000000))
         ) `month-week`,
         COUNT(DISTINCT r.`rev`) `revisions`,
         r.`author`
      FROM `revision` r
      WHERE
         r.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         r.`author`,
         YEARWEEK(FROM_UNIXTIME(r.`time`/1000000))
      ORDER BY NULL
   ) `r` ON t.`month-week` = r.`month-week` AND t.`author` = r.`author`
) 
UNION
(
   SELECT
      t.`author`,
      t.`month-week`,
      t.changes,
      t.tickets,
      t.closed,
      r.`revisions`
   FROM
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(tc.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(tc.`time`/1000000))
         ) `month-week`,
         COUNT(IF(tc.`field` = "comment",tc.ticket,NULL)) `changes`,
         COUNT(DISTINCT tc.`ticket`) `tickets`,
         COUNT(DISTINCT IF(tc.`field` = "status" AND tc.`newvalue` = "closed",tc.`ticket`,NULL)) `closed`,
         tc.`author`
      FROM `ticket_change` tc
      WHERE
         tc.`time`> UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         tc.`author`,
         YEARWEEK(FROM_UNIXTIME(tc.`time`/1000000))
      ORDER BY NULL
   ) `t`
   RIGHT JOIN
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(r.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(r.`time`/1000000))
         ) `month-week`,
         COUNT(DISTINCT r.`rev`) `revisions`,
         r.`author`
      FROM `revision` r
      WHERE
         r.`time`> UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         r.`author`,
         YEARWEEK(FROM_UNIXTIME(r.`time`/1000000))
      ORDER BY NULL
   ) `r` ON t.`month-week` = r.`month-week` AND t.`author` = r.`author`
) 
) mtb
WHERE revisions > 0 OR closed > 0
ORDER BY
   `author`,
   `month-week`
4

1 に答える 1

0

答えは質問自体にありますが、他の誰かがそれを必要とする場合に備えてここにあります:

SELECT
   `author` __group__,
   `month-week`,
   changes,
   tickets,
   closed,
   `revisions`
FROM
(
(
   SELECT
      t.`author`,
      t.`month-week`,
      t.changes,
      t.tickets,
      t.closed,
      r.`revisions`
   FROM
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(tc.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(tc.`time`/1000000))
         ) `month-week`,
         COUNT(IF(tc.`field` = "comment",tc.ticket,NULL)) `changes`,
         COUNT(DISTINCT tc.`ticket`) `tickets`,
         COUNT(DISTINCT IF(tc.`field` = "status" AND tc.`newvalue` = "closed",tc.`ticket`,NULL)) `closed`,
         tc.`author`
      FROM `ticket_change` tc
      WHERE
         tc.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         tc.`author`,
         YEARWEEK(FROM_UNIXTIME(tc.`time`/1000000))
      ORDER BY NULL
   ) `t`
   LEFT JOIN
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(r.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(r.`time`/1000000))
         ) `month-week`,
         COUNT(DISTINCT r.`rev`) `revisions`,
         r.`author`
      FROM `revision` r
      WHERE
         r.`time` > UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         r.`author`,
         YEARWEEK(FROM_UNIXTIME(r.`time`/1000000))
      ORDER BY NULL
   ) `r` ON t.`month-week` = r.`month-week` AND t.`author` = r.`author`
) 
UNION
(
   SELECT
      t.`author`,
      t.`month-week`,
      t.changes,
      t.tickets,
      t.closed,
      r.`revisions`
   FROM
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(tc.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(tc.`time`/1000000))
         ) `month-week`,
         COUNT(IF(tc.`field` = "comment",tc.ticket,NULL)) `changes`,
         COUNT(DISTINCT tc.`ticket`) `tickets`,
         COUNT(DISTINCT IF(tc.`field` = "status" AND tc.`newvalue` = "closed",tc.`ticket`,NULL)) `closed`,
         tc.`author`
      FROM `ticket_change` tc
      WHERE
         tc.`time`> UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         tc.`author`,
         YEARWEEK(FROM_UNIXTIME(tc.`time`/1000000))
      ORDER BY NULL
   ) `t`
   RIGHT JOIN
   (
      SELECT
         CONCAT(
            MONTH(FROM_UNIXTIME(r.`time`/1000000)),
            '-',
            WEEK(FROM_UNIXTIME(r.`time`/1000000))
         ) `month-week`,
         COUNT(DISTINCT r.`rev`) `revisions`,
         r.`author`
      FROM `revision` r
      WHERE
         r.`time`> UNIX_TIMESTAMP(NOW()-INTERVAL 2 MONTH)*1000000
      GROUP BY
         r.`author`,
         YEARWEEK(FROM_UNIXTIME(r.`time`/1000000))
      ORDER BY NULL
   ) `r` ON t.`month-week` = r.`month-week` AND t.`author` = r.`author`
) 
) mtb
WHERE revisions > 0 OR closed > 0
ORDER BY
   `author`,
   `month-week`
于 2012-07-08T22:03:09.850 に答える