| Author |
Topic |
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2011-09-22 : 13:05:30
|
| I have a table with addresses with 3 types of addresses in it, Primary, Primary Transient, and Mailing. A person can have either a primary address or a Primary Transient address. They will always have a Mailing. I am putting this on a sql report. They do not want to show the mailing address if it is the same as the primary or if they have a primary transient. How do I do a select statement to check if the mailing is the same as the other 2 and if it is, do bring the mailing data backDaveHelixpoint Web Developmenthttp://www.helixpoint.com |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-22 : 13:15:21
|
| They do not want to show the mailing address if it is the same as the primary or if they have a primary transient. How do I do a select statement to check if the mailing is the same as the other 2 and if it is, do bring the mailing data backthese two statements are contradicting. one says they do not want mailing if its same as primary or primary transient and next statement says just opposite. Can you clarify what exactly are you looking at?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2011-09-22 : 13:20:55
|
In the database, they will always have a mailing address, but if it is the same as the other 2 types, I do not want to bring the record backquote: Originally posted by visakh16 They do not want to show the mailing address if it is the same as the primary or if they have a primary transient. How do I do a select statement to check if the mailing is the same as the other 2 and if it is, do bring the mailing data backthese two statements are contradicting. one says they do not want mailing if its same as primary or primary transient and next statement says just opposite. Can you clarify what exactly are you looking at?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
DaveHelixpoint Web Developmenthttp://www.helixpoint.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-22 : 13:24:06
|
just do likeSELECT other columns..., NULLIF(MailingAddress,COALESCE(Primary,PrimaryTransient)) AS MailingAddressFROM Table This is the best we can suggest without knowing your table structure or column names------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2011-09-22 : 13:42:23
|
No understanding this. Here is a select sql and the date that returns. Notice that the Primary and Mailing is the sameSELECT Street, TypeFROM dbo.OffenderAddressWHERE (OffenderID = 2436) AND (IsCurrent = 1)550 e. madison street Primary550 e. madison street Mailing234 S. vine Alternatequote: Originally posted by visakh16 just do likeSELECT other columns..., NULLIF(MailingAddress,COALESCE(Primary,PrimaryTransient)) AS MailingAddressFROM Table This is the best we can suggest without knowing your table structure or column names------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
DaveHelixpoint Web Developmenthttp://www.helixpoint.com |
 |
|
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2011-09-22 : 13:49:01
|
quote: Originally posted by helixpoint Not understanding this. Here is a select sql and the date that returns. Notice that the Primary and Mailing is the sameSELECT Street, TypeFROM dbo.OffenderAddressWHERE (OffenderID = 2436) AND (IsCurrent = 1)550 e. madison street Primary550 e. madison street Mailing234 S. vine Alternatequote: Originally posted by visakh16 just do likeSELECT other columns..., NULLIF(MailingAddress,COALESCE(Primary,PrimaryTransient)) AS MailingAddressFROM Table This is the best we can suggest without knowing your table structure or column names------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
DaveHelixpoint Web Developmenthttp://www.helixpoint.com
DaveHelixpoint Web Developmenthttp://www.helixpoint.com |
 |
|
|
jhatley247
Starting Member
4 Posts |
Posted - 2011-09-22 : 16:54:26
|
| This is not in the least bit elegant but would probably work.SELECT [MailingAddress], 'Mailing' FROM OffenderAddress a WHERE (a.OffenderID = 2436) AND (a.IsCurrent = 1) AND (a.Type = 'Mailing') AND NOT EXISTS (SELECT * FROM OffenderAddress b WHERE a.OffenderID = b.OffenderID AND b.IsCurrent = 1 AND b.type= 'Primary' AND [MailingAddress] = [PrimaryAddress]) AND NOT EXISTS (SELECT * FROM OffenderAddress c WHERE a.OffenderID = c.OffenderID AND c.IsCurrent = 1 AND c.type= 'PrimaryTransient' AND [MailingAddress] = [PrimaryTransientAddress])UNIONSELECT [PrimaryAddress], 'Primary' FROM OffenderAddress WHERE (OffenderAddress.OffenderID = 2436) AND (OffenderAddress.IsCurrent = 1) AND (OffenderAddress.Type = 'Primary')UNIONSELECT [PrimaryTransientAddress], 'Alternate' FROM OffenderAddress WHERE (OffenderAddress.OffenderID = 2436) AND (OffenderAddress.IsCurrent = 1) AND (OffenderAddress.Type = 'PrimaryTransient') |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-22 : 22:56:39
|
| [code]SELECT Street, TypeFROM(SELECT ROW_NUMBER() OVER (PARTITION BY OffenderID,Street ORDER BY CASE WHEN Type='Primary' THEN 1 WHEN Type='Primary Transient' THEN 2 WHEN Type='Mailing' THEN 3 END ASC) AS Rn,Street, TypeFROM dbo.OffenderAddressWHERE (OffenderID = 2436) AND (IsCurrent = 1))tWHERE Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jhatley247
Starting Member
4 Posts |
Posted - 2011-09-23 : 11:49:32
|
| That would still only ever bring one address back. It sounds as though he would like the mailing and primary if they differ and only the primary transient if there is one. Removing this "AND [MailingAddress] = [PrimaryTransientAddress]" from the second subselect in the first query I posted yesterday would do this.Spartan |
 |
|
|
jhatley247
Starting Member
4 Posts |
Posted - 2011-09-23 : 12:23:21
|
| My apologies. It would bring back multiple addresses should they differ. However, it sounds as though he only wants the Primary Transient when one exists which this, I believe, would not accommodate.Spartan |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-09-23 : 12:25:31
|
Can you please post some DDL, DML and expected output?http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxOffer up a guess though:SELECT CASE WHEN ( AddressMailing.Street = AddressPrimary.Street OR AddressPrimaryTransient.Street IS NOT NULL ) THEN '' ELSE AddressMailing.Street END AS Street, TypeFROM dbo.OffenderAddress AS AddressMailingLEFT OUTER JOIN dbo.OffenderAddress AS AddressPrimary ON AddressMailing.OffenderID = AddressPrimary.OffenderID AND AddressPrimary.IsCurrent = 1 AND AddressPrimary.Type = 'Primary'LEFT OUTER JOIN dbo.OffenderAddress AS AddressPrimaryTransient ON AddressPrimary.OffenderID = AddressPrimaryTransient.OffenderID AND AddressPrimaryTransient.IsCurrent = 1 AND AddressPrimaryTransient.Type = 'Primary Transient' WHERE AddressMailing.OffenderID = 2436 AND AddressMailing.IsCurrent = 1 AND AddressMailing.Type = 'Mailing' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-23 : 12:27:11
|
quote: Originally posted by jhatley247 That would still only ever bring one address back. It sounds as though he would like the mailing and primary if they differ and only the primary transient if there is one. Removing this "AND [MailingAddress] = [PrimaryTransientAddress]" from the second subselect in the first query I posted yesterday would do this.Spartan
thats not true see the sample illustration below-- Create the table with test dataSELECT * INTO TesttblFROM(SELECT 2436 AS OffenderID,'550 e. madison street' AS Street, 'Primary' AS [Type] UNION ALLSELECT 2436, '550 e. madison street', 'Mailing' UNION ALLSELECT 2436,'234 S. vine', 'Alternate' UNION ALLSELECT 1110,'112 Seventh Street', 'Primary' UNION ALLSELECT 1110,'112 Seventh Street', 'Mailing' UNION ALLSELECT 1110,'2/365 hayman street', 'Alternate' UNION ALLSELECT 2343,'', 'Primary' UNION ALLSELECT 2343,'1134 Kirgiston street', 'Mailing' UNION ALLSELECT 2343,'1123 Vahmann Street', 'Alternate' )tSELECT OffenderID,Street, TypeFROM(SELECT ROW_NUMBER() OVER (PARTITION BY OffenderID,Street ORDER BY CASE WHEN Type='Primary' THEN 1 WHEN Type='Primary Transient' THEN 2 WHEN Type='Mailing' THEN 3 END ASC) AS Rn,OffenderID,Street, [Type]FROM Testtbl)tWHERE Rn=1output--------------------------------OffenderID Street Type1110 112 Seventh Street Primary1110 2/365 hayman street Alternate2343 Primary2343 1123 Vahmann Street Alternate2343 1134 Kirgiston street Mailing2436 234 S. vine Alternate2436 550 e. madison street Primary ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jhatley247
Starting Member
4 Posts |
Posted - 2011-09-23 : 14:16:22
|
| I recanted my original statement. However, I still believe, based on the wording in the original post, that they only want the alternate address when one exists.Spartan |
 |
|
|
|