Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. New column retrival on Sql

New column retrival on Sql

Scheduled Pinned Locked Moved Database
databaseagentic-aihelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tash18
    wrote on last edited by
    #1

    Hi guys... Actually i have a table called products which has 4 products in it say for example pepsi, miranda, coke and fanta. The table has two columns product id and productname as follows: 1 pepsi 2 miranda 3 coke 4 fanta now i have another table called agent, which has 3 columns such as agentid, region, productid. Now each agentid has a region and productid assigned to it as follows: 1001 DXB 1 1001 AJM 4 1002 ABC 2 1003 XYZ 3 1003 DEF 4 and so on. now in my application i need to make use of a query which gives me a result of all the product name with status for any particular agent id, for example: for agentid 1001 the result should be

    productname status

    pepsi true
    miranda false
    coke false
    fanta true

    the query i tried is :

    select a.productname, status = case b.product_id
    when 1 then 'True' else 'False'
    when 2 then 'True' else 'False'
    when 3 then 'True' else 'False'
    when 4 then 'True' else 'False' end
    from products a, agent b where a.productid = b.productid and agentid ='1001'

    but doest work... any help is appreciated...

    G 1 Reply Last reply
    0
    • T Tash18

      Hi guys... Actually i have a table called products which has 4 products in it say for example pepsi, miranda, coke and fanta. The table has two columns product id and productname as follows: 1 pepsi 2 miranda 3 coke 4 fanta now i have another table called agent, which has 3 columns such as agentid, region, productid. Now each agentid has a region and productid assigned to it as follows: 1001 DXB 1 1001 AJM 4 1002 ABC 2 1003 XYZ 3 1003 DEF 4 and so on. now in my application i need to make use of a query which gives me a result of all the product name with status for any particular agent id, for example: for agentid 1001 the result should be

      productname status

      pepsi true
      miranda false
      coke false
      fanta true

      the query i tried is :

      select a.productname, status = case b.product_id
      when 1 then 'True' else 'False'
      when 2 then 'True' else 'False'
      when 3 then 'True' else 'False'
      when 4 then 'True' else 'False' end
      from products a, agent b where a.productid = b.productid and agentid ='1001'

      but doest work... any help is appreciated...

      G Offline
      G Offline
      Goutam Patra
      wrote on last edited by
      #2

      select a.productname,
      case when b.agentid is null then 'False' else 'True' end AS status
      from products a
      left outer join agent b on a.productid = b.productid
      and b.agentid ='1001'

      T 1 Reply Last reply
      0
      • G Goutam Patra

        select a.productname,
        case when b.agentid is null then 'False' else 'True' end AS status
        from products a
        left outer join agent b on a.productid = b.productid
        and b.agentid ='1001'

        T Offline
        T Offline
        Tash18
        wrote on last edited by
        #3

        Thanx a million buddy.. works like a charm.... thanx alot...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups