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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL query required

SQL query required

Scheduled Pinned Locked Moved Database
databasequestiontutorial
4 Posts 4 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.
  • I Offline
    I Offline
    Imtiaz Murtaza
    wrote on last edited by
    #1

    Friends, I got a table in which records are in the following manner:

    category product
    ======== =======
    clothes jeans
    clothes shirts
    clothes ties
    weapons Machine Gun
    weapons Missile
    ColdDrinks Pepsi
    ColdDrinks Coca Cola

    Now i want to select a result from the above table in such a manner that that the first record of each "category" gets returned. For example, select result should be:

    category product
    ======== =======
    clothes jeans
    weapons Machine Gun
    ColdDrinks Pepsi

    How can i do so ? Imtiaz

    W L I 3 Replies Last reply
    0
    • I Imtiaz Murtaza

      Friends, I got a table in which records are in the following manner:

      category product
      ======== =======
      clothes jeans
      clothes shirts
      clothes ties
      weapons Machine Gun
      weapons Missile
      ColdDrinks Pepsi
      ColdDrinks Coca Cola

      Now i want to select a result from the above table in such a manner that that the first record of each "category" gets returned. For example, select result should be:

      category product
      ======== =======
      clothes jeans
      weapons Machine Gun
      ColdDrinks Pepsi

      How can i do so ? Imtiaz

      W Offline
      W Offline
      wgdesigner
      wrote on last edited by
      #2

      Dear Friend, I don;t know which type of query u want, as ur mind asks.. but i have one query which will give u same result which u want.. select category,product from table_name where product in('jeans','machine gun','pepsi'); from taht u can view same result as u want, Raj Khatri

      1 Reply Last reply
      0
      • I Imtiaz Murtaza

        Friends, I got a table in which records are in the following manner:

        category product
        ======== =======
        clothes jeans
        clothes shirts
        clothes ties
        weapons Machine Gun
        weapons Missile
        ColdDrinks Pepsi
        ColdDrinks Coca Cola

        Now i want to select a result from the above table in such a manner that that the first record of each "category" gets returned. For example, select result should be:

        category product
        ======== =======
        clothes jeans
        weapons Machine Gun
        ColdDrinks Pepsi

        How can i do so ? Imtiaz

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Try the following SQL statement: SELECT Category, FIRST(Product) AS FirstOfProduct FROM YourTable GROUP BY Category; You may substitue FIRST() with either MIN() or MAX() if FIRST() is not supported. Hope that works... Go2Canada

        1 Reply Last reply
        0
        • I Imtiaz Murtaza

          Friends, I got a table in which records are in the following manner:

          category product
          ======== =======
          clothes jeans
          clothes shirts
          clothes ties
          weapons Machine Gun
          weapons Missile
          ColdDrinks Pepsi
          ColdDrinks Coca Cola

          Now i want to select a result from the above table in such a manner that that the first record of each "category" gets returned. For example, select result should be:

          category product
          ======== =======
          clothes jeans
          weapons Machine Gun
          ColdDrinks Pepsi

          How can i do so ? Imtiaz

          I Offline
          I Offline
          irshu
          wrote on last edited by
          #4

          Your solution is here it works fine for your problem. select distinct(category), (select top 1 product from myTable b where b.category = a.category) as 'Product' from myTable a

          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