Solved

SQL 2005 Group By Query

Posted on 2011-09-12
8
357 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

20 Experts available now in Live!

Get 1:1 Help Now