Solved

sql sytnx : include a count for each srp_no in range

Posted on 2013-12-02
5
329 Views
Last Modified: 2013-12-03
I would like to have all srp_no's in the "IN" statement with a 0 for those that have no count.
Right now, only get srp_no's tht meet other conditions.

**********************

select srp_no, isnull(count(srp_no),0) as ttlcnt
from asrp_usage
where convert(char(10),convert(datetime,entry_dt), 112) between '20130101' and '20131231'
and frp_ror = '1'
and bus_type_gc = 'G'
and srp_no IN ('14-1','15-1','31-11','31-14','31-15','31-19','31-20','31-21','31-5','31-6','31-9','GB-01','GB-02','GB-06')
group by srp_no
order by srp_no

Resultset:

srp_no      ttlcnt
14-1        6
15-1        5
31-14       1
31-15       5
31-5        3
31-9        1
0
Comment
Question by:mahpog
[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
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39690555
afaik this can't be done via the IN statement.

The easiest way to pull this off would be to create another table with all of the spr_no values that you currently have in the IN statement, then re-write the T-SQL to do a LEFT JOIN on that table.  Something like...
CREATE TABLE #sprno (no varchar(10))

INSERT INTO #sprno (no) 
VALUES ('14-1'),('15-1'),('31-11'),('31-14'),('31-15'),('31-19'),('31-20'),('31-21'),('31-5'),('31-6'),('31-9'),('GB-01'),('GB-02'),('GB-06')

-- Then your main query will be something like... 
select s.srp_no, isnull(count(u.srp_no),0) as ttlcnt
from #sprno s
   -- LEFT ensures that all #sprno rows are returned
   LEFT JOIN asrp_usage u ON u.spr_no = s.spr_no
where convert(char(10),convert(datetime,u.entry_dt), 112) between '20130101' and '20131231'
and u.frp_ror = '1'
and u.bus_type_gc = 'G'
group by s.srp_no
order by s.srp_no

Open in new window

0
 

Author Comment

by:mahpog
ID: 39690579
thx. will give it a go.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39690640
Thanks for the grade.  Good luck with your project.  -Jim
0
 

Author Comment

by:mahpog
ID: 39691041
did not work, but gave same result as original code.  will not save for future reference.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39692336
Try moving the WHERE clause to the JOIN..
CREATE TABLE #sprno (no varchar(10))

INSERT INTO #sprno (no) 
VALUES ('14-1'),('15-1'),('31-11'),('31-14'),('31-15'),('31-19'),('31-20'),('31-21'),('31-5'),('31-6'),('31-9'),('GB-01'),('GB-02'),('GB-06')

-- Then your main query will be something like... 
select s.srp_no, isnull(count(u.srp_no),0) as ttlcnt
from #sprno s
   -- LEFT ensures that all #sprno rows are returned
   LEFT JOIN asrp_usage u ON u.spr_no = s.spr_no
AND convert(char(10),convert(datetime,u.entry_dt), 112) between '20130101' and '20131231'
AND u.frp_ror = '1'
AND u.bus_type_gc = 'G'
group by s.srp_no
order by s.srp_no

Open in new window

0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query group by data in SQL Server - cursor? 3 64
SQL DATEADD 10 80
Database maintenance 36 101
SQL server client app 3 28
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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