Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select query show each row as two rows based on 2

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2013-12-09 : 06:29:45
I have two columns(filepath, archivepath) in same table / row holding two different file paths, primary key is docid.

I want to show as two rows.
Doctype is just a flag to show whther it is type F or A and first three columns has same value in table.


docid,modrecordid, modname, filepath as docpath, 'F' as DocType
docid,modrecordid, modname, archivepath as docpath, 'A', as DocType


Using Base query getting above 5 column values, every docid with rows:

select docid, modrecordid, modName, filepath, archivepath from tab_docrepository order by docid;

Thank you very much for the helpful info.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-12-09 : 07:13:40
select docid,modrecordid, modname, filepath as docpath, 'F' as DocType from table
union all
select docid,modrecordid, modname, archivepath as docpath, 'A' as DocType from table



Too old to Rock'n'Roll too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-09 : 07:23:23
[code]
select t.docid,t.modrecordid, t.modname,t1.docpath,t1.DocType
from table t
cross apply (values(t.filepath,'F'),(t.archivepath,'A'))t1(docpath,DocType)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2013-12-09 : 13:57:36
Visakh, I like your approach using cross apply. Thank you very much for the help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-09 : 22:28:37
quote:
Originally posted by cplusplus

Visakh, I like your approach using cross apply. Thank you very much for the help.


you're welcome
VALUES is table constructor available from 2008 onwards and you can use it for following scenarios

http://visakhm.blogspot.in/2012/05/multifacet-values-clause.html
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -