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. Typical Requirement

Typical Requirement

Scheduled Pinned Locked Moved Database
databasetutorial
3 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.
  • S Offline
    S Offline
    Satish Developer
    wrote on last edited by
    #1

    Need Query for this requirement. The data in my table is as follows Zip Blac White green 123 1.76 2.04 1.00 234 3.49 2.39 3.24 Need Columname as output if the column value is > 2 for given zipcode. For example.if i give zip as "123" the query has to find out the value among the columns which is > 2. i.e White If i give zip as "234" then the query has to find out the value among the columns which is > 2. But here all values are > 2. So you have to pick the max value. i.e 3.49 = Blac

    G. Satish

    D B 2 Replies Last reply
    0
    • S Satish Developer

      Need Query for this requirement. The data in my table is as follows Zip Blac White green 123 1.76 2.04 1.00 234 3.49 2.39 3.24 Need Columname as output if the column value is > 2 for given zipcode. For example.if i give zip as "123" the query has to find out the value among the columns which is > 2. i.e White If i give zip as "234" then the query has to find out the value among the columns which is > 2. But here all values are > 2. So you have to pick the max value. i.e 3.49 = Blac

      G. Satish

      D Offline
      D Offline
      DoctorMick
      wrote on last edited by
      #2

      What have you managed to come up with so far? I'd imagine there are quite a few different solutions to this problem so if you show what you have so far I'll try and help from there.

      1 Reply Last reply
      0
      • S Satish Developer

        Need Query for this requirement. The data in my table is as follows Zip Blac White green 123 1.76 2.04 1.00 234 3.49 2.39 3.24 Need Columname as output if the column value is > 2 for given zipcode. For example.if i give zip as "123" the query has to find out the value among the columns which is > 2. i.e White If i give zip as "234" then the query has to find out the value among the columns which is > 2. But here all values are > 2. So you have to pick the max value. i.e 3.49 = Blac

        G. Satish

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

        Hi,I hope i understood you good enought. Here is TSQL solution which I hope will works for you. select * from mytable declare @zip as int set @zip = 123 declare @white as varchar(100) set @white =(select white from mytable where zip=@zip) declare @black as varchar(100) set @black =(select black from mytable where zip=@zip) declare @green as varchar(100) set @green =(select green from mytable where zip=@zip) create table #temptable (valstr varchar(100),colname varchar(100)) insert into #temptable values (@white,'white') insert into #temptable values (@black,'black') insert into #temptable values (@green,'green') declare @colname as varchar(100) set @colname=( select top 1 colname from #temptable order by valstr desc ) drop table #temptable declare @selectquery as nvarchar(max) set @selectquery ='select '+@colname+' from mytable where zip='+cast(@zip as varchar(10)) exec(@selectquery)


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        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