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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:25:29+00:00 2026-06-04T09:25:29+00:00

I have a query, SELECT E.staffid AS staff SUM(E.Frequency=’Less’) AS lessCount, SUM(E.Frequency=’More’) AS moreCount,

  • 0

I have a query,

SELECT E.staffid AS staff
SUM(E.Frequency='Less') AS lessCount, 
SUM(E.Frequency='More') AS moreCount,  
FROM effort_frequency E 
INNER JOIN ost_staff S ON S.staff_id = E.staffid 
WHERE E.log_date BETWEEN '2012-05-01' AND '2012-05-23' 
GROUP BY E.staffid

I get the below table,

+-------+-----------+-----------+
| Staff | lessCount | moreCount |
+-------+-----------+-----------+
|  1    |    2      |     3     |
|  3    |    1      |     4     |
|  5    |    2      |     3     |
+-------+-----------+-----------+

You can see that the 2 & 4 is missing in the result, There will be no rows with staff_id 2 & 4 in the effort_frequency table for the given date, but I’m trying to join the ost_staff table, so that I can get something like the below table, The ost_staff will contain all the staff id, lets say 1-5.

+-------+-----------+-----------+
| Staff | lessCount | moreCount |
+-------+-----------+-----------+
|  1    |    2      |     3     |
|  2    |    0      |     0     |
|  3    |    1      |     4     |
|  4    |    0      |     0     |
|  5    |    2      |     3     |
+-------+-----------+-----------+

I tried outer joins but its not working, I think I’m making mistake somewhere.

  • 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-04T09:25:31+00:00Added an answer on June 4, 2026 at 9:25 am

    Move the WHERE clause to ON clause, and change the INNER JOIN to RIGHT JOIN. And change the GROUP BY E.staffid to GROUP BY S.staff_id; likewise with SELECT E.staffid, change it to SELECT s.staff_id:

    SELECT S.staff_id AS staff
    
        COALESCE(SUM(E.Frequency='Less'),0) AS lessCount, 
        COALESCE(SUM(E.Frequency='More'),0) AS moreCount,  
    
    FROM effort_frequency E 
    
    RIGHT JOIN ost_staff S 
    ON S.staff_id = E.staffid 
    AND E.log_date BETWEEN '2012-05-01' AND '2012-05-23' 
    
    GROUP BY S.staff_id
    

    If you want to use LEFT JOIN, switch the tables ost_staff and effort_frequency:

    SELECT S.staff_id AS staff
    
        COALESCE(SUM(E.Frequency='Less'),0) AS lessCount, 
        COALESCE(SUM(E.Frequency='More'),0) AS moreCount,  
    
    FROM ost_staff S 
    
    LEFT JOIN effort_frequency E 
    ON S.staff_id = E.staffid 
    AND E.log_date BETWEEN '2012-05-01' AND '2012-05-23' 
    
    GROUP BY s.staff_id
    

    You can also use IFNULL in lieu of COALESCE. IFNULL works only on MySQL though

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

Sidebar

Related Questions

I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have this query: SELECT DISTINCT IM.EDIFICIOS_ID, TI.TITULAR FROM IMPORTACION IM INNER JOIN I_EDIFICIO
I have a query SELECT date, SUM(cimpression) AS cimp, SUM(cclick) AS cclick FROM stdaily
I have this query: SELECT Auctions.ID FROM Auctions INNER JOIN Products ON Auctions.ProductID =
I have this query: SELECT page.id, revision.title, revision.number FROM page INNER JOIN revision ON
Have follow query: SELECT tbl_yaca_sites.`id_site` FROM `tbl_yaca_sites` INNER JOIN `tbl_relation` ON tbl_relation.id_website = tbl_yaca_sites.id_site
I have this query SELECT l.licitatii_id, l.nume, l.data_publicarii, l.data_limita FROM licitatii_ue l INNER JOIN
I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN
I have this query: SELECT (SUM(tblTransaction.AmountPaid) - SUM(tblTransaction.AmountCharged)) AS TenantBalance, tblTransaction.TenantID FROM tblTransaction GROUP
I have a query: SELECT ID FROM requests WHERE date <operator> <expression> I need

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.