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 Server

SQL Server

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadminhelp
4 Posts 3 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.
  • A Offline
    A Offline
    Ali Rashid
    wrote on last edited by
    #1

    Hi Guys, I tries to look around but I could not find an answer for this question. I need to run an SQl Query which retuns me the highest Unique Value and the lowest Unique value from the same column. For example. Lets say the column name is itemPrice, and has the following values: 10, 10, 11, 12, 12, 13, 14, 14 respectively. How can I get the 11 and 13 as the result of my query. I have tried DISTINCT, MIN, MAX and HAVING but to no avail. I am using SQL Server 2000. If anyone can help, I would really be grateful. Thanking in Anticiption. Regards Ali

    S B 2 Replies Last reply
    0
    • A Ali Rashid

      Hi Guys, I tries to look around but I could not find an answer for this question. I need to run an SQl Query which retuns me the highest Unique Value and the lowest Unique value from the same column. For example. Lets say the column name is itemPrice, and has the following values: 10, 10, 11, 12, 12, 13, 14, 14 respectively. How can I get the 11 and 13 as the result of my query. I have tried DISTINCT, MIN, MAX and HAVING but to no avail. I am using SQL Server 2000. If anyone can help, I would really be grateful. Thanking in Anticiption. Regards Ali

      S Offline
      S Offline
      Syed Mehroz Alam
      wrote on last edited by
      #2

      Look at my solution below. I have created a temp table to depict your scenario. The query in bold is the one you need. Explanation: I first selected unique values from the table using group by clause, then I just picked the min/max ones

      Create table #MyTable
      (
      ItemPrice int
      )

      insert into #MyTable values(10)
      insert into #MyTable values(10)
      insert into #MyTable values(11)
      insert into #MyTable values(12)
      insert into #MyTable values(12)
      insert into #MyTable values(13)
      insert into #MyTable values(14)
      insert into #MyTable values(14)

      select * from #MyTable

      Select min(ItemPrice) as MinUniquePrice, max(ItemPrice) as MaxUniquePrice
      From
      ( select ItemPrice, Count(*) as ItemPriceCount
      from #Mytable
      group by ItemPrice
      Having Count(*) = 1
      ) as MyTableWithUniqueItemPrices

      drop table #MyTable

      Happy querying, Mehroz

      modified on Sunday, March 30, 2008 5:18 AM

      A 1 Reply Last reply
      0
      • A Ali Rashid

        Hi Guys, I tries to look around but I could not find an answer for this question. I need to run an SQl Query which retuns me the highest Unique Value and the lowest Unique value from the same column. For example. Lets say the column name is itemPrice, and has the following values: 10, 10, 11, 12, 12, 13, 14, 14 respectively. How can I get the 11 and 13 as the result of my query. I have tried DISTINCT, MIN, MAX and HAVING but to no avail. I am using SQL Server 2000. If anyone can help, I would really be grateful. Thanking in Anticiption. Regards Ali

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        select itemPrice from Table1 where (select count(i1.itemprice) from Table1 as i1 where i1.itemprice=Table1.itemprice)=1


        I Love SQL

        1 Reply Last reply
        0
        • S Syed Mehroz Alam

          Look at my solution below. I have created a temp table to depict your scenario. The query in bold is the one you need. Explanation: I first selected unique values from the table using group by clause, then I just picked the min/max ones

          Create table #MyTable
          (
          ItemPrice int
          )

          insert into #MyTable values(10)
          insert into #MyTable values(10)
          insert into #MyTable values(11)
          insert into #MyTable values(12)
          insert into #MyTable values(12)
          insert into #MyTable values(13)
          insert into #MyTable values(14)
          insert into #MyTable values(14)

          select * from #MyTable

          Select min(ItemPrice) as MinUniquePrice, max(ItemPrice) as MaxUniquePrice
          From
          ( select ItemPrice, Count(*) as ItemPriceCount
          from #Mytable
          group by ItemPrice
          Having Count(*) = 1
          ) as MyTableWithUniqueItemPrices

          drop table #MyTable

          Happy querying, Mehroz

          modified on Sunday, March 30, 2008 5:18 AM

          A Offline
          A Offline
          Ali Rashid
          wrote on last edited by
          #4

          Thanks Guys! That was really helpfull. Regards.

          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