0

I have a table where the data could be like these below:

CusID | Name | State | PhyAddress

160285 | FYZPZ | NULL | 0

160285 | FYZPZ | NJ | 1

160285 | FYZPZ | NJ | 1

or

CusID | Name | State | PhyAddress

160285 | FYZPZ | NJ xx | 1

or

CusID | Name | State | PhyAddress

160285 | FYZPZ | NULL | 0

160285 | FYZPZ | NJ xx | 0

I need to get the default state for the user. And the logic is:

The result should display the physcial state first (PhyAddress = 1) and if there is no physical address (PhyAddress = 0), then it should display the mailing adress state (PhyAddress = 0). If there are none, the state can be blank.

There are multiple customers in the table and they each can have multiple rows like above and I need to get the default state for each customer.

The resuts needs to show a result of CusID, Name, State

Please help me with the query. TIA

2
  • Do you have the ability to change the structure of the table? Could you split out address into a new table, with a type, separate from name? Commented Sep 30, 2011 at 14:34
  • Nope :( It can break other things in the application. Its a huge application. Commented Sep 30, 2011 at 14:45

4 Answers 4

1

use (EDIT after comments):

SELECT DISTINCT Y.CusID, Y.Name, ISNULL (Y.S1, Y.S0) AS State
FROM
(
SELECT
X.CusID,
X.Name,
( SELECT MAX ( State ) FROM Address T WHERE T.PhyAddress = 1 AND T.CusID = X.CusID ) AS S1,
( SELECT MAX ( State ) FROM Address T WHERE T.PhyAddress = 0 AND T.CusID = X.CusID ) AS S0
FROM Address AS X
) AS Y
Sign up to request clarification or add additional context in comments.

2 Comments

Its giving me Incorrect syntax near the keyword 'SELECT'. for teh 3rd select. and its not recognizing X.table1 in the 2nd and 4th select statements. And the last SO is also not recognized.
what is the real name of your table ?
0

Search the maximum value of PhyAddress:

SELECT CusID, 
       State
FROM table t1
WHERE PhyAddress = 
    (
        SELECT MAX(t2.PhyAddress)
        FROM table t2
        WHERE t2.CusID = t1.CusID
    )

1 Comment

Thank you. PhyAddress is a bit operator and MAX wont work on that one.
0

What have you tried?

let me give you something to get you going

 Select CusID, Name, case when phyaddress = something then something
        when
        when
        ELSE ' ' END as State

from sometable

where something

Comments

0

You could do something like

select CusID, Name, State, PhyAddress from sometable where (CusID, PhyAddress) in
  (select CusID, max(PhyAddress) from sometable)
  group by CusID, Name, State, PhyAddress

3 Comments

What version of MySQL are you using? I just created the table on my local machine with physAddress as a bit and max worked just fine.
Its SQL 2008. Its giving me the error: Operand data type bit is invalid for max operator.
Well, that makes sense. I've re-tagged the question, because we were all answering this as if it was a MySQL question, not a SQL Server 2008 question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.