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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Not sure how to do this

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 back

Dave
Helixpoint Web Development
http://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 back


these 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 back


quote:
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 back


these 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 MVP
http://visakhm.blogspot.com/





Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-22 : 13:24:06
just do like

SELECT other columns...,
NULLIF(MailingAddress,COALESCE(Primary,PrimaryTransient)) AS MailingAddress
FROM Table


This is the best we can suggest without knowing your table structure or column names

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 same
SELECT Street, Type
FROM dbo.OffenderAddress
WHERE (OffenderID = 2436) AND (IsCurrent = 1)

550 e. madison street Primary
550 e. madison street Mailing
234 S. vine Alternate



quote:
Originally posted by visakh16

just do like

SELECT other columns...,
NULLIF(MailingAddress,COALESCE(Primary,PrimaryTransient)) AS MailingAddress
FROM Table


This is the best we can suggest without knowing your table structure or column names

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

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 same
SELECT Street, Type
FROM dbo.OffenderAddress
WHERE (OffenderID = 2436) AND (IsCurrent = 1)

550 e. madison street Primary
550 e. madison street Mailing
234 S. vine Alternate



quote:
Originally posted by visakh16

just do like

SELECT other columns...,
NULLIF(MailingAddress,COALESCE(Primary,PrimaryTransient)) AS MailingAddress
FROM Table


This is the best we can suggest without knowing your table structure or column names

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Dave
Helixpoint Web Development
http://www.helixpoint.com



Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

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])
UNION
SELECT [PrimaryAddress], 'Primary'
FROM OffenderAddress
WHERE (OffenderAddress.OffenderID = 2436)
AND (OffenderAddress.IsCurrent = 1)
AND (OffenderAddress.Type = 'Primary')
UNION
SELECT [PrimaryTransientAddress], 'Alternate'
FROM OffenderAddress
WHERE (OffenderAddress.OffenderID = 2436)
AND (OffenderAddress.IsCurrent = 1)
AND (OffenderAddress.Type = 'PrimaryTransient')

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-22 : 22:56:39
[code]
SELECT Street, Type
FROM
(
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, Type
FROM dbo.OffenderAddress
WHERE (OffenderID = 2436) AND (IsCurrent = 1)
)t
WHERE Rn=1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

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.aspx

Offer up a guess though:
SELECT
CASE
WHEN
(
AddressMailing.Street = AddressPrimary.Street
OR AddressPrimaryTransient.Street IS NOT NULL
)
THEN ''
ELSE AddressMailing.Street
END AS Street,
Type
FROM
dbo.OffenderAddress AS AddressMailing
LEFT 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'
Go to Top of Page

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 data
SELECT * INTO Testtbl
FROM
(
SELECT 2436 AS OffenderID,'550 e. madison street' AS Street, 'Primary' AS [Type] UNION ALL
SELECT 2436, '550 e. madison street', 'Mailing' UNION ALL
SELECT 2436,'234 S. vine', 'Alternate' UNION ALL
SELECT 1110,'112 Seventh Street', 'Primary' UNION ALL
SELECT 1110,'112 Seventh Street', 'Mailing' UNION ALL
SELECT 1110,'2/365 hayman street', 'Alternate' UNION ALL
SELECT 2343,'', 'Primary' UNION ALL
SELECT 2343,'1134 Kirgiston street', 'Mailing' UNION ALL
SELECT 2343,'1123 Vahmann Street', 'Alternate'
)t

SELECT OffenderID,Street, Type
FROM
(
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

)t
WHERE Rn=1


output
--------------------------------
OffenderID Street Type
1110 112 Seventh Street Primary
1110 2/365 hayman street Alternate
2343 Primary
2343 1123 Vahmann Street Alternate
2343 1134 Kirgiston street Mailing
2436 234 S. vine Alternate
2436 550 e. madison street Primary



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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
Go to Top of Page
   

- Advertisement -