Solved

SQL Count

Posted on 2014-11-18
7
381 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
7 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
--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
Comment Utility
;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
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
Ok thanks, gave it a try but it does not return rows when count is zero
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now