Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Counting Values that are in one column of SQL

Posted on 2013-06-21
16
370 Views
Last Modified: 2013-06-21
I have a Column called Quote in a table called NewBusinessLineItem.. There are 4 possible responses, Yes, No, Drop or Self. I would like to count the responses into 4 columns.

Yes Total       No Total        Drop Total      Self Total
   45                16                    8                     47
0
Comment
Question by:Newbi22
16 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 200 total points
ID: 39266579
<air code, rename the obvious>

SELECT
  Count(CASE WHEN Quote='Yes' THEN 1 ELSE 0 END) as 'yes_total',
  Count(CASE WHEN Quote='No' THEN 1 ELSE 0 END) as 'No_total',
  Count(CASE WHEN Quote='Drop' THEN 1 ELSE 0 END) as 'drop_total',
  Count(CASE WHEN Quote='Self' THEN 1 ELSE 0 END) as 'self_total'
FROM NewBusinessLineItem
0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 200 total points
ID: 39266585
try the below one, this is one possible way of achieving your goal, although there is another functionality called pivot which can be employed to do this as well.

select Count(case quote when 'Yes' THEN 1 ELSE 0 END) AS 'YES TOTAL'
,Count(case quote when 'NO' THEN 1 ELSE 0 END) AS 'No TOTAL'
,Count(case quote when 'Drop' THEN 1 ELSE 0 END) AS 'Drop TOTAL'
,Count(case quote when 'Self' THEN 1 ELSE 0 END) AS 'Self TOTAL'
from NewBusinessLineItem

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39266586
btw the above is the SQL Server answer.  Since I see the Access zone in the question, but not whether you are looking for a SQL Server or an Access answer, I'll defer to my Access peers if that's what you're looking for.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 39266589
You need to use "Sum" instead of "Count" in the above code.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39266599
Bah, need more coffee.

Scott is correct, Sum handles the 1's and 0's, whereas Count would count them both.
0
 

Author Comment

by:Newbi22
ID: 39266600
Actually, I have the option to do it in either place. Does not matter. It get imbedded into additional software.



btw the above is the SQL Server answer.  Since I see the Access zone in the question, but not whether you are looking for a SQL Server or an Access answer, I'll defer to my Access peers if that's what you're looking for.
0
 

Author Comment

by:Newbi22
ID: 39266604
Sorry,,,, way New to this. Not a programmer..... change SUM how??
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39266611
Yes.  I'll repeat the T-SQL, Scott should get partial credit in this deal...

SELECT
  Sum(CASE WHEN Quote='Yes' THEN 1 ELSE 0 END) as 'yes_total',
  Sum(CASE WHEN Quote='No' THEN 1 ELSE 0 END) as 'No_total',
  Sum(CASE WHEN Quote='Drop' THEN 1 ELSE 0 END) as 'drop_total',
  Sum(CASE WHEN Quote='Self' THEN 1 ELSE 0 END) as 'self_total'
FROM NewBusinessLineItem

>Actually, I have the option to do it in either place.
Do it in SQL then.
0
 

Author Comment

by:Newbi22
ID: 39266659
That is WORKING !!!! YEA! So how do I split the credit
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39266669
Good deal.

<my opinion>

Equally between the first two comments, which were correct except for count/sum and a minute a part, and ScottPletcher's comment with the Sum catch, would be fine.
0
 

Author Comment

by:Newbi22
ID: 39266682
I did forget to ask, how to do a column that has the total in it. is it something like this...

 Sum(CASE WHEN Quote <> " " THEN 1 ELSE 0 END) as 'Total Quoted'
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39266700
Just add it to the SELECT clause.
Keep in mind if there is any value other than Yes, No, Drop, or Self (like 'Banana') then the total will not match the sum of the rest.

SELECT
  Sum(CASE WHEN Quote='Yes' THEN 1 ELSE 0 END) as 'yes_total',
  Sum(CASE WHEN Quote='No' THEN 1 ELSE 0 END) as 'No_total',
  Sum(CASE WHEN Quote='Drop' THEN 1 ELSE 0 END) as 'drop_total',
  Sum(CASE WHEN Quote='Self' THEN 1 ELSE 0 END) as 'self_total',
  Sum(CASE WHEN Quote <> " " THEN 1 ELSE 0 END) as 'Total Quoted'
FROM NewBusinessLineItem
0
 

Author Comment

by:Newbi22
ID: 39266706
Thanks Jim, but the field selector up front is a drop down list maintained by me, so no other choices......
0
 

Author Closing Comment

by:Newbi22
ID: 39266711
AWESOME....Hope I allocated appropriately.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39266721
Close enough.  Thanks for the split.  Good luck with your project.  -Jim
0
 
LVL 32

Expert Comment

by:awking00
ID: 39266733
Just add count(*) as 'Total_quoted'
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question