Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Select query to determine unique values Expand / Collapse
Author
Message
Posted Friday, November 30, 2012 5:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 5, 2012 7:14 AM
Points: 9, Visits: 27
Can these two pieces of code be combined into one query?
Post #1391232
Posted Friday, November 30, 2012 5:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 1:16 PM
Points: 107, Visits: 511
If it produces the results the results you desire, no.
If not, it may require some tweaking.
It's what you asked for, but is it what you wanted? Only you can answer that one.
Post #1391233
Posted Friday, November 30, 2012 10:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:31 PM
Points: 6,174, Visits: 7,251
Olly, a question before I work this code.

First, you put in 0-length strings, '', instead of NULLS where teh data is missing for PRVID/PrvName. Are these NULL, or actually blanks?

Also, anyone else trying to work the problem, the script provided is a little non-functional due to special quotes usage and no identity fields. Use the following:

--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable

--===== Create the test table with
CREATE TABLE #mytable
(
GrpID INT,
GrpName TEXT,
PrvID INT,
PrvName TEXT,
Spc TEXT,
)

--===== Insert the test data into the test table
INSERT INTO #mytable
(GrpID, GrpName, PrvID, PrvName, Spc)
SELECT '1234','Med Associates',NULL,NULL,'Mixed' UNION ALL
SELECT '1234','Med Associates','6875','Bill Ham','Cardiology' UNION ALL
SELECT '1234','Med Associates','3274','Sarah Jones','Cardiology' UNION ALL
SELECT '8975','Pulm Associates',NULL,NULL,'Mixed' UNION ALL
SELECT '5781','Babies Inc',NULL,NULL,'Pediatrics' UNION ALL
SELECT '6321','ABC Therapy',NULL,NULL,'Mixed' UNION ALL
SELECT '6321','ABC Therapy','4925','Tate Silverman','Physical Therapy' UNION ALL
SELECT '6321','ABC Therapy','3274','Sarah Jones','Occupational Therapy'

SELECT * FROM #mytable




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1391441
Posted Friday, November 30, 2012 4:42 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Try this...

IF OBJECT_ID('TempDB..#mytable') IS NOT NULL
DROP TABLE #mytable

CREATE TABLE #mytable
(
ID INT IDENTITY(1,1) NOT NULL
,GrpID INT NULL
,GrpName VARCHAR(50) NULL
,PrvID INT NULL
,PrvName VARCHAR(50) NULL
,Spc VARCHAR(50) NULL
,PRIMARY KEY (ID)
)

INSERT INTO #mytable (GrpID, GrpName, PrvID, PrvName, Spc)
SELECT '1234','Med Associates','','','Mixed' UNION ALL
SELECT '1234','Med Associates','6875','Bill Ham','Cardiology' UNION ALL
SELECT '1234','Med Associates','3274','Sarah Jones','Cardiology' UNION ALL
SELECT '8975','Pulm Associates','','','Mixed' UNION ALL
SELECT '5781','Babies Inc','','','Pediatrics' UNION ALL
SELECT '6321','ABC Therapy','','','Mixed' UNION ALL
SELECT '6321','ABC Therapy','4925','Tate Silverman','Physical Therapy' UNION ALL
SELECT '6321','ABC Therapy','3274','Sarah Jones','Occupational Therapy'


SELECT DISTINCT
m.GrpID
,m.GrpName
,m.Spc
FROM
#mytable AS m
INNER JOIN
(
SELECT
GrpID
,COUNT(DISTINCT Spc) AS SpcCount
FROM
#mytable AS countspc
WHERE
ID > 0
GROUP BY
GrpID
) Sub1
ON m.GrpID = Sub1.GrpID
WHERE
(Spc = 'Mixed' AND PrvID = 0 AND SpcCount = 1)
OR (Spc <> 'Mixed' AND SpcCount = 2)




 
Post #1391590
Posted Friday, November 30, 2012 6:58 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 5, 2012 7:14 AM
Points: 9, Visits: 27
First off I want to say Thank You. It is my first time posting to a SQL forum and I am still learning, and appreciate everyones patience.

The values contained in the database are blanks.
Post #1391614
Posted Monday, December 3, 2012 5:09 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:28 PM
Points: 1,982, Visits: 2,932
SELECT
GrpID, GrpName,
ISNULL(NULLIF(MAX(CASE WHEN Spc <> 'Mixed' THEN Spc ELSE '' END), ''), 'Mixed') AS Spc
FROM #mytable
GROUP BY
GrpID, GrpName
HAVING
MAX(CASE WHEN Spc = 'Mixed' AND PrvName = '' THEN 1 ELSE 0 END) = 1 AND
COUNT(DISTINCT CASE WHEN Spc <> 'Mixed' THEN Spc END) < 2
ORDER BY
GrpID



SQL DBA,SQL Server MVP('07, '08, '09)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1392194
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse