merging tables by using PROC SQL where in statement in SAS - mysql

everyone.
I'm currently using SAS.
Here's my situation.
I have a table called 'a', which looks like:
id_c id_t
5 3
2 9
15 1
65 43
... ...
This,a, table has only two different ids from other tables. These 5&3, 2&9,... are pairs.
What I want to do is that I want to take out each id_t and id_c so that I can merge other variables in other tables, respectively.
To do so, first I decide to take out id_t and merge other variables.
This following code works:
proc sql;
create table t1 as
select *
from other_var_table_1
where id_t in (select id_t from a);
quit;
The result is:
id_t var1 var2 var3
3
9
1
43
Here, the order of id_t is exactly the same as the order of id_t in table 'a'.
However, when I do the same thing by using id_c, the order is messed up.
I want:
id_c var_x var_y var_z
5
2
15
65
but when I run the same code, the result becomes such as:
proc sql;
create table t2 as
select *
from other_var_table_2
where id_c in (select id_c from a);
quit;
id_c var_x var_y var_z
65
15
5
2
The purpose of doing this is to merge the two tables again including all the variables such as:
data final;
set t2;
set t1;
run;
id_c var_x var_y var_z id_t var1 var2 var3
5 3
2 9
15 1
65 43
... ...
It would be greatly appreciated if someone could help me out.
Thank you,

/****CREATING DATASETS****/
data a;
input int_c int_t;
cards;
5 3
2 9
15 1
65 43
;
run;
/*Create a **seq** number so that your order does not change*/
data a;
set a;
seq = _n_;
run;
data other_var_table_1;
input int_t var1 var2 var3;
cards;
3 12 43 76
1 10 20 30
;
run;
data other_var_table_2;
input int_c var_x var_y var_z;
cards;
2 100 200 300
5 1 2 3
65 10 20 30
;
run;
/****JOINING THE REQUIRED COLUMNS****/
proc sql;
create table final as
select r.*,p.var1,p.var2,p.var3,q.var_x,q.var_y,q.var_z
from
a r
left join
other_var_table_1 p
on r.int_t = p.int_t
left join
other_var_table_2 q
on r.int_c = q.int_c
order by r.seq;
run;
You can drop column seq after the results if you want. Let me know if you have any doubts.
My Output:
int_c |int_t |SEQ |var1 |var2 |var3 |var_x |var_y |var_z
5 |3 |1 |12 |43 |76 |1 |2 |3
2 |9 |2 |. |. |. |100 |200 |300
15 |1 |3 |10 |20 |30 |. |. |.
65 |43 |4 |. |. |. |10 |20 |30

Related

How to display values from a column of a table which are non existant and display zero beside it in SQL?

I have a table:
Id|Name |orders|units
1 |Tom |6 |4
2 |Lucy |4 |8
3 |Frank |3 |7
4 |Jane |2 |5
5 |Robert|1 |NULL
I want the output as:
Id|units
1 |4
4 |5
5 |0
8 |0
9 |0
Can you help me with the query?
I'm not sure whether this meets your requirements because I have no idea where & why your result set misses IDs 2 and 3. Anyway, here you are: full outer join with a fabricated set of IDs from 1 to 9.
SQL> with test (id, units) as
2 (select 1, 4 from dual union
3 select 2, 8 from dual union
4 select 3, 7 from dual union
5 select 4, 5 from dual union
6 select 5, null from dual
7 )
8 select x.column_value id, nvl(t.units, 0) units
9 from test t full outer join
10 table(cast(multiset(select level from dual
11 connect by level < 10
12 ) as sys.odcinumberlist)) x
13 on x.column_value = t.id;
ID UNITS
---------- ----------
1 4
2 8
3 7
4 5
5 0
6 0
7 0
8 0
9 0
9 rows selected.
SQL>
add IFNULL to the query. Like IFNULL(Units, 0)

Multiple row show in one row as sum in sql statement

ID |A |B
1 1 3
1 412 2
2 567 3
2 567 1
3 2 3
3 5 4
4 6 1
4 8 2
4 2 3
I want to get table:
ID |A |B
1 413 5
2 1134 4
3 7 7
4 16 6
As was pointed out, you want to use a group by clause for aggregate functions. In this case, you are summing the values for A and B, and grouping them by ID. The syntax is as follows:
SELECT ID, SUM(A), SUM(B)
FROM tablename
GROUP BY ID

create new column and set increment by step of 3 rows

I have table1, i want get table2 ? increment by step of 3 rows.
I want create same |B value for first 3 rows, then increment +1 for second's 3 rows
table1
ID |A
1 125
2 412
3 567
4 567
5 485
6 458
7 656
8 856
9 456
table2
ID |A |B
1 125 101
2 412 101
3 567 101
4 567 102
5 485 102
6 458 102
7 656 103
8 856 103
9 456 103
In MySQL you can use something like this:
SET #c := -1;
SELECT id, A, (#c := #c+1) DIV 3 + 101 AS B FROM table1
You can use either create table from select or insert from select.
You can achieve it using MySql's mathematical functions, e.g.:
select t.id, t.a, 101 + floor((#rn:=#rn+1)/3) as B
from temp t, (SELECT #rn:=-1) t2;
Here is the SQL Fiddle.

How to remove repeated data from the result of joins of 2 different tables?

There are 3 tables in my database, these are the following:
1. table1 (po)
po_id other_col_data
1 other col data
2 other col data
3 other col data
4 other col data
5 other col data
6 other col data
2. table2 (po_items)
po_id |poitems |amount
========+=======+==========
1 |aaaa |200
1 |bbbbb |150
2 |ccccc |50
2 |ddddd |250
3 |eeeee |325
3 |fffff |520
3 |ggggg |120
3. table3 (inv_items)
po_id |invitems|amount
========+=======+==========
1 |aaaa |200
1 |bbbbb |150
1 |ccccc |50
2 |ddddd |250
3 |eeeee |325
3 |fffff |520
3 |ggggg |120
1 |ccccc |50
2 |ddddd |250
3 |eeeee |325
1 |kkkkk |50
2 |ddddd |250
3 |eeeee |325
and I want to result in below format:
Result
ID ITEM PO-Amount INV-Amount
====+======+==========+==========
1 aaaaa 200 200
1 aaaaa 150 150
1 ccccc 00 50
1 kkkkk 00 50
Try this query:
SELECT DISTINCT
po_items.po_id AS ID,
if(po_items.poitems = 'NULL' or po_items.poitems = '', inv_items.invitems, po_items.poitems) AS ITEM,
ifnull(po_items.amount,0) AS PO-AMOUNT,
ifnull(inv_items.amount,0) AS INV-AMOUNT
FROM po_items
LEFT OUTER JOIN inv_items ON po_items.po_id = inv_items.po_id
UNION
SELECT DISTINCT
po_items.po_id AS ID,
if(po_items.poitems = 'NULL' or po_items.poitems = '', inv_items.invitems, po_items.poitems) AS ITEM,
ifnull(po_items.amount,0) AS PO-AMOUNT,
ifnull(inv_items.amount,0) AS INV-AMOUNT
FROM po_items
RIGHT OUTER JOIN inv_items ON po_items.po_id = inv_items.po_id
WHERE inv.purchase_order_id = poi.purchase_order_id;

How do update and insert data in one table using the data in another table

I have two tables prod1 and prod2 having same fields (pid,pname,pprice,pcode). Have some data in prod1 table like below
Pid Pname Pprice Pcode
--- ----- ------ -----
1 Aaaaa 500 A-1
2 Bbbbb 1000 B-1
3 Ccccc 1500 C-1
And have data in prod2 table like below
Pid Pname Pprice Pcode
--- ----- ------ -----
1 Aaaaa 5000 A-1
2 Bbbbb 5000 B-1
3 Ccccc 1500 C-2
4 Ddddd 2000 D-1
5 Eeeee 500 E-1
I need to update prod1 table using prod2 table
If prid1.pid=prod2.pid
Else
Insert the data into prod1 from prod2
Final aim is to store prod2 data in prod1 without delete in prod1
Help me to solve I tried cases and while etc
Finally Solved My Problem
..........................
update prod1
set pname=b.pname, pprice=b.pprice, pcode=b.pcode
from prod2 b,prod1 a where a.pid=b.pid
select * from prod1
insert into prod1
select * from prod2 b
where b.pid not in(select pid from prod1)
Thanks