BrianMichaels
BrianMichaels

Reputation: 522

How do I create two rows from one row SQL

I do I make two rows from one row using a select query?

Here is an example

  create table timecards 
  (employeeid nvarchar(10) not null,
  regulartime int null,
  overtime int null)

  insert into timecards
  values ('A', 8, 1)

I would like the results to be

 A,8
 A,1

Upvotes: 3

Views: 10517

Answers (2)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

You can unpivot the table. null values in any of the columns would be eliminated in this case.

SQL Fiddle

select employeeid, tm
from 
(select employeeid,regulartime,overtime from timecard) t
UNPIVOT
(tm for id in (regulartime,overtime)
) as x

Upvotes: 2

joshp
joshp

Reputation: 1892

Lots of ways....how about

select employeeid, regulartime as somekindatime
from timecards
union all
select employeeid, overtime
from timecards

The ALL keyword just informs SQL that we don't want to sort and eliminate duplicates. Can save time unless you actually want to eliminate duplicate rows.

Upvotes: 2

Related Questions