Problem in Query
-
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
-
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
Why exactly do you need something like this? It looks weird. You want do mix pieces of different information in the same column.
-
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
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.
-
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
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=''
'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
-
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.
-
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
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