SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select query to determine unique values


Select query to determine unique values

Author
Message
ollyjolly
ollyjolly
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 27
Can these two pieces of code be combined into one query?
lnardozi 61862
lnardozi 61862
Old Hand
Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)Old Hand (363 reputation)

Group: General Forum Members
Points: 363 Visits: 617
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.
Evil Kraig F
Evil Kraig F
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19665 Visits: 7660
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
Steven Willis
Steven Willis
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1929 Visits: 1721
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)






 
ollyjolly
ollyjolly
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 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.
ScottPletcher
ScottPletcher
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18792 Visits: 7402

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) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search