Solved

sql sytnx : include a count for each srp_no in range (2)

Posted on 2013-12-02
4
281 Views
Last Modified: 2013-12-03
Reposting (I was confident that answer given was 100% correct- I was wrong- gave same resultset in original post )

Here is original text:

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


-Answer given below: I changed to inner, full, right, left  and never get .........
14-1 = 6, 15-1 = 5. 31-11 = 0, 31-14=1, 31-15=0)  - If zero value not included.

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

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
0
Comment
Question by:mahpog
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
There is a small problem with the query above. The conditions that control the data in asrp_usage table need to be included into the ON clause, not WHERE clause. The latter restrict the resultset of the whole query, removing the records with 0 count. So, the correct query will be:
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
-- continue to add the conditions to the ON clause, do not use WHERE clause
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
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
mahpog - fyi you can always ask for a clarification in the original question, even if an answer is already accepted.
0
 

Author Closing Comment

by:mahpog
Comment Utility
chaau that fixed the problem. removing the where clause. thx.
0
 

Author Comment

by:mahpog
Comment Utility
jim, thanks again.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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