0

これが私の非常に長い SQL クエリです。

基本的に、私は 2 つの学校レベル、JC1 と JC2 を持っており、現在の日付と前の日付の各レベルについて以下に示すようにフィールドを数えています。

これは今日のオリジナルのサブクエリで、別の日付で別のサブクエリを使用しています。

SELECT
level,
COUNT(studentid) AS total,
SUM(leader1 <> '' OR leader2 <> '') AS leaders,
SUM(scholarship <> '') AS scholarship,
SUM(pegasus <> '') as pegasus
FROM `laterec-students` 
WHERE latetime > '2012-05-25 00:00:00' 
GROUP BY level;

戻ってきます

level | total | leaders | scholarship |pegasus
  JC1 |  28   |    7    |      0      |   2
  JC2 |  14   |    6    |      0      |   3

一部の日付では、JC1 と JC2 の両方が返されない場合があります。(例: 上記のように 2012-05-25 は JC1 と JC2 の両方を取得しました。前日は、JC1 のデータがないため、JC2 のみを取得しました)

これが、私が JOIN を使用して役に立たなかった理由です。または、JOIN を適切に使用する方法を本当に知らないためかもしれません。

以下の私の SQL クエリでは、

サブクエリはこれを返します (サブクエリ tjc1 の例)

total | leaders | scholarship |pegasus
 28   |    7    |      0      |   2


SELECT

SUM(tjc1.total) AS JC1total,
SUM(yjc1.ytotal) AS JC1ytotal,
SUM(tjc1.leaders) AS JC1leaders,
SUM(yjc1.yleaders) AS JC1yleaders,
SUM(tjc1.scholarship) AS JC1scholarship,
SUM(yjc1.yscholarship) AS JC1yscholarship,
SUM(tjc1.pegasus) AS JC1pegasus,
SUM(yjc1.ypegasus) AS JC1ypegasus,

SUM(tjc2.total) AS JC2total,
SUM(yjc2.ytotal) AS JC2ytotal,
SUM(tjc2.leaders) AS JC2leaders,
SUM(yjc2.yleaders) AS JC2yleaders,
SUM(tjc2.scholarship) AS JC2scholarship,
SUM(yjc2.yscholarship) AS JC2yscholarship,
SUM(tjc2.pegasus) AS JC2pegasus,
SUM(yjc2.ypegasus) AS JC2ypegasus

FROM

(
SELECT
COUNT(studentid) AS total,
SUM(leader1 <> '' OR leader2 <> '') AS leaders,
SUM(scholarship <> '') AS scholarship,
SUM(pegasus <> '') as pegasus
FROM `laterec-students` 
WHERE latetime > '2012-05-25 00:00:00' 
AND level = 'JC1'
) tjc1,
(
SELECT
COUNT(studentid) AS ytotal,
SUM(leader1<>'' or leader2<>'') AS yleaders,
SUM(scholarship<>'') AS yscholarship,
SUM(pegasus<>'') as ypegasus
FROM `laterec-students` 
WHERE latetime BETWEEN '2012-05-24 00:00:00' AND '2012-05-24 23:59:59'
AND level = 'JC1'
) yjc1,
(
SELECT
COUNT(studentid) AS total,
SUM(leader1 <> '' OR leader2 <> '') AS leaders,
SUM(scholarship <> '') AS scholarship,
SUM(pegasus <> '') as pegasus
FROM `laterec-students` 
WHERE latetime > '2012-05-25 00:00:00' 
AND level = 'JC2'
) tjc2,
(
SELECT
COUNT(studentid) AS ytotal,
SUM(leader1<>'' or leader2<>'') AS yleaders,
SUM(scholarship<>'') AS yscholarship,
SUM(pegasus<>'') as ypegasus
FROM `laterec-students` 
WHERE latetime BETWEEN '2012-05-24 00:00:00' AND '2012-05-24 23:59:59'
AND level = 'JC2'
) yjc2

したがって、クエリを短くしたり、より効率的にしたりする方法を見つけることができると思われる場合は、永遠に感謝しており、途中で何かを学ぶことができます. ありがとう!

4

2 に答える 2

1

これを試して:

select the_type, 
       level, 
       sum(total), 
       sum(leaders), 
       sum(scholarship), 
       sum(pegasus)

FROM
(
  (
  SELECT 
  't' the_type, 
  level,
  COUNT(studentid) AS total,
    SUM(leader1 <> '' OR leader2 <> '') AS leaders,
    SUM(scholarship <> '') AS scholarship,
    SUM(pegasus <> '') as pegasus
   FROM `laterec-students` 
  WHERE latetime > '2012-05-25 00:00:00' 
  AND level in('JC1', 'JC2')
  GROUP BY the_type, level
  ) 
UNION ALL
  (
  SELECT
   'y' the_type,
    level,
    COUNT(studentid) AS ytotal,
    SUM(leader1<>'' or leader2<>'') AS yleaders,
    SUM(scholarship<>'') AS yscholarship,
    SUM(pegasus<>'') as ypegasus
   FROM `laterec-students` 
  WHERE latetime BETWEEN '2012-05-24 00:00:00' AND '2012-05-24 23:59:59'
  AND level in('JC1', 'JC2')
  GROUP BY the_type, level
  )
) AS the_union
GROUP BY the_type, level;

別の方法は次のとおりです。

SELECT
date(latetime) the_date,
level,
COUNT(studentid) AS total,
SUM(leader1<>'' or leader2<>'') AS leaders,
SUM(scholarship<>'') AS scholarship,
SUM(pegasus<>'') as pegasus
FROM `laterec-students` 
WHERE latetime between '2012-05-24 00:00:00' AND '2012-05-25 23:59:59'
AND level in('JC1', 'JC2')
group by the_date, level;
于 2012-08-20T00:54:37.987 に答える
0

たぶん、leader1 <>''またはleader2<>''(たとえば)の行だけを合計するために使用したのと同じトリックを使用して、レベルと日付の条件を追加して、このようなことを行うことができます

SELECT
SUM(level = 'JC1' AND latetime > '2012-05-25 00:00:00' ) AS total_TJC1,
SUM(leader1 <> '' OR leader2 <> '' AND level = 'JC1' AND latetime > '2012-05-25 00:00:00') AS leaders_TJC1,
SUM(scholarship <> '' AND level = 'JC1' AND latetime > '2012-05-25 00:00:00' ) AS scholarship_TJC1,
SUM(pegasus <> '' AND level = 'JC1' AND latetime > '2012-05-25 00:00:00' ) as pegasus_TJC1
FROM `laterec-students` 
于 2012-08-20T03:19:06.003 に答える