Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Unique Value Question

Author  Topic 

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-09-19 : 09:45:16
I created this view to find the unique providers. It is giving me dups because the provider has mutliple addresses. My client doesn't care what address it pull just as long as it pulls one with the pcs number(pcsa_id1). Can that be done?



SELECT DISTINCT
pcsa_id1, pcsa_lname, pcsa_fname, pcsa_minit, pcsa_degre, pcsa_addr1,
pcsa_addr2, pcsa_city, pcsa_state, CONVERT(VARCHAR(10), (pcs_dob), 101) as pcs_dob,pcs_ssn, pcsl_exid

FROM dbo.pcsa
Inner Join pcs
ON pcsa_id1 = pcs_id1

left Join pcsl
ON pcsa_id1 = pcsl_id1

WHERE
PCSL_WHAT = 'MEDICA' and
pcsa_state IN ('PA', 'DE') and
pcsa_stat = 'ACTIVE' and pcsa_xtyp <> 'ANCI' and pcsa_xtyp <> 'HOSP'
and pcsa_xtyp <> 'RETC' and pcsa_ctl <> 'X' and pcsa_ctl <> 'E'
ORDER BY pcsa_lname


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 09:47:58
GROUP BY pcsa_id1 and apply aggregates like MIN(),MAX() on other columns.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-09-19 : 09:48:44
got sample data?

what if there is no id?

post the ddl along with sample data, and expected results



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2008-09-19 : 09:54:00
Normally you could use a subquerry to get a set of unique IDs first, then bring in other fields AND use TOP on the way to get the first record for each ID.
Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-09-19 : 10:00:14
They are all assigned with an id in our data.

Here is a little sample :)

I want to just pull the 1 pcs_id1 record. I don't want to see multiple addresses. I just want to see one of them.

pcs_id1 L_name F_name Address
00093685 ABARBANEL JACK 815 FREEPORT ROAD
00093685 ABARBANEL JACK 1604 BURTNER ROAD
00093685 ABARBANEL JACK 565 COAL VALLEY ROAD
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 10:02:10
quote:
Originally posted by werhardt

They are all assigned with an id in our data.

Here is a little sample :)

I want to just pull the 1 pcs_id1 record. I don't want to see multiple addresses. I just want to see one of them.

pcs_id1 L_name F_name Address
00093685 ABARBANEL JACK 815 FREEPORT ROAD
00093685 ABARBANEL JACK 1604 BURTNER ROAD
00093685 ABARBANEL JACK 565 COAL VALLEY ROAD


SELECT pcs_id1,L_name,F_name,MAX(Address) AS Address
FROM YourTable
GROUP BY pcs_id1,L_name,F_name
Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-09-19 : 10:35:04
Ok, I did this and it is working good, but some of the address are not matching up it is pulling city from one of the other addresses that it is linked to. Did I do this wrong?

SELECT pcsa_id1,pcsa_lname,pcsa_fname,MAX(pcsa_minit) AS INTL, MAX(pcsa_degre) AS DEG,MAX(pcsa_addr1), MAX(pcsa_city) as city, MAX(pcsa_state)as state,MAX(pcs_dob)as DOB,MAX(pcs_ssn), MAX(pcsl_exid)as pcsl_exid
FROM vw_Unique_Values
GROUP BY pcsa_id1,pcsa_lname,pcsa_fname
order by pcsa_lname
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-09-19 : 10:47:51
No,

Max takes the MAX from a set of data...it doesn't mean that they will all be on the same row...

Do you have an add or update datetime column on the row?

Post the DDL of the table



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-09-19 : 10:54:14
This is the whole view.

SELECT DISTINCT pcsa_id1, pcsa_lname, pcsa_fname, pcsa_minit, pcsa_degre, pcsa_addr1,
pcsa_addr2, pcsa_city, pcsa_state, CONVERT(VARCHAR(10), (pcs_dob), 101) as pcs_dob,pcs_ssn, pcsl_exid

FROM dbo.pcsa
Inner Join pcs
ON pcsa_id1 = pcs_id1

left Join pcsl
ON pcsa_id1 = pcsl_id1

WHERE
PCSL_WHAT = 'MEDICA' and
pcsa_state IN ('PA', 'DE') and
pcsa_stat = 'ACTIVE' and pcsa_xtyp <> 'ANCI' and pcsa_xtyp <> 'HOSP'
and pcsa_xtyp <> 'RETC' and pcsa_ctl <> 'X' and pcsa_ctl <> 'E'

quote:
Originally posted by X002548

No,

Max takes the MAX from a set of data...it doesn't mean that they will all be on the same row...

Do you have an add or update datetime column on the row?

Post the DDL of the table



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-09-19 : 11:13:17
Well, it doesn't look like you have a date or an identity column that can separate the rows, so you would need to make one...cut and paste this sample to see it in action



CREATE TABLE myTable99(pcs_id1 varchar(25), L_name varchar(25), F_name varchar(25)
, Address varchar(25), City varchar(10))
GO

INSERT INTO myTable99(pcs_id1, L_name, F_name, Address, City)

SELECT '00093685', 'ABARBANEL', 'JACK', '815 FREEPORT ROAD', 'Newark' UNION ALL
SELECT '00093685', 'ABARBANEL', 'JACK', '1604 BURTNER ROAD', 'New York' UNION ALL
SELECT '00093685', 'ABARBANEL', 'JACK', '565 COAL VALLEY ROAD', 'New Ark' UNION ALL
SELECT '00093686', 'KMan', 'Brett', '815 FREEPORT ROAD','Boston' UNION ALL
SELECT '00093686', 'KMan', 'Brett', '1604 BURTNER ROAD', 'London' UNION ALL
SELECT '00093686', 'KMan', 'Brett', '565 COAL VALLEY ROAD', 'Paris'
GO

SELECT * FROM myTable99
GO

CREATE TABLE #myTemp99 ( ColOrder int IDENTITY(1,1)
, pcs_id1 varchar(25), L_name varchar(25), F_name varchar(25)
, Address varchar(25), City varchar(10))
GO

INSERT INTO #myTemp99 (pcs_id1, L_name, F_name, Address, City)
SELECT pcs_id1, L_name, F_name, Address, City
FROM myTable99
ORDER BY pcs_id1, City
GO

SELECT * FROM #myTemp99 ORDER BY ColOrder
GO

SELECT *
FROM #myTemp99 o
WHERE EXISTS (SELECT pcs_id1
FROM #myTemp99 i
WHERE i.pcs_id1 = o.pcs_id1
GROUP BY pcs_id1
HAVING MAX(i.ColOrder) = o.ColOrder)
GO

DROP TABLE myTable99, #myTemp99
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2008-09-19 : 11:41:31
This is great. Thank you so much.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-09-19 : 12:09:29
Hey, Pretty Close

http://maps.google.com/maps?hl=en&safe=off&q=King+of+Prussia,+PA&um=1&ie=UTF-8&sa=X&oi=geocode_result&resnum=1&ct=title

If you have many rows of data would be a concern.

Also, Normalizing the data would be helpful



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -