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.
| 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_exidFROM dbo.pcsa Inner Join pcs ON pcsa_id1 = pcs_id1 left Join pcslON pcsa_id1 = pcsl_id1WHERE 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. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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. |
 |
|
|
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 Address00093685 ABARBANEL JACK 815 FREEPORT ROAD00093685 ABARBANEL JACK 1604 BURTNER ROAD00093685 ABARBANEL JACK 565 COAL VALLEY ROAD |
 |
|
|
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 Address00093685 ABARBANEL JACK 815 FREEPORT ROAD00093685 ABARBANEL JACK 1604 BURTNER ROAD00093685 ABARBANEL JACK 565 COAL VALLEY ROAD
SELECT pcs_id1,L_name,F_name,MAX(Address) AS AddressFROM YourTableGROUP BY pcs_id1,L_name,F_name |
 |
|
|
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_exidFROM vw_Unique_ValuesGROUP BY pcsa_id1,pcsa_lname,pcsa_fnameorder by pcsa_lname |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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_exidFROM dbo.pcsa Inner Join pcs ON pcsa_id1 = pcs_id1 left Join pcslON pcsa_id1 = pcsl_id1WHERE 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 tableBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
|
 |
|
|
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 actionCREATE TABLE myTable99(pcs_id1 varchar(25), L_name varchar(25), F_name varchar(25), Address varchar(25), City varchar(10))GOINSERT INTO myTable99(pcs_id1, L_name, F_name, Address, City)SELECT '00093685', 'ABARBANEL', 'JACK', '815 FREEPORT ROAD', 'Newark' UNION ALLSELECT '00093685', 'ABARBANEL', 'JACK', '1604 BURTNER ROAD', 'New York' UNION ALLSELECT '00093685', 'ABARBANEL', 'JACK', '565 COAL VALLEY ROAD', 'New Ark' UNION ALLSELECT '00093686', 'KMan', 'Brett', '815 FREEPORT ROAD','Boston' UNION ALLSELECT '00093686', 'KMan', 'Brett', '1604 BURTNER ROAD', 'London' UNION ALLSELECT '00093686', 'KMan', 'Brett', '565 COAL VALLEY ROAD', 'Paris'GOSELECT * FROM myTable99GOCREATE 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))GOINSERT 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, CityGOSELECT * FROM #myTemp99 ORDER BY ColOrderGOSELECT * 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) GODROP TABLE myTable99, #myTemp99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-19 : 11:41:31
|
| This is great. Thank you so much. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|