Solved

SQL 2005 Group By Query

Posted on 2011-09-12
8
359 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 34
string fuctions 4 26
Sql Server group by 10 27
Which version of SQL Server Express edition  to install on Windows 2012 server 6 11
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

776 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