Solved

SQL Count

Posted on 2014-11-18
7
396 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 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 500 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

735 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