Help in writing this query

  • I have two tables named account and transaction..

    account{

    String msisdn (PK)

    .....

    }

    transaction{

    String msisdn(FK)

    }

    I need query for , getting msisdn one after the other from the account table, and if no transaction exists for that msisdn in the transaction table then it should return me account.* and true. Or in other way if no transaction available for that msisidn then it should return me true and account values.

    Thanks in advance

  • Please post table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Account table values

    MSISDNOpt_out_dateGroup

    0NULL 0

    1NULL 0

    2NULL 0

    25487NULL 445

    3NULL 0

    4NULL 0

    5NULL 0

    6NULL 0

    7NULL 0

    Transaction table values

    MSISDNamounttrans_date

    0252010

    1252010

    5252010

    7252009

    Results for this year 2010

    MSISDNStatus

    2TRUE

    25487TRUE

    3TRUE

    4TRUE

    6TRUE

    7TRUE

  • You can do this using a LEFT OUTER JOIN

    SELECTA.*, 'True' Status

    FROM[Account] A

    LEFT OUTER JOIN[Transaction] T ON A.msisdn = T.msisdn

    WHERET.msisdn IS NULL


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks Kingston..

    But when i add the year in where condition the results is not as expected.. i am searching for nothing where the year is 2010, when i add that where condition i guess

  • Any suggestions..?

  • Patience, this is a forum, not a chat room. When someone has time they'll help you.

    You want to maybe show us the query that you're using? The one that doesn't return anything?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks for remembering sorry, if it really bothers you.

  • I am really not sure what is required...

    Assuming that you are searching for the rows that do not have transations in year 2010, you can try the following one

    SELECT A.*, 'True' Status

    FROM Account A

    LEFT OUTER JOIN

    (Select * from Transaction where trans_date = 2010) as T ON A.msisdn = T.msisdn

    WHERE T.msisdn IS NULL

    Prashant Bhatt
    Sr Engineer - Application Programming

  • pattamuthu (6/16/2010)


    But when i add the year in where condition the results is not as expected.. i am searching for nothing where the year is 2010, when i add that where condition i guess

    Please provide the query that doesnot return anything and the expected result based on your example.

    Anyways, this is what i think you want..

    SELECT A.*, 'True' Status

    FROM [Account] A

    LEFT OUTER JOIN [Transaction] T ON A.msisdn = T.msisdn AND T.trans_date = 2010

    WHERE T.msisdn IS NULL

    Please take some time to read the article that Gail referred you to. The same is there in my signature as well. A well presented question gets you faster answers. I am sure the time you spend reading that article will be worth it.

    I am also sure you didnot understand the detail of the code i provided. So take some time to Google LEFT OUTER JOIN. If I have some spare time i will give you some links as well. Once you understand the LEFT OUTER JOIN the query will be simple.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks Prashant it works..

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply