SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select query show each row as two rows based on 2
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Aged Yak Warrior

533 Posts

Posted - 12/09/2013 :  06:29:45  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 12/09/2013 :  07:13:40  Show Profile  Visit webfred's Homepage  Reply with Quote
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

India
52309 Posts

Posted - 12/09/2013 :  07:23:23  Show Profile  Reply with Quote

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)


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

cplusplus
Aged Yak Warrior

533 Posts

Posted - 12/09/2013 :  13:57:36  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 12/09/2013 :  22:28:37  Show Profile  Reply with Quote
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

Edited by - visakh16 on 12/09/2013 22:30:05
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000