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. Problem in Query

Problem in Query

Scheduled Pinned Locked Moved Database
databasehelp
6 Posts 5 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.
  • G Offline
    G Offline
    garga1
    wrote on last edited by
    #1

    Hi Friends, I hv got 2 fields in a table. Year and title. year Title 1945 t1 1945 t2 1945 t3 i want a query to retrieve answer like foll. : 1945 //Field name t1 t2 t3 1945 should be a field name and under that i need 3 rows where i can display title values.. Thanks in advance

    M A A N 4 Replies Last reply
    0
    • G garga1

      Hi Friends, I hv got 2 fields in a table. Year and title. year Title 1945 t1 1945 t2 1945 t3 i want a query to retrieve answer like foll. : 1945 //Field name t1 t2 t3 1945 should be a field name and under that i need 3 rows where i can display title values.. Thanks in advance

      M Offline
      M Offline
      Marek Grzenkowicz
      wrote on last edited by
      #2

      Why exactly do you need something like this? It looks weird. You want do mix pieces of different information in the same column.

      1 Reply Last reply
      0
      • G garga1

        Hi Friends, I hv got 2 fields in a table. Year and title. year Title 1945 t1 1945 t2 1945 t3 i want a query to retrieve answer like foll. : 1945 //Field name t1 t2 t3 1945 should be a field name and under that i need 3 rows where i can display title values.. Thanks in advance

        A Offline
        A Offline
        Anurag Gandhi
        wrote on last edited by
        #3

        Use Union select distinct Year as [ColumnName] from myTable union select Title from myTable Use group by in first query if required in your circumstance.

        Anurag Gandhi. http://www.gandhisoft.com Life is a computer program and every one is the programmer of his own life.

        G 1 Reply Last reply
        0
        • G garga1

          Hi Friends, I hv got 2 fields in a table. Year and title. year Title 1945 t1 1945 t2 1945 t3 i want a query to retrieve answer like foll. : 1945 //Field name t1 t2 t3 1945 should be a field name and under that i need 3 rows where i can display title values.. Thanks in advance

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          What you are trying to achieve (I think) is known as pivoting. This sample should point you in the right direction.

          /* create test table and populate */

          create table UserArea(Country varchar(20))

          insert into UserArea
          select 'India'
          union all
          select 'USA'
          union all
          select 'India'
          union all
          select 'UK'

          /* now the actual code */

          DECLARE @SQL nvarchar(4000)

          SET @SQL=''

          SELECT @SQL= @SQL +

          'SUM(CASE WHEN Country=''' + a.Country + ''' THEN 1 ELSE 0 END) AS [' + a.Country + '],'

          FROM (select distinct Country from UserArea) as a

          select @SQL = left(@SQL,len(@SQL)-1)

          SET @SQL='SELECT ' + @SQL + ' FROM UserArea'

          EXEC(@SQL)

          /* tidy up */

          drop table UserArea

          Bob Ashfield Consultants Ltd

          1 Reply Last reply
          0
          • A Anurag Gandhi

            Use Union select distinct Year as [ColumnName] from myTable union select Title from myTable Use group by in first query if required in your circumstance.

            Anurag Gandhi. http://www.gandhisoft.com Life is a computer program and every one is the programmer of his own life.

            G Offline
            G Offline
            garga1
            wrote on last edited by
            #5

            Thanks yaar. It works.. But can we get a output like this : 1945 1950 1960 t1 t4 t6 t2 t5 t7 t3 i hv also added group by. But it doesn't work..

            1 Reply Last reply
            0
            • G garga1

              Hi Friends, I hv got 2 fields in a table. Year and title. year Title 1945 t1 1945 t2 1945 t3 i want a query to retrieve answer like foll. : 1945 //Field name t1 t2 t3 1945 should be a field name and under that i need 3 rows where i can display title values.. Thanks in advance

              N Offline
              N Offline
              Niraj_Silver
              wrote on last edited by
              #6

              hi Niraj Here , I hope this will work for you.... try this.. Select fieldnamewhich containst1t2ec as [1945] From Tablename Where year = 1945 Have a noce Day.. Stay Tune ..... Take Care.... :-D

              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