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 6963097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:44:44+00:00 2026-05-27T15:44:44+00:00

I am trying to generate a list derived from two tables. The end result

  • 0

I am trying to generate a list derived from two tables. The end result should be:

Date     A    OHS     OMSN    OMSS
date1    1     1        2       3
date2    4     5        6       7
date...   ..    ..      ..      ..

The date ranges are generated off of a calendare table I generated and are being right joined so they are included even if the count for the other buildings are 0. Under each building there is a count that should simply count the workorders for each day. Here is the query I have so far:

Select calendar.datefield As 'Date',
  Count(FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d')) As 'Count'
From workorders Right Join
  calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
      calendar.datefield)
Where calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d')
Group By calendar.datefield

This query gives me the total workorders for all locations for each day on a specific range. Including this is fine but I want to have 3 more columns that are filtered by each specific loction. If I add the building name like this:

Select calendar.datefield As 'Date', workorders.location,
  Count(FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d')) As 'Count'
From workorders Right Join
  calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
      calendar.datefield)
Where calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d')
Group By calendar.datefield, workorders.location

Then the table gives me the data i need but not the columns I am looking for. The data will be somethine lie this:

2011-09-08  OHS     27
2011-09-09  OHS     24
2011-09-10  NULL    0
2011-09-11  NULL    0
2011-09-12  OHS     23
2011-09-13  OHS     18
2011-09-13  OMS-N   1
2011-09-14  OHS     20

I want to pull from this query by selecting the date, then the numbers where it is OHS, then the numbers where it is OMSN, then the numbers where it is OMSS etc…

There has to be an easier way to do this and I have literally worked tall day trying to piece this query together.

I hope someone can help. I learned mysql on the fly over the past few months and any help is much appreciated.

__ FURTHER REVIEW:

SELECT calendar.datefield, tOHS.Count AS 'OHS'
FROM calendar
LEFT JOIN (
SELTCT workorders.school, Count( workorders.dateSubmitted ) AS 'Count', FROM_UNIXTIME( workorders.dateSubmitted, '%Y-%m-%d' ) AS test
FROM workorders
RIGHT JOIN calendar ON ( FROM_UNIXTIME( workorders.dateSubmitted, '%Y-%m-%d' ) = calendar.datefield )
WHERE calendar.datefield
BETWEEN '2011-08-30'
AND date_format( now( ) , '%Y-%m-%d' )
AND School = 'OHS'
GROUP BY calendar.datefield, workorders.school
)tOHS ON calendar.datefield = tOHS.test
WHERE calendar.datefield
BETWEEN '2011-08-30'
AND date_format( now( ) , '%Y-%m-%d' )

This returns the dates and the daily count for the first location OHS. WHEN I try this:

select calendar.datefield, tOHS.Count as 'OHS'
from calendar
LEFT JOIN 
(
Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as test
        From workorders Right Join
      calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
          calendar.datefield)
    Where calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d') AND School = 'OHS'
    Group By calendar.datefield,workorders.school
    ) tOHS
 on calendar.datefield = tOHS.test
 LEFT JOIN
 (
Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as test
        From workorders Right Join
      calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
          calendar.datefield)
    Where calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d') AND School = 'OMS-S'
    Group By calendar.datefield,workorders.school
    ) tOMSS
 on calendar.datefield = tOHS.test
 WHERE calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d')

It breaks the query all together. I see the same date and the same value repeated for all columns. Am I using the wrong join?

  • 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-05-27T15:44:44+00:00Added an answer on May 27, 2026 at 3:44 pm

    After several hours of research and rethinking I determined how to do this. Basically, we need to be selecting each field (calendar.datefield, the count for location 1, the count for locaiton 2, the count for location 3…)

    To get the count I did a LEFT OUTER JOIN of the subquery that is right joined to the calendar. This makes the subquery return the count for each individual day (grouped by each calendar day) and this gets left outer joined (as mentioned a few seconds ago) to the calendar.datefield which is then filtered by the where clause between dates a and b.

    Then rinse and repeat that left outer join of each subquery. I am not an SQL master and I never went to school for this so it may not be optimized or perfect but it works and works fairly quickly. I am just glad to have figured it out.

        select calendar.datefield, (ifnull(tOHS.Count,0)+ifnull(tOMSS.Count,0)+ifnull(tOMSN.Count,0)) as 'Total', ifnull(tOHS.Count,0) as 'OHS', ifnull(tOMSS.Count,0) as 'OMS-S', ifnull(tOMSN.Count,0) as 'OMS-N'
    from calendar
    
    LEFT OUTER JOIN 
    (
    Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as 'Date'
            From workorders Right Join
          calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
              calendar.datefield)
        Where School = 'OHS'
        Group By calendar.datefield,workorders.school
        ) tOHS
     on calendar.datefield = tOHS.Date
    
     LEFT OUTER JOIN 
    (
    Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as 'Date'
            From workorders Right Join
          calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
              calendar.datefield)
        Where School = 'OMS-S'
        Group By calendar.datefield,workorders.school
        ) tOMSS
     on calendar.datefield = tOMSS.Date
    
     LEFT OUTER JOIN 
    (
    Select workorders.school, Count(workorders.dateSubmitted) As 'Count', FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') as 'Date'
            From workorders Right Join
          calendar On (FROM_UNIXTIME(workorders.dateSubmitted, '%Y-%m-%d') =
              calendar.datefield)
        Where School = 'OMS-N'
        Group By calendar.datefield,workorders.school
        ) tOMSN
     on calendar.datefield = tOMSN.Date
    
    
     WHERE calendar.datefield Between '2011-08-30' And date_format(now(), '%Y-%m-%d')
    

    This returns dates from range a to b and gives total for 3 buildings and each building. I have 4 buildings but it is easy to add as many as you want by copy and pasting each LEFT OUTER JOIN down to the on clause and changing the variables then adding a new select for that count.

    Wow this stuff can get confusing. hope it helps someone someday 🙂

    Bil

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate a multi level nested html list from xml/xsl. For example,
I am trying to generate a list of numbers from -1 to 1 in
I am trying to generate a list of emails from the phone's contacts but
I'm trying to generate a list dynamically from database. The results can be retrieved,
I am trying to generate a list of all possible number combinations within a
While trying to generate classes from a xsd, i got this error: java.lang.IllegalArgumentException: Illegal
I'm trying to generate customized xml files from a template xml file in python.
I'm trying to generate a list in scala according to the formula: for n
i am trying to generate the following list in asp.net? <div class=items> <!-- 1-5
I'm trying to generate a new SharePoint list item directly using SQL server. What's

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.