Solved

SQL 2005 Group By Query

Posted on 2011-09-12
8
353 Views
Last Modified: 2012-05-12
The below SQL 2005 Group by query works.  The additional solution I am looking for is when a Null appears for the ProName.  I would like a solution that places the word: "unknown" when the group by results in a Null for ProName?   Currently it lists the ProNames and sometimes a Null when the provider is missing from the tables.

SELECT zReports.PhysResp, sum(1) as Volume,
Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle as ProName
FROM Orders
INNER JOIN zReports ON Orders.VisitID = zReports.VisitID
left JOIN Provider ON Provider.ProID = zReports.PhysResp  
WHERE
(zReports.TimeIn2 >= '200701010000' AND zReports.TimeIn2 <= '201108302359' and zReports.FacNo = 1 and Orders.OrderType = 'RAD')
Group by ZReports.PhysResp,Provider.ProLName, Provider.ProFName, Provider.Protitle
Order by Provider.ProLName, Provider.ProFName, Provider.Protitle
0
Comment
Question by:dastaub
8 Comments
 
LVL 6

Expert Comment

by:tangchunfeng
ID: 36526816
SELECT 'Number of Titles', Count(*)
FROM titles
GROUP BY
    CASE
        WHEN price IS NULL THEN 'Unpriced'
        WHEN price < 10 THEN 'Bargain'
        WHEN price BETWEEN 10 and 20 THEN 'Average'
        ELSE 'Gift to impress relatives'
    END
GO
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36526851
It looks like you just need to wrap ProName expression with COALESCE():
COALESCE(Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle, 'Unknown') as ProName

You may also want to account for NULL in any one of the columns also.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36526862
COALESCE(Provider.ProLName + COALESCE(' ' + Provider.ProFName, '') + COALESCE(', ' + Provider.Protitle, ''), 'Unknown') as ProName
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 250 total points
ID: 36526930
you can use isNull(exp, ret_if_null) or case & IsNull combination

IsNull(Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle, 'Unknown') as ProName

or

case
when Provider.ProLName is null or Provider.ProFName is null or Provider.Protitle is null then 'Unknown'
else Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle
end as ProName

or

case
when Provider.ProLName is null and Provider.ProFName is null and Provider.Protitle is null then 'Unknown'
else IsNull(Provider.ProLName,'') + ' ' + IsNull(Provider.ProFName,'') + ', ' + IsNull(Provider.Protitle,'')
end as ProName
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Assisted Solution

by:csense
csense earned 250 total points
ID: 36527016
SELECT            zReports.PhysResp,
                  sum(1) as Volume,
                  CASE WHEN ISNULL(Provider.ProID, '') = '' THEN 'unknown'
                        ELSE Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle
                        END as ProName
FROM
                  Orders INNER JOIN
                  zReports ON Orders.VisitID = zReports.VisitID left JOIN      
                  Provider ON Provider.ProID = zReports.PhysResp  
WHERE
                  (zReports.TimeIn2 >= '200701010000'
                  AND zReports.TimeIn2 <= '201108302359'
                  and zReports.FacNo = 1
                  and Orders.OrderType = 'RAD')
Group by
                  ZReports.PhysResp,
                  Provider.ProLName,
                  Provider.ProFName,
                  Provider.Protitle
Order by
                  Provider.ProLName,
                  Provider.ProFName,
                  Provider.Protitle
0
 
LVL 1

Expert Comment

by:csense
ID: 36527062
might need to switch Provider.ProID = zReports.PhysResp   around.

SELECT		zReports.PhysResp, 
			sum(1) as Volume,
			CASE WHEN ISNULL(Provider.ProID, '') = '' THEN 'unknown'
				ELSE Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle
				END as ProName
FROM 
			Orders INNER JOIN 
			zReports ON Orders.VisitID = zReports.VisitID left JOIN	
			Provider ON zReports.PhysResp = Provider.ProID
WHERE 
			(zReports.TimeIn2 >= '200701010000' 
			AND zReports.TimeIn2 <= '201108302359' 
			and zReports.FacNo = 1 
			and Orders.OrderType = 'RAD')
Group by 
			ZReports.PhysResp,
			Provider.ProLName, 
			Provider.ProFName, 
			Provider.Protitle 
Order by 
			Provider.ProLName, 
			Provider.ProFName, 
			Provider.Protitle

Open in new window

0
 
LVL 8

Expert Comment

by:Ghunaima
ID: 36527243
Try this
SELECT zReports.PhysResp, sum(1) as Volume,
isnull(Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle, 'Unknown') as ProName
FROM Orders 
INNER JOIN zReports ON Orders.VisitID = zReports.VisitID
left JOIN Provider ON Provider.ProID = zReports.PhysResp  
WHERE 
(zReports.TimeIn2 >= '200701010000' AND zReports.TimeIn2 <= '201108302359' and zReports.FacNo = 1 and Orders.OrderType = 'RAD')
Group by ZReports.PhysResp,Provider.ProLName, Provider.ProFName, Provider.Protitle 
Order by Provider.ProLName, Provider.ProFName, Provider.Protitle

Open in new window

0
 

Author Closing Comment

by:dastaub
ID: 36527540
all answer were thoughtful.  Below is solution:

SELECT zReports.PhysResp, sum(1) as Volume,
IsNull(Provider.ProLName + ' ' + Provider.ProFName + ', ' + Provider.Protitle, 'Unknown') as ProName
FROM Orders
INNER JOIN zReports ON Orders.VisitID = zReports.VisitID
left JOIN Provider ON Provider.ProID = zReports.PhysResp  
WHERE
(zReports.TimeIn2 >= '200701010000' AND zReports.TimeIn2 <= '201108302359' and zReports.FacNo = 1 and Orders.OrderType = 'RAD')
Group by ZReports.PhysResp,Provider.ProLName, Provider.ProFName, Provider.Protitle
Order by Provider.ProLName, Provider.ProFName, Provider.Protitle
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

16 Experts available now in Live!

Get 1:1 Help Now