Get a row from one table and check the corresponding row in the other table if exists the add this row to the next of the first table row - sql-server-2012

I have a table say A which has some rows and 'Id' column as a primary key. And other table B and it has the 'TabAId' and references the table A id column.
Want to get a report like shown in the attached image.
Explanation
I 'sql server' database
select One row from the Table A and check the Id in the Table B, If exists then add table B row as next row(If multiple rows are there then also add those number of rows as next rows) else go further.
Tried with case statement which appends to the row, not adds as next row.
With join also happens same only.
It may be easy through programming language like php or scripting like jquery and ajax but I want it through sql server only. It helps me for the further requirements.
So please someone help me.
Edited:
create table tabA(id int not null primary key,
name varchar(20) null,age int null)
insert into tabA values(1,'Sudeep',35),
(2,'Darshan',34)
create table tabB(A_id int not null,nickname varchar(20) null )
insert into tabB values(1,'Kiccha'),
(1,'Nalla'),
(2,'Boss')
output should be like below
Id | name | age |
------------------------
1 | Sudeep | 35 |
------------------------
| *Kichha | |
------------------------
| *Nalla | |
------------------------
2 | Darshan | 34 |
------------------------
| *Boss | |
------------------------

based on the requirement i have done the following.
;
WITH cte
AS (
SELECT *
,DENSE_RANK() OVER (
ORDER BY id
) dn
,ROW_NUMBER() OVER (
PARTITION BY id ORDER BY age DESC
) rn
FROM (
SELECT *
FROM tabA a
UNION ALL
SELECT *
,NULL
FROM tabB b where exists (select 1 from taba a where a.id=b.A_id)
) a
)
SELECT iif(rn = 1, cast(id AS VARCHAR(50)), '') ID
,CONCAT (
iif(rn = 1, '', '*')
,name
) NAME
,iif(rn = 1, cast(age AS VARCHAR(50)), '') AGE
FROM cte
please let me know if anything needs to be added
Edit: as requested display the results based on the offset
if OBJECT_ID('tempdb..#cte_results') is not null
drop table #cte_results
/*
in order to achieve the second goal we need to store in results in a table then use that table to display
results
*/
;
WITH cte
AS (
SELECT *
,DENSE_RANK() OVER (
ORDER BY id
) dn
,ROW_NUMBER() OVER (
PARTITION BY id ORDER BY age DESC
) rn
,ROW_NUMBER() over( order by id asc,age desc) off_set
FROM (
SELECT *
FROM tabA a
UNION ALL
SELECT *
,NULL
FROM tabB b where exists (select 1 from taba a where a.id=b.A_id)
) a
)
SELECT iif(rn = 1, cast(id AS VARCHAR(50)), '') ID
,CONCAT (
iif(rn = 1, '', '*')
,name
) NAME
,iif(rn = 1, cast(age AS VARCHAR(50)), '') AGE,off_set,rn,max(rn) over(partition by id) max_rn,id idrrr
into #cte_results
FROM cte
/*
the following query is used to display the results in the screen dynamically
*/
declare #pre_offset int=0, #post_offset int =2
set #post_offset=( select top 1 max(max_rn)-max(rn)
from #cte_results
where off_Set
between #pre_offset and #post_offset
group by idrrr
order by idrrr desc
)+#post_offset
select id,name,age from #cte_results
where off_Set
between #pre_offset and #post_offset
Results were as follows

Related

Need to return an ID which has start and END in sql server

I have a scenario wherein I need to find the ID which only has start and END in it. Below is the table for reference.
Declare #T Table ( ID int, Name varchar(100))
Insert into #T values (1,'Start')
Insert into #T values (1,'END')
Insert into #T values (1,'Stuart')
Insert into #T values (1,'robin')
Insert into #T values (2,'Start')
Insert into #T values (2,'END')
Insert into #T values (3,'Start')
Insert into #T values (4,'END')
I want the Output as:
ID Name
2 Start
2 END
I want those ID which only has start and end in it.
What I tried so far:
SELECT * FROM #T t
WHERE EXISTS (SELECT * FROM #T WHERE id = t.id AND name = 'start')
AND EXISTS (SELECT * FROM #T WHERE id = t.id AND name = 'END')
But my query is giving ID 1 as well.
Can someone please help me rectify the problem.
I presume your issue is that record 1 has a 'Stuart' in it too?
As such, you can do a similar check in the WHERE e.g.,
SELECT * FROM #T t
WHERE EXISTS (SELECT * FROM #T WHERE id = t.id AND name = 'start')
AND EXISTS (SELECT * FROM #T WHERE id = t.id AND name = 'END')
AND NOT EXISTS (SELECT * FROM #T WHERE id = t.id AND name NOT IN ('start','END'))
Note that you may want to consider
What happens if you have two 'start' rows or two 'end' rows (e.g., start-start-end)? Can you even have two 'start' rows (e.g., start-start)?
What happens if you have a blank/NULL (e.g., start-NULL-end)?
EDIT: removed 'What happens if they're out of order (e.g., end-start)?' as a question as there is no sorting in the data at all (e.g., not even an implicit sort).
You can go for CTE to get group wise count and total count as 2.
Declare #T Table ( ID int, Name varchar(100))
Insert into #T values (1,'Start')
Insert into #T values (1,'END')
Insert into #T values (1,'Stuart')
Insert into #T values (1,'robin')
Insert into #T values (2,'Start')
Insert into #T values (2,'END')
Insert into #T values (3,'Start')
Insert into #T values (4,'END')
;WITH CTE_Total_StartEnd AS
(
select id, count(*) AS Total_Cnt
, COUNT( case when Name IN ('Start') THEN 1 END) as start_cnt
, COUNT( case when Name IN ('End') THEN 1 END) as end_cnt
from #t
group by id
having COUNT( case when Name IN ('Start') THEN 1 END) =1 and
COUNT( case when Name IN ('End') THEN 1 END) = 1 and
count(*) = 2
)
SELECT t.* from #t t
inner join CTE_Total_StartEnd as c
ON c.id = t.id
+----+-------+
| ID | Name |
+----+-------+
| 2 | Start |
| 2 | END |
+----+-------+
You can do this by using group by function also like below
WITH cte AS
(
SELECT 1 AS id , 'Start' AS name
UNION ALL
SELECT 1 AS id ,'END' AS name
UNION ALL
SELECT 1 AS id ,'Stuart' AS name
UNION ALL
SELECT 1 AS id ,'robin' AS name
UNION ALL
SELECT 2 AS id ,'Start' AS name
UNION ALL
SELECT 2 AS id ,'END' AS name
UNION ALL
SELECT 3 AS id ,'Start' AS name
UNION ALL
SELECT 4 AS id ,'END' AS name
)
SELECT T.ID,SUM(T.VAL)AS SUM
FROM
(
SELECT id,name , CASE WHEN name='Start' THEN 1
WHEN name='END' THEN 2
ELSE 3
END AS VAL
FROM cte
)T
GROUP BY T.ID
HAVING SUM(T.VAL) =3
could you please try this? Pls note i added collate command in the end of sql.
SQL Server check case-sensitivity?
SELECT * FROM #T t
WHERE EXISTS (SELECT * FROM #T WHERE id = t.id AND name = 'start' COLLATE SQL_Latin1_General_CP1_CS_AS)
AND EXISTS (SELECT * FROM #T WHERE id = t.id AND name = 'END' COLLATE SQL_Latin1_General_CP1_CS_AS)

Insert Missing Values Into Table Using SQL

Objective: I need to fully populate a table with a matrix of values for each column by [PropertyId] grouping. Several [PropertyId] have all the necessary values for each column (Table 1), however, many are missing some values (Table 2). Furthermore, not every [PropertyId] needs these values as they have completely different regional values. Therefore, I need to identify which [PropertyId] both need the values populated and don't have all the necessary values.
Examples:
Table 1. Each identified [PropertyId] grouping should have 23 distinct records for these four columns [ReportingVolumeSettingId],[SpeciesGroupInventoryID],[CropCategoryID],[SortOrder].
Table 2. Here is an example of a PropertyID that is missing a value combination as it only has 22 records:
Both of these example results were queried from the same table [ReportingVolume]. I have not been successful in even identifying which record combination per [PropertyID] are missing. I would like to identify each missing record combination and then insert that record combination into the [ReportingVolume] table.
Problem to Solve -- The SQL Code below is my attempt to 1. Identify the correct List of Values; 2. Identify which properties should have matching values; 3. Identify which properties are missing values; 4. Identify the missing values per property.
;with CORRECT_LIST as
(
select
SpeciesGroupInventoryName, SpeciesGroupInventoryId, CropCategoryName,CropCategoryID, UnitOfMeasure, SortOrder
--*
from [GIS].[RST].[vPropertyDefaultTimberProductAndUnitOfMeasure]
where PropertyId in (1)
)
,
property_list as
(
select distinct rvs.propertyid as Volume_Property, pd.PropertyName, pd.PropertyId from RMS.GIS.ReportingVolumeSetting rvs
right outer JOIN RMS.GIS.PropertyDetail AS pd ON rvs.PropertyId = pd.PropertyId
left outer JOIN RMS.GIS.SpeciesGroupInventory AS sgi ON rvs.SpeciesGroupInventoryId = sgi.SpeciesGroupInventoryId
where sgi.SpeciesGroupInventoryId in (1,2,3)
or pd.PropertyId = 171
)
, Partial_LISTS as
(
select Count(distinct ReportingVolumeSettingId) as CNT_REPORT, pd.PropertyName, pd.PropertyId
from [GIS].[ReportingVolumeSetting] rvs
right outer JOIN property_list AS pd ON rvs.PropertyId = pd.PropertyId
group by pd.propertyId, pd.PropertyName
)
, Add_Props as
(
select propertyName, propertyId, SUM(CNT_REPORT) as CNT_RECORDS from Partial_LISTS
where CNT_REPORT < 23
group by propertyName, propertyId
)
, RVS_RECORDS_PROPS as
(
select addProps.PropertyName, rvs.* from [GIS].[ReportingVolumeSetting] rvs
join Add_Props addProps on addprops.PropertyId = rvs.PropertyID
where rvs.PropertyId in (select PropertyId from Add_Props)
)
select rp.PropertyName, cl.*, rp.SpeciesGroupInventoryId from correct_list cl
left outer join RVS_Records_Props rp
on rp.SpeciesGroupInventoryId = cl.SpeciesGroupInventoryId
and rp.CropCategoryId = cl.CropCategoryID
and rp.SortOrder = cl.SortOrder
Order by rp.PropertyName
How can I modify the code or create a new code block identifies the missing values and inserts them into the table per PropertyId?
I am using SQL SMSS v15.
Thanks so much.
This should identify missing entries. You could simply add an INSERT INTO command on top of this. Keep in mind as the ReportingVolumeSettingId is unique and unknown it's not covered here.
SELECT * FROM (SELECT DISTINCT PropertyId FROM ReportingVolume) rv
CROSS APPLY
(
SELECT DISTINCT SpeciesGroupInventoryId
, CropCategoryId
, SortOrder
FROM ReportingVolume
) x
EXCEPT
SELECT PropertyId, SpeciesGroupInventoryId, CropCategoryId, SortOrder FROM ReportingVolume
I don't have access to your data, so I cannot provide an example specific to your environment, but I can provide you a simple example using SQL Server's EXCEPT operator.
Run the following example in SSMS:
-- Create a list of required values.
DECLARE #Required TABLE ( required_id INT, required_val VARCHAR(50) );
INSERT INTO #Required ( required_id, required_val ) VALUES
( 1, 'Required value 1.' ), ( 2, 'Required value 2.' ), ( 3, 'Required value 3.' ), ( 4, 'Required value 4.' ), ( 5, 'Required value 5.' );
-- Create some sample data to compare against.
DECLARE #Data TABLE ( PropertyId INT, RequiredId INT );
INSERT INTO #Data ( PropertyId, RequiredId ) VALUES
( 1, 1 ), ( 1, 2 ), ( 1, 3 ), ( 2, 1 ), ( 2, 2 ), ( 2, 4 ), ( 2, 5 );
-- Set a property id value to query.
DECLARE #PropertyId INT = 1;
-- Preview #Data's rows for the specified #PropertyId.
SELECT * FROM #Data WHERE PropertyId = #PropertyId ORDER BY PropertyId, RequiredId;
At this point, I've created a list of required values (required_id 1 through 5) and some dummy data to check them against. This initial SELECT shows the current resultset for the specified #PropertyID:
+------------+------------+
| PropertyId | RequiredId |
+------------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
+------------+------------+
You can see that required_id values 4 and 5 are missing for the current property. Next, we can compare #Required against #Data and INSERT any missing required values using the EXCEPT operator and then return the corrected resultset.
-- Insert any missing required values for #PropertyId.
INSERT INTO #Data ( PropertyId, RequiredId )
SELECT #PropertyId, required_id FROM #Required
EXCEPT
SELECT PropertyId, RequiredId FROM #Data WHERE PropertyId = #PropertyId;
-- Preview #Data's revised rows for #PropertyId.
SELECT * FROM #Data WHERE PropertyId = #PropertyId ORDER BY PropertyId, RequiredId;
The updated resultset now looks like the following:
+------------+------------+
| PropertyId | RequiredId |
+------------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
+------------+------------+
You can run this again against #PropertyId = 2 to see a different scenario.
Note the order to using EXCEPT. The required rows comes first, followed by the EXCEPT operator, and then the current rows to be validated. This is important. EXCEPT is saying show me rows from #Required that are not in #Data--which allows for the inserting of any missing required values into #Data.
I know this example doesn't represent your existing data with the 23 rows requirement, but hopefully, it will get you moving with a solution for your needs. You can read more about the EXCEPT operator here.
Here is the complete SSMS example:
-- Create a list of required values.
DECLARE #Required TABLE ( required_id INT, required_val VARCHAR(50) );
INSERT INTO #Required ( required_id, required_val ) VALUES
( 1, 'Required value 1.' ), ( 2, 'Required value 2.' ), ( 3, 'Required value 3.' ), ( 4, 'Required value 4.' ), ( 5, 'Required value 5.' );
-- Create some sample data to compare against.
DECLARE #Data TABLE ( PropertyId INT, RequiredId INT );
INSERT INTO #Data ( PropertyId, RequiredId ) VALUES
( 1, 1 ), ( 1, 2 ), ( 1, 3 ), ( 2, 1 ), ( 2, 2 ), ( 2, 4 ), ( 2, 5 );
-- Set a property id value to query.
DECLARE #PropertyId INT = 1;
-- Preview #Data's rows for the specified #PropertyId.
SELECT * FROM #Data WHERE PropertyId = #PropertyId ORDER BY PropertyId, RequiredId;
-- Insert any missing required values for #PropertyId.
INSERT INTO #Data ( PropertyId, RequiredId )
SELECT #PropertyId, required_id FROM #Required
EXCEPT
SELECT PropertyId, RequiredId FROM #Data WHERE PropertyId = #PropertyId;
-- Preview #Data's revised rows for #PropertyId.
SELECT * FROM #Data WHERE PropertyId = #PropertyId ORDER BY PropertyId, RequiredId;

Update Table 1 memo field with values from table 2 in a one-to-many relationship

I have 2 tables I would like to update one column in table 1 with values from table 2 where id=id. However table 2 has many rows matching table 1 and all rows of table 2 would need to be updated to 1 row in table 1
Table_A
id | all_names |
---+-----------------+
1 |AB CD FG HI |
2 | |
** Table_B **
id | name |
---+-------+
1 | |
2 | Jon |
2 | Mike |
After the update Table 1 should look like
id | all_names |
---+-----------------+
1 |AB CD FG HI |
2 |Jon Mike |
I tried
update a
set a.all_names = TRIM(a.all_names) + b.name + ' '
from table_a a, table_b b
where a.id = b.id
All I end up getting is an empty all_names in table_a
Any idea?
I can't really see any other way of doing this other than through a loop.
DECLARE #id int
DECLARE #name varchar(50)
SELECT * INTO #temp FROM TABLE_B
WHILE EXISTS (SELECT 1 FROM #temp)
BEGIN
SELECT #id = (SELECT TOP 1 id from #temp)
SELECT #name = (SELECT TOP 1 [name] from #temp where id = #id)
UPDATE A
SET all_names = LTRIM(RTRIM(all_names + CHAR(32) + #name))
FROM Table_A A
WHERE A.id = #id
DELETE FROM #temp WHERE id = #id and [name] = #name
END
DROP TABLE #temp
The query puts the contents of table B into a temporary table, and removes the row once it has used it. So essentially all names keeps the same value through the loop for its own ID, except a space + the next name gets added each time. I've added a trim to the update as well to prevent leading / trailing spaces.
I don't know if this helps, but this is an Oracle version strictly using SQL. You didn't mention it in your requirements, but the second merge prevents duplicate entries in the row:
Create tables and insert sample rows
DROP TABLE table_a;
DROP TABLE table_b;
CREATE TABLE table_a
(
id INTEGER
, all_names VARCHAR2 (128)
);
CREATE TABLE table_b
(
id INTEGER
, name VARCHAR2 (10)
);
INSERT INTO table_a (id, all_names)
VALUES (1, 'AB CD FG HI');
INSERT INTO table_a (id, all_names)
VALUES (2, NULL);
INSERT INTO table_b (id, name)
VALUES (1, NULL);
INSERT INTO table_b (id, name)
VALUES (2, 'Jon');
INSERT INTO table_b (id, name)
VALUES (2, 'Mike');
COMMIT;
Merge allowing duplicates
MERGE INTO table_a ta
USING (SELECT DISTINCT id, LISTAGG (name, ' ') WITHIN GROUP (ORDER BY name) OVER (PARTITION BY id) names
FROM table_b) tb
ON (ta.id = tb.id)
WHEN MATCHED
THEN
UPDATE SET all_names = all_names || tb.names
WHEN NOT MATCHED
THEN
INSERT (
ta.id, ta.all_names
)
VALUES (
tb.id, tb.names
);
SELECT *
FROM table_a;
ROLLBACK;
Merge eliminating duplicates
MERGE INTO table_a ta
USING (SELECT DISTINCT id, LISTAGG (name, ' ') WITHIN GROUP (ORDER BY name) OVER (PARTITION BY id) names
FROM (WITH
aset
AS
(SELECT id, TRIM (all_names) || ' ' AS all_names
FROM table_a),
bset (id, name, REMAINDER)
AS
(SELECT id
, SUBSTR (all_names, 1, INSTR (all_names, ' ') - 1) name
, SUBSTR (all_names, INSTR (all_names, ' ') + 1) REMAINDER
FROM aset
UNION ALL
SELECT id
, SUBSTR (REMAINDER, 1, INSTR (REMAINDER, ' ') - 1) name
, SUBSTR (REMAINDER, INSTR (REMAINDER, ' ') + 1) REMAINDER
FROM bset
WHERE name IS NOT NULL)
SELECT id, name
FROM bset
WHERE name IS NOT NULL
UNION
SELECT id, name
FROM table_b
WHERE name IS NOT NULL)) tb
ON (ta.id = tb.id)
WHEN MATCHED
THEN
UPDATE SET all_names = tb.names
WHEN NOT MATCHED
THEN
INSERT (ta.id, ta.all_names)
VALUES (tb.id, tb.names);
SELECT *
FROM table_a;
--ROLLBACK;
What I ended up doing
Declare #Crs cursor as select * from Table_B; //Temp Table
open #crs;
while fetch #crs do
update Table_A set all_names=ifnull(Table_B,'')+trim(#crs.name)+' ' where
id=#Crs.id;
end while;
close #crs;
This uses the least of lines and is elegant

SQL Server, Merge two records in one record

We have these tables
CREATE TABLE tbl01
(
[id] int NOT NULL PRIMARY KEY,
[name] nvarchar(50) NOT NULL
)
CREATE TABLE tbl02
(
[subId] int NOT NULL PRIMARY KEY ,
[id] int NOT NULL REFERENCES tbl01(id),
[val] nvarchar(50) NULL,
[code] int NULL
)
If we run this query:
SELECT
tbl01.id, tbl01.name, tbl02.val, tbl02.code
FROM
tbl01
INNER JOIN
tbl02 ON tbl01.id = tbl02.id
we get these results:
-------------------------------
id | name | val | code
-------------------------------
1 | one | FirstVal | 1
1 | one | SecondVal | 2
2 | two | YourVal | 1
2 | two | OurVal | 2
3 | three | NotVal | 1
3 | three | ThisVal | 2
-------------------------------
You can see that each two rows are related to same "id"
The question is: we need for each id to retrieve one record with all val, each val will return in column according to the value of column code
if(code = 1) then val as val-1
else if (code = 2) then val as val-2
Like this:
-------------------------------
id | name | val-1 | val-2
-------------------------------
1 | one | FirstVal | SecondVal
2 | two | YourVal | OurVal
3 | three | NotVal | ThisVal
-------------------------------
Any advice?
Use can use MAX and Group By to achieve this
SELECT id,
name,
MAX([val1]) [val-1],
MAX([val2]) [val-2]
FROM ( SELECT tbl01.id, tbl01.name,
CASE code
WHEN 1 THEN tbl02.val
ELSE ''
END [val1],
CASE code
WHEN 2 THEN tbl02.val
ELSE ''
END [val2]
FROM tbl01
INNER JOIN tbl02 ON tbl01.id = tbl02.id
) Tbl
GROUP BY id, name
Is it the PIVOT operator (http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx) that you are looking for?
You've already got a few answers, but heres one using PIVOT as an alternative. The good thing is this approach is easy to scale if there are additional columns required later
-- SETUP TABLES
DECLARE #t1 TABLE (
[id] int NOT NULL PRIMARY KEY,
[name] nvarchar(50) NOT NULL
)
DECLARE #t2 TABLE(
[subId] int NOT NULL PRIMARY KEY ,
[id] int NOT NULL,
[val] nvarchar(50) NULL,
[code] int NULL
)
-- SAMPLE DATA
INSERT #t1 ( id, name )
VALUES ( 1, 'one'), (2, 'two'), (3, 'three')
INSERT #t2
( subId, id, val, code )
VALUES ( 1,1,'FirstVal', 1), ( 2,1,'SecondVal', 2)
,( 3,2,'YourVal', 1), ( 4,2,'OurVal', 2)
,( 5,3,'NotVal', 1), ( 6,3,'ThisVal', 2)
-- SELECT (using PIVOT)
SELECT id, name, [1] AS 'val-1', [2] AS 'val-2'
FROM
(
SELECT t2.id, t1.name, t2.val, t2.code
FROM #t1 AS t1 JOIN #t2 AS t2 ON t2.id = t1.id
) AS src
PIVOT
(
MIN(val)
FOR code IN ([1], [2])
) AS pvt
results:
id name val-1 val-2
---------------------------------
1 one FirstVal SecondVal
2 two YourVal OurVal
3 three NotVal ThisVal
If there are always only two values, you could join them or even easier, group them:
SELECT tbl01.id as id, Min(tbl01.name) as name, MIN(tbl02.val) as val-1, MAX(tbl02.val) as val-2
FROM tbl01
INNER JOIN tbl02 ON tbl01.id = tbl02.id
GROUP BY tbl02.id
note: this query will always put the lowest value in the first column and highest in the second, if this is not wanted: use the join query:
Join query
If you always want code 1 in the first column and code 2 in the second:
SELECT tbl01.id as id, tbl01.name as name, tbl02.val as val-1, tbl03.val as val-2
FROM tbl01
INNER JOIN tbl02 ON tbl01.id = tbl02.id
ON tbl02.code = 1
INNER JOIN tbl03 ON tbl01.id = tbl03.id
ON tbl03.code = 2
Variable amount of columns
You cannot get an variable amount of columns, only when you do this by building your query in code or t-sql stored procedures.
My advice:
If its always to values: join them in query, if not, let your server-side code transform the data. (or even better, find a way which makes it not nessecery to transform data)
Try this - it uses a pivot function but it also creates creates the dynamic columns dependent on code
DECLARE #ColumnString varchar(200)
DECLARE #sql varchar(1000)
CREATE TABLE #ColumnValue
(
Value varchar(500)
)
INSERT INTO #ColumnValue (Value)
SELECT DISTINCT '[' + 'value' + Convert(Varchar(20),ROW_NUMBER() Over(Partition by id Order by id )) + ']'
FROM Test
SELECT #ColumnString = COALESCE(#ColumnString + ',', '') + Value
FROM #ColumnValue
Drop table #ColumnValue
SET #sql =
'
SELECT *
FROM
(
SELECT
id,name,val,''value'' + Convert(Varchar(20),ROW_NUMBER() Over(Partition by id Order by id ))as [values]
FROM Test
) AS P
PIVOT
(
MAX(val) FOR [values] IN ('+#ColumnString+')
) AS pv
'
--print #sql
EXEC (#sql)

How to convert columns to rows?

I have a table like this one
RowNum | TranNo | nTotalSales | nBalance
1 | 1 | 800 | 0
and I want to display it this way
RowNum | 1
cTranNo | 1
nTotalSales | 800
nBalance | 0
How can I do this?
Here is a complete working example, when you you do an UNPIVOT, which is what your are asking for, your 'value' types need to be of the same type, so cast them however you want. In my example, I have cast them all to VARCHAR(20):
DECLARE #bob TABLE
(
RowNum INT,
TranNo INT,
nTotalSales INT,
nBalance INT
);
INSERT INTO #bob(RowNum, TranNo, nTotalSales, nBalance)
VALUES(1, 1, 800, 0);
WITH T AS (
SELECT CAST(RowNum AS VARCHAR(20)) AS RowNum,
CAST(TranNo AS VARCHAR(20)) AS TranNo,
CAST(nTotalSales AS VARCHAR(20)) AS nTotalSales,
CAST(nBalance AS VARCHAR(20)) AS nBalance
FROM #bob
)
SELECT attribute, value
FROM T
UNPIVOT(value FOR attribute IN(RowNum, TranNo, nTotalSales, nBalance)) AS U;
SELECT 'RowNum' TITLE, RowNum AS [VALUE]
FROM TABLE
UNION ALL
SELECT 'TranNo', TranNo
FROM TABLE
UNION ALL
SELECT 'nTotalSales', nTotalSales
FROM TABLE
UNION ALL
SELECT 'nBalance', nBalance
FROM TABLE
It's not real fun, but here's one solution:
SELECT 'RowNum', RowNum FROM tbl
UNION
SELECT 'cTranNo', TranNo FROM tbl
UNION
SELECT 'nTotalSales', nTotalSales FROM tbl
UNION
SELECT 'nBalance', nBalance FROM tbl
That will turn the columns into rows. If you want each of the column-rows to be interlaced, you may need to introduce a record number along with some sorting.
That would look like this:
SELECT 'RowNum' AS ColName, RowNum AS [Value], RowNum FROM tbl
UNION
SELECT 'cTranNo' AS ColName, TranNo, RowNum FROM tbl
UNION
SELECT 'nTotalSales' AS ColName, nTotalSales, RowNum FROM tbl
UNION
SELECT 'nBalance' AS ColName, nBalance, RowNum FROM tbl
ORDER BY RowNum, ColName