Solved

SQL Count

Posted on 2014-11-18
7
386 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 40

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax error [Missing Operator] in query expression 7 29
Sql query 107 27
SQL Field Length for Email Address 3 17
Running Total in Access 4 13
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

920 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

12 Experts available now in Live!

Get 1:1 Help Now