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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:54:25+00:00 2026-06-08T08:54:25+00:00

Suppose I have table Tab ( id,, DeptName, Total, … ) There are 4

  • 0

Suppose I have table Tab (id,, DeptName, Total, ...)

There are 4 possible values for Deptname: Dept1, Dept2, Dept3, Dept4.

Then I can issue SQL for grouping like this:

Select DeptName, Totaol = Sum(total) 
from Tab 
group by DeptName

Normally, the result is will have 4 rows based on the value of DeptName:

DeptName Total
Dept1    1234.09
Dept2    234.80
Dept3    34.34
Dept4    614.48

If there is no data for Dept2, the result will only has 3 rows:

Dept1    1234.09
Dept3    34.34
Dept4    614.48

What I want is the result always has 4 rows. If there is no data for Dept2, I want the result like:

Dept1    1234.09
Dept2    0
Dept3    34.34
Dept4    614.48

How to implement this request?

  • 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-08T08:54:28+00:00Added an answer on June 8, 2026 at 8:54 am

    This seems right SQL Fiddle:

    SELECT t2.DeptName, Total = COALESCE(Sum(total),0)
    FROM Tab t1
    RIGHT OUTER JOIN (SELECT 'Dept1' as DeptName UNION ALL
                      SELECT 'Dept2' UNION ALL
                      SELECT 'Dept3' UNION ALL
                      SELECT 'Dept4') t2 ON t2.DeptName = t1.DeptName
    GROUP BY t2.DeptName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table like: Tab(MyDate datetime null, MyIs bit null, ...) Then
Suppose I have database table TABLE1 and there is Column COLUMN1 . The variable
Suppose I have a table in a web page: <table><th id='th1'></th></table> Then I define
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a table with data like: Tab(id, name) 1, 'Name 1' 2,
Possible Duplicate: Concatenate many rows into a single text string? Suppose I have table
Suppose I have table A with a field that can be either 1 or
Suppose I have table Person table Employee, which inherits Person. I want to get
Suppose I have a table Item (Id int Primary Key, Number INT) having records
Suppose I have this table parent | child 1 2 1 3 2 4

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.