Tuesday, August 23, 2011

To check for more than one primary key to insert into a table.

For example table p1 and p2 are combined and inserted into table p3

INSERT INTO p1 VALUES (1000)
INSERT INTO p1 VALUES (2000)
INSERT INTO p1 VALUES (3000)
INSERT INTO p2 VALUES (1000,5)
INSERT INTO p2 VALUES (1000,2)
INSERT INTO p2 VALUES (2000,1)
INSERT INTO p2 VALUES (3000,1)

SELECT * FROM p3
INSERT INTO p3

select hdr.ponumber,ord
from p2 Det,
p1 Hdr
where Hdr.PONumber = Det.PONumber
and (det.ponumber not in (select ponumber from p3)
or det.ord not in (select ord from p3))

Friday, August 5, 2011

Converting rows into columns

drop table temp1
select td.salesordernoitem,
case when diskcomp = 'inlet' then material end as I into temp1
from tbltransassemblies ta
inner join tbltransdiskcomp td on td.salesordernoitem = ta.salesordernoitem
where diskcomp in ('inlet')
drop table temp2
select td.salesordernoitem,
case when diskcomp = 'outlet' then material end as O into temp2
from tbltransassemblies ta
inner join tbltransdiskcomp td on td.salesordernoitem = ta.salesordernoitem
where diskcomp in ('outlet')
select t1.salesordernoitem,I,o into temp3
from temp1 t1 inner join temp2 t2 on
t1.salesordernoitem = t2.salesordernoitem

select * from temp3
select * from tbltransdiskcomp where salesordernoitem = '11381068-1'