0

これら 2 つのクエリを 1 つに結合し、大きなテーブルで効率的に実行するにはどうすればよいでしょうか?

SELECT  field1, count(1) as requestCount
FROM table1
WHERE date_complete >= '2012-06-12 00:00:00' 
      AND date_complete <= '2012-07-12 23:59:59' 
GROUP BY field1

SELECT field2, count(1) as completeCount
FROM table1
WHERE date_complete >= '2012-06-12 00:00:00' 
      AND date_complete <= '2012-07-12 23:59:59' 
GROUP BY field2

テーブルには、複数のユーザーが関与するプロセスの情報が保持されます。たとえば、最初の人がリクエストを作成し、2 人目がリクエストを完了し、3 人目がリクエストを提出してクローズするとします。

各ユーザーが特定の期間内にリクエスト、完了、提出した数を数えたい

この2つを組み合わせて次のようにしたい

+----------------+--------------+
| field1         | requestCount |
+----------------+--------------+
| PJB            |            1 |
| RFD            |            6 |
| YAS            |            4 |
+----------------+--------------+

+

+---------+---------------+
| field2  | completeCount |
+---------+---------------+
| PJB     |            4  |
| YAS     |            5  |
+---------+---------------+

=

+----------------+--------------+---------------+
| Username       | requestCount | completeCount |
+----------------+--------------+---------------+
| PJB            |            1 |             4 |
| RFD            |            6 |             0 |
| YAS            |            4 |             5 |
+----------------+--------------+---------------+
4

4 に答える 4

1
select  o.USERNAME, a.requestCount , b.completeCount from (SELECT  USERNAME
FROM owner_login_pass )
 as o left join (SELECT field1, count(1) as requestCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59' 
GROUP BY field1) as a on o.USERNAME=a.field1
left join (SELECT field2, count(1) as completeCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59' 
GROUP BY field2) as b on o.USERNAME=b.field2

データを表示する必要があるすべてのユーザーが field1 に含まれていることを確認する必要があります。それ以外の場合は、users テーブルにも参加する必要があります。

于 2012-07-12T16:04:04.247 に答える
1

Just guessing here but I think you need:

SELECT
    u .Username
  , t1.requestCount
  , t2.completeCount
FROM
        ( SELECT field1 AS Username
          FROM table1
          UNION
          SELECT field2
          FROM table2
        ) AS u
    LEFT JOIN
        ( SELECT field1
               , COUNT(*) AS requestCount
          FROM table1
          WHERE date_complete >= '2012-06-12' 
            AND date_complete < '2012-07-13' 
          GROUP BY field1
        ) AS t1  
            ON t1.field1 = u.Username
    LEFT JOIN
        ( SELECT field2
               , COUNT(*) AS completeCount
          FROM table1
          WHERE date_complete >= '2012-06-12' 
            AND date_complete < '2012-07-13' 
          GROUP BY field2 
        ) AS t2
            ON t2.field2 = u.Username   
;

If you have a user table, simply replace the first derived table with user AS u

于 2012-07-12T15:56:51.653 に答える
0

それらを組み合わせることはできないと思いますが(を除くUNION)、次を使用してわずかに改善できます。WHERE date_complete BETWEEN '2012-06-12 00:00:00' AND '2012-07-12 23:59:59'

于 2012-07-12T15:29:42.757 に答える
0

必要ですか :::

Select * from 
(
   SELECT  field1, count(1) as requestCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59'  
GROUP BY field1 ) tab1
left join
(
SELECT fied2, count(1) as completeCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59' 
GROUP BY field2) tab2 on (tab1.field1 = tab2.field2)
于 2012-07-12T15:30:10.290 に答える