Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Count

Posted on 2014-11-18
7
Medium Priority
?
403 Views
Last Modified: 2014-12-24
Hello,

I have table that contains

total      type
66      1
1560      3
6      5
130      2

The total column is derrived from a count / group by.

Basically I wish to ammend this so that it only counts certains types, how would I accomplish this for say type 1,3,4 so it would return

total      type
66      1
1560      3
0      4

Notice that it should return 0 where it does not find any, not miss the row off
0
Comment
Question by:andyw27
[X]
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
7 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40450392
--put 1,3,4 into a temporary table.
select 1 type into #types

insert into #types select 3
insert into #types select 4


select isnull(total, 0) total, t.type
From table t
left join #types t2 on t.type = t2.Type
where t.type in (select type from #types)
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40450405
;WITH cnt (cType )  AS (
 SELECT 1  
 UNION ALL
 SELECT 3
 UNION ALL
 SELECT 4   )


 SELECT cType, SUM( ISNULL(total,0) )  AS Total  
 FROM UrTable u
 RIGHT JOIN cnt c ON u.typpe = c.cType  
 GROUP BY cType
0
 

Author Comment

by:andyw27
ID: 40450424
should note the total is not a column, but is derived from an count command, here is the query:

SELECT
COUNT(object_id) as total,
object_type
FROM
object_audit_info
WHERE
action_type = '1' and person_id = '123'
group by object_type

Open in new window

0
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40450593
It's usually helpful if you post that kind of stuff in your question to begin with.

Here is my modified query:
select 1 type into #types
insert into #types select 3
insert into #types select 4


SELECT
isnull(COUNT(object_id), 0) as total,
object_type
FROM
object_audit_info i
left join #types t2 on i.object_type = t2.Type
WHERE
action_type = '1' and person_id = '123' and i.object_type in (select type from #types)
group by object_type

Open in new window

0
 

Author Comment

by:andyw27
ID: 40451744
Ok thanks, gave it a try but it does not return rows when count is zero
0
 
LVL 41

Expert Comment

by:Sharath
ID: 40456813
try this.
 ;WITH CTE
     AS (SELECT 1 object_type
         UNION ALL
         SELECT 3
         UNION ALL
         SELECT 4)
SELECT ISNULL(t2.total, 0) total,t1.object_type
  FROM CTE t1
       LEFT JOIN (SELECT Count(object_id) AS total,object_type
                    FROM object_audit_info
                   WHERE action_type = '1'
                     AND person_id = '123'
                   GROUP BY object_type) t2
              ON t1.object_type = t2.object_type  

Open in new window

0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 40457784
That should be a right join . . . my apologies.

Try the following:
select 1 type into #types
insert into #types select 3
insert into #types select 4

SELECT
isnull(COUNT(object_id), 0) as total,
object_type
FROM
object_audit_info i
RIGHT join #types t2 on i.object_type = t2.Type
WHERE
action_type = '1' and person_id = '123' 
group by object_type

Open in new window

0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

715 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