Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:45:24+00:00 2026-06-09T20:45:24+00:00

So here is my very long SQL query. Basically, I’ve got 2 school levels,

  • 0

So here is my very long SQL query.

Basically, I’ve got 2 school levels, JC1 & JC2 and I am counting the fields as you see below for each level for the current date and the previous date.

This is my original subquery for today, and I use another sub query with a different date.

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;

It will return

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

Now for some dates, I may not have both JC1 & JC2 being returned. (Eg. As above 2012-05-25 I got both JC1 & JC2, for the day before, I only got JC2 because there is no JC1 data)

So this is why I did not use JOINs to help me, or maybe because I don’t really know how to use JOINs properly.

For my SQL query below,

The subqueries return this (example for subquery 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

So if you think you can find a way to help me shorten my query, make it more efficient, etc. I’m forever grateful and will be able to learn something along the way. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:45:25+00:00Added an answer on June 9, 2026 at 8:45 pm

    Try this:

    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;
    

    An alternate way might be this:

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that takes a very long time to run on
I have an XML file that is very long, but here is a shot
Here is a very and straight forward question: How long will it take to
I'm a long time developer but not very experienced with DNS. Here's my problem:
I Have a Big Linq-to-entity Query and it's seem to be very long to
I made a research and all posts here are very blury regarding this issue.
Here is a very simple program: a = [[]]*3 print str(a) a[0].append(1) a[1].append(2) a[2].append(3)
Here's a very simple thing I'd like to do: when the pointer is within
Here is a very basic class for handling sessions on App Engine: Lightweight implementation
Here's a very quick example for my problem: http://img19.imageshack.us/img19/3575/workertool.png (I'm not allowed to post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.