Solved

SQL 2005 Group By Query

Posted on 2011-09-12
8
361 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
[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
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 60

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 60

Expert Comment

by:Kevin Cross
ID: 36526862
COALESCE(Provider.ProLName + COALESCE(' ' + Provider.ProFName, '') + COALESCE(', ' + Provider.Protitle, ''), 'Unknown') as ProName
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 53

Accepted Solution

by:
Huseyin KAHRAMAN 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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

751 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