How to order by a table based on the other tables column - sql-order-by

i have 2 tables
Table 1
col1 col2
---- ----
BLANK A
D A
V A
BLANK B
D B
V B
Table 2
col1 col2 sex age
---- ---- ---- ----
A as M 45
A sa F 32
A asd F 45
B as M 45
B sa F 32
B asd F 45
my output should be in a order like for each value in col1 of Table 1 should be repeated with the Table2 values and that to in the order and in the second table it should be ordered by Sex and for Female records alone we need to order them by age older first. finally the output table should look like below.
COL1 COL2 COL3 SEX AGE
---- ----- ---- --- ---
BLANK A as M 45
BLANK A asd F 45
BLANK A sa F 32
D A as M 45
D A asd F 45
D A sa F 32
V A as M 45
V A asd F 45
V A sa F 32
BLANK B as M 45
BLANK B asd F 45
BLANK B sa F 32
D B as M 45
D B asd F 45
D B sa F 32
V B as M 45
V B asd F 45
V B sa F 32

If you just want the rows repeating for each value without any join criteria, something like this should do (although I don't have Sybase to test on, it's fairly straight forward SQL);
SELECT t1.col1, t1.col2, t2.col2 AS col3, sex, age
FROM Table1 t1, Table2 t2
ORDER BY t1.col1, sex DESC, age DESC
An SQL Server SQLfiddle as a sample.
EDIT: After your edited question, I assume you want to pair t1.col2 with the corresponding t2.col1, then this ordering should be the correct one;
SELECT t1.col1, t1.col2, t2.col2 AS col3, sex, age
FROM Table1 t1
JOIN Table2 t2
ON t1.col2 = t2.col1
ORDER BY t1.col2, t1.col1, sex DESC, age DESC
Another SQLfiddle.

Related

Google sheet query 2 columns as search key and search

I met some problem with google sheet function.
I have 2 tables. I want to search table1 Date+User as key value in table2.
example:
Date User Unit
2022/05/30 A 109
2022/05/30 B 119
2022/05/30 C 119
2022/05/29 D 109
2022/05/29 E 114
Date User Amount
2022/05/30 A 1
2022/05/30 B 2
2022/05/30 C 3
2022/05/30 D 41
2022/05/30 E 5
2022/05/29 D 6
2022/05/29 E 7
2022/05/29 F 81
2022/05/29 G 9
2022/05/29 A 101
2022/05/29 B 11
2022/05/29 C 121
2022/05/29 D 13
after query I hope the table looks like
Hope Result
Date User Unit Amount
2022/05/30 A 109 1
2022/05/30 B 119 2
2022/05/30 C 119 3
2022/05/29 D 109 6
2022/05/29 E 114 7
This is a sample google sheet
https://docs.google.com/spreadsheets/d/1oxhWMVPt-GziG10agob-xbiNYfKrZVFK9ro0Pj7tn6Y/edit#gid=0
Can I ask for help ?
Many Thanks
Two options. The first pulls all matching combinations of DATE and USER
=ARRAYFORMULA(
QUERY(
{E2:G,
IF(ISBLANK(E2:E),,
IFERROR(
VLOOKUP(
E2:E&"|"&F2:F,
{A2:A&"|"&B2:B,C2:C},
2,FALSE)))},
"select Col1, Col2, Col4, Col3
where Col4 is not null
label
Col1 'Date',
Col2 'User',
Col3 'Amount',
Col4 'Unit'"))
which returns
Date
User
Unit
Amount
2022/05/30
A
109
1
2022/05/30
B
119
2
2022/05/30
C
119
3
2022/05/29
D
109
6
2022/05/29
E
114
7
2022/05/29
D
109
13
The second matches your output exactly, but does omit that second D value for the 29th (13)
=ARRAYFORMULA(
QUERY(
{IFERROR(
VLOOKUP(
UNIQUE(E2:E&"|"&F2:F),
{E2:E&"|"&F2:F,E2:G},
{2,3,4},FALSE)),
IFERROR(
VLOOKUP(
UNIQUE(E2:E&"|"&F2:F),
{A2:A&"|"&B2:B,C2:C},
2,FALSE))},
"where Col4 is not null
format Col1 'yyyy/mm/dd'"))
Both have been added to your sheet. If either of these work out for you, I can break it down.

How to use lead or Lag for a Group in oracle or any other analytic function to get desired result?

input
id name date value
1 aa x v1
1 aa y v1
1 aa z v1
2 bb a v2
2 bb b v2
3 cc c v2
4 dd d v3
4 dd e v3
5 ee f v4
output
id name date value lead value
1 aa x v1 v2
1 aa y v1 v2
1 aa z v1 v2
2 bb a v2 v2
2 bb b v2 v2
3 cc c v2 v3
4 dd d v3 v4
4 dd e v3 v4
5 ee f v4 null
How to get lead value for a id? i.e for id 1 lead value is v2,for id 2 lead value again is v2(as id 3 value is v2), for id 3 lead value is v3 and so on..
You can use the below query to get the desired result:
with
CTE1(ID, VALUE) AS
(select distinct id,
value from table1
order by id
),
CTE2 AS
(
SELECT A.ID,
A.VALUE,
LEAD(A.VALUE) OVER(ORDER BY A.ID) LEAD_VALUE
FROM CTE1 A
)
SELECT A.*, B.LEAD_VALUE
FROM TABLE1 A, CTE2 B
WHERE A.ID = B.ID;
DB_FIDDLE

PIVOT help needed

I have table and data like this in oracle
create table ab (a varchar2(10), b varchar2(10), c varchar2(10) , d number, e number);
insert into ab values ('yuxc', 'x13', 'shjsh', -1, 12345);
insert into ab values ('yuxc', 'x15', 'shjsh', 12345, -1);
and I want data like this
a b1 b2 c d e
yuxc x13 x15 shjsh 12345 12345
please help.
So far one posible solution matching your sample data.
If this is not an expected general solution you must provide more information
select
A, min(B) B1, max(B) B2, C, max(D) D, max(E) E
from ab
group by A,C
A B1 B2 C D E
---------- ---------- ---------- ---------- ---------- ----------
yuxc x13 x15 shjsh 12345 12345

Merging different columns of different tables in one select query in Oracle

I have two tables:
Table: AA with columns A, B , C, D
Table: BB with columns E , F G
I want to get output which is a combination of these two tables like following:
A | B | C | D | E | F | G
I am currently doing following:
Select * from (
( select A, B, C ,D from AA where some condition)
UNION ALL
( select E , F , G , NULL from BB where some condition) )
But it is giving me output which contains only A , B , C , D from AA table while it's not adding E, F , G from table BB.
What am I doing wrong?
not sure exactly what you idea is , but if you want just plain data from AA and BB in same table display then you can use something like this
( select A, B, C ,D, NULL as E, NULL as F, NULL as G from AA where some condition)
UNION ALL
( select NULL as A , NULL as B, NULL as C, NULL as D, E , F , G from BB where some condition) )
but not sure what is the point of this :|
if you want some combination of data then join is must have.
If you're looking for results that look something like this:
A1 B1 C1 D1
A2 B2 C2 D2
A3 B3 C3 D3
E1 F1 G1
E2 F2 G2
then try something like
(select A Fld1, B Fld2, C Fld3, D Fld4 from AA where some condition)
union
(select E Fld2, F Fld2, G Fld3, Null from BB where some condition)
In other words, give a common alias for the fields that are the same between the two selects. And of course, the fields must be the same types.

My oracle merge does not work

I am running
Oracle Database 10g Enterprise Edition Release 10.1.0.5.0 - Prod.
I want to merge data from m2 into m1. I expect to see 3 records
in m1 after the merge, one for 'c' with a knt of 4, one for 'a' with a knt of 1
and one for 'b' with a knt of 1.
But I get everything. As if no checking for update or insert occurred.
See below.
Best regards,
Phil
SQL> desc m1;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> desc m2;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> select * from m1;
no rows selected
SQL> select * from m2;
K V KNT
------ ------ ----------
a aaa 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
b bbb 0
c ccc 0
c ccc 0
SQL> merge into m1 d
2 using (select k,v,knt from m2) s
SQL> desc m1;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> desc m2;
Name Null? Type
----------------------------------------- -------- ----------------------------
K VARCHAR2(6)
V VARCHAR2(6)
KNT NUMBER(4)
SQL> select * from m1;
no rows selected
SQL> select * from m2;
K V KNT
------ ------ ----------
a aaa 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
b bbb 0
c ccc 0
c ccc 0
SQL> merge into m1 d
2 using (select k,v,knt from m2) s
3 on (d.k = s.k)
4 when matched then
5 update set d.knt = d.knt+1
6 when not matched then
7 insert(d.k,d.v,d.knt)
8 values(s.k,s.v,s.knt)
9 ;
SQL> select * from m1;
K V KNT
------ ------ ----------
b bbb 0
b bbb 0
c ccc 0
c ccc 0
c ccc 0
c ccc 0
c ccc 0
a aaa 0
a aaa 0
Are you sure you want a MERGE? It sounds like you really want
INSERT INTO m2( k, v, knt )
SELECT k, v, count(*)
FROM m1
GROUP BY k, v
A MERGE is a set-based operation. The data in M2 is evaluated at the time the query is executed so your USING clause is not going to see the rows that are being inserted as part of the MERGE. Since the USING clause returns 0 rows, all the data from M1 is going to be inserted into M2. The WHEN MATCHED clause is never going to be triggered.
Sigh. It looks like I have to keep the records I am inserting 'distinct'.
So this works. .... kind of.
merge into m1 d
using (select distinct k,v,knt from m2) s
on (d.k = s.k and d.v = s.v)
when matched then
update set d.knt = d.knt+1
when not matched then
insert(d.k,d.v,d.knt)
values(s.k,s.v,s.knt)

Resources