how to retrieve data in sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mauj
    New Member
    • Apr 2010
    • 2

    how to retrieve data in sql

    Code:
    select * from 
      ( select trans_mstr.*,third_party_transfer.acc_no_to from trans_mstr join third_party_transfer on trans_mstr.acc_no=third_party_transfer.acc_no_from) 
      where acc_no=12200101
    when i run this querry i get an error (incorrect syntax near keyword 'where')

    table: trans_mstr
    Code:
    trans_no       acc_no          date             particular  wd_dt   amount      balance
    
         
    1   12200101   3/31/2010 11:11:37 PM   deposit     d	5000.00	5000.00
    2   12200102   2/23/2010 12:00:00 AM   deposit     d	5000.00	5000.00
    3   12200103   3/31/2010 11:11:37 PM   deposit     d	5000.00	5000.00
    18  12200101   4/5/2010 6:10:37 PM     transfered   w	100.00	4900.00
    19  12200102   4/5/2010 6:10:40 PM     received     d	100.00	5100.00
    20  12200101   4/5/2010 6:12:48 PM     transfered  w	100.00	4800.00
    21  12200103   4/5/2010 6:12:49 PM     received    d	100.00	5100.00
    22  12200102   4/5/2010 6:13:39 PM     transfered  w	100.00	5000.00
    23  12200103   4/5/2010 6:13:39 PM     received    d	100.00	5200.00
    table2 : third_party_tra nsfer
    Code:
    trans_no    acc_no_from     acc_no_to        date                amount
                
    9	 12200101	12200102	4/5/2010 6:10:29 PM	100.00
    10	12200101	12200103	4/5/2010 6:12:48 PM	100.00
    11	12200102	12200103	4/5/2010 6:13:39 PM	100.00
    12	12200102	12200101	4/5/2010 6:13:48 PM	100.00
    13	12200103	12200101	4/5/2010 6:14:22 PM	100.00
    14	12200103	12200102	4/5/2010 6:14:33 PM	100.00

    i want an output like
    Code:
    trans_no       acc_no   acc_no_to          particular   amount      balance   
    
    1      12200101                       deposit           5000.00   5000.00
    18     12200101       12200102        transfered        100.00    4900.00
    20     12200101       12200103        transfered        100.00    4800.00
    Last edited by Frinavale; Apr 7 '10, 08:21 PM. Reason: Please post code in [code] ... [/code] tags. Moved to SQL Server from ASP.NET and added code tags.
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    when you wrap a query up as a sub query you must give the subquery an alias name. You haven't


    The bolded A in the code below is the alias name
    It can be anything you like
    [code=sql]
    select *
    from
    ( select trans_mstr.*,th ird_party_trans fer.acc_no_to
    from trans_mstr
    join third_party_tra nsfer on trans_mstr.acc_ no=third_party_ transfer.acc_no _from
    ) A
    where acc_no=12200101
    [/code]



    actually you didn't need to use a subquery
    [code=sql]
    select trans_mstr.*,th ird_party_trans fer.acc_no_to
    from trans_mstr
    join third_party_tra nsfer on trans_mstr.acc_ no=third_party_ transfer.acc_no _from
    where acc_no=12200101
    [/code]

    Comment

    Working...