Sql query for combination two tables - sql

Let say, I have two tables
Table 1:
code|name
121 ab
456 qw
Table 2 :
Kode_ak|Nama_ak
1 Bursa
2 Kustodian
3 Kliring
4 Assurance
5 Bunga
I want to combine those two tables. The result will be like this:
Kode_AK|Nama_ak |code|name
1 Bursa 121 ab
2 Kustodian 121 ab
3 Kliring 121 ab
4 Assurance 121 ab
5 Bunga 121 ab
1 Bursa 456 qw
2 Kustodian 456 qw
3 Kliring 456 qw
4 Assurance 456 qw
5 Bunga 456 qw
How can I do it?

You would do this using cross join:
select *
from table1 cross join
table2;

select t3.Kode_AK,t3.Nama_ak,t1.code,t1.name from [dbo].[Table_1] t1 cross join [dbo].[Table_3] t3

Related

looking for values from another table where they do not exist in a given group

I have two tables:
SHOPPING
date
id_customer
id_shop
id_fruit
28.03.2018
7423
123
1
13.02.2019
8408
354
1
28.03.2019
7767
123
9
13.02.2020
8543
472
7
28.03.2020
8640
346
9
13.02.2021
7375
323
9
28.03.2021
7474
323
8
13.02.2022
7476
499
1
28.03.2022
7299
123
4
13.02.2023
8879
281
2
28.03.2023
8353
452
1
13.02.2024
8608
499
6
28.03.2024
8867
318
1
13.02.2025
7997
499
6
28.03.2025
7715
499
4
13.02.2026
7673
441
7
FRUITS
id_fruit
name
1
apple
2
pear
3
grape
4
banana
5
plum
6
melon
7
watermelon
8
orange
9
pineapple
I would like to find fruits that have never been bought in a specific id_shop
I tried with this:
SELECT
s.idshop,
s.id_fruit ,
f.name
FROM
shopping s
LEFT JOIN fruit f ON f.id_fruit = s.id_fruit
WHERE NOT EXISTS (
SELECT *
FROM
fruit f1
WHERE f1.id_fruit = s.id_fruit
)
but it does not work...
Yes, you need an OUTER JOIN, but that should be RIGHT JOIN along with NULL values picked from shopping table after join applied, considering your current query such as
SELECT f.*
FROM shopping s
RIGHT JOIN fruit f
ON f.id_fruit = s.id_fruit
WHERE s.id_fruit IS NULL
Demo

Count only original seconds with Oracle SQL

I have a table with this structure and data, with start and stop positions of an audio/video. I have to count the original seconds and discard the not original ones.
E.g.
CUSTOMER_ID ITEM_ID CHAPTER_ID START_POSITION END_POSITION
A 123456 1 6 0 97
B 123456 1 6 97 498
C 123456 1 6 498 678
D 123456 1 6 678 1332
E 123456 1 6 1180 1190
F 123456 1 6 1190 1206
G 123456 1 6 1364 1529
H 123456 1 6 1530 1531
Original Data
Lines "E" and "F" does not represent original seconds because "D" line starts at 678 and finishes with 1332 so I need to create a new set of lines like this:
CUSTOMER_ID ITEM_ID CHAPTER_ID START_POSITION END_POSITION
A 123456 1 6 0 97
B 123456 1 6 97 498
C 123456 1 6 498 678
D 123456 1 6 678 1332
E 123456 1 6 1364 1529
F 123456 1 6 1530 1531
New Result Set
Can you help mw with this?
If I am following you correctly, you can use not exists to filter out rows whose range is contained in the range of another row:
select t.*
from mytable t
where not exists (
select 1
from mytable t1
where
t1.customer_id = t.customer_id
and t1.start_position < t.start_position
and t1.end_position > t.end_position
)
You can use the self join as follows:
Select distinct t.*
from your_table t
Left Join your_table tt
On t.customer_id = tt.customer_id
And t.item_id = tt.item_id
And t.chapter_id = tt.chapter_id
And t.rowid <> tt.rowid
And t.start_position between tt.start_position and tt.end_position - 1
Where tt.rowid is null

Exclude rows where keys match, but are on different rows

I'm looking for the best way to produce the result set in the scenario provided. My cust3 column isn't identifying the repeated values in the indvid2 column. The end result I'm looking for is to exclude the rows where key1 and key2 match (ids:1,2,6 and 7), then sum accounts where the acctids match.If there's a better way to code this, I welcome all suggestions. Thanks!
WITH T10 as (
SELECT acctid,invid,(
case
when invid like '%-R' then left (InvID,LEN(invid) -2) else InvID
END) as InvID2
FROM table x
GROUP BY acctID,invID
),
T11 as (
SELECT acctid, Invid2, COUNT(InvID2) as cust3
FROM T10
GROUP BY InvID2,acctid
HAVING
COUNT (InvID2) > 1
)
select DISTINCT
a.acctid,
a.name,
b.invid,
C.invid2,
D.cust3,
b.amt,
b.key1,
b.key2
from table a
inner join table b (nolock) on a.acctid = b.acctid
inner join T10 C (nolock) on b.invid = c.invid
inner join T11 D (nolock) on C.invid2 = D.invid2
Resultset
id acctID name invid invid2 Cust3 amt key1 key2
1 123 James 101 101 2 $500 NULL 6789
2 123 james 101-R 101 2 ($500) 6789 NULL
3 123 James 102 102 2 $350 NULL NULL
4 123 James 103 103 2 $200 NULL NULL
5 246 Tony 98-R 98 2 ($750) 7423 NULL
6 432 David 45 45 2 $100 NULL 9634
7 432 David 45-R 45 2 ($100) 9634 NULL
8 359 Stan 39-R 39 2 ($50) 6157 NULL
9 753 George 95 95 2 $365 NULL NULL
10 753 George 108 108 2 $100 NULL NULL
Desired Resultset
id acctID name invid invid2 Cust3 amt key1 key2
1 123 James 101 101 2 $500 NULL 6789
2 123 james 101-R 101 2 ($500) 6789 NULL
3 123 James 102 102 1 $350 NULL NULL
4 123 James 103 103 1 $200 NULL NULL
5 246 Tony 98-R 98 1 ($750) 7423 NULL
6 432 David 45 45 2 $100 NULL 9634
7 432 David 45-R 45 2 ($100) 9634 NULL
8 359 Stan 39-R 39 1 ($50) 6157 NULL
9 753 George 95 95 1 $365 NULL NULL
10 753 George 108 108 1 $100 NULL NULL
Then to sum amt by acctid
id acctid name amt
1 123 James $550
2 246 Tony ($750)
3 359 Stan ($50)
4 753 George $465
Something like:
;WITH Keys as (
SELECT Key1.acctID, [Key] = Key1.Key1
FROM YourTable as Key1
INNER JOIN YourTable as Key2
ON Key1.Key1 = Key2.Key2 and Key1.acctID = Key2.acctID
)
SELECT t.acctID, t.name, amt = SUM(t.amt)
FROM YourTable as t
LEFT JOIN Keys as k
ON t.acctID = k.acctID and (t.Key1 = [Key] or t.Key2 = [Key])
WHERE k.acctID is Null
GROUP BY t.acctID, t.name

Pandas/SQL join

I would like to add some data (event_date) from table B to table A, as described below. It looks like a join on event_id, however this column contains duplicate values in both tables. There are more columns in both tables but I'm omitting them for clarity.
How to achieve the desired effect in Pandas and in SQL in the most direct way?
Table A:
id,event_id
1,123
2,123
3,456
4,456
5,456
Table B:
id,event_id,event_date
11,123,2017-02-06
12,456,2017-02-07
13,123,2017-02-06
14,456,2017-02-07
15,123,2017-02-06
16,123,2017-02-06
Desired outcome (table A + event_date):
id,event_id,event_date
1,123,2017-02-06
2,123,2017-02-06
3,456,2017-02-07
4,456,2017-02-07
5,456,2017-02-07
Using merge, first drop duplicates from B
In [662]: A.merge(B[['event_id', 'event_date']].drop_duplicates())
Out[662]:
id event_id event_date
0 1 123 2017-02-06
1 2 123 2017-02-06
2 3 456 2017-02-07
3 4 456 2017-02-07
4 5 456 2017-02-07
SQL part:
select distinct a.*, b.event_date
from table_a a
join table_b b
on a.event_id = b.event_id;
You can use Pandas Merge to get the desired result. Finally get only the columns that you are interested from DataFrame
df_Final = pd.merge(df1,df2,on='event_id',how='left')
print df_Final[['id_y','event_id','event_date']]
output
id_y event_id event_date
0 1 123 2017-02-06
1 2 123 2017-02-06
2 3 456 2017-02-07
3 4 456 2017-02-07
4 5 456 2017-02-07
5 1 123 2017-02-06
6 2 123 2017-02-06
7 3 456 2017-02-07
8 4 456 2017-02-07
9 5 456 2017-02-07
10 1 123 2017-02-06
11 2 123 2017-02-06
12 1 123 2017-02-06
13 2 123 2017-02-06

Need to join three tables. Not sure which join fits the req

table 1
AcctNO CustomerId Role Name
1 123 A ABC
2 121 B BCA
3 321 C CBA
table 2
AcctNo CustomerId Role Address
1 123 A 1/12
2 121 B 11/3
4 231 C 12-1
3 321 C 111
5 221 C 121
table 3
AcctNo CustomerId Role CompanyName
4 231 C hello
5 221 C bello
3 321 C cello
output should be as follows
AcctNo CustomerId Role Name Address COmpanyName
1 123 A ABC 1/12 NULL
2 121 B BCA 11/3 NULL
3 321 C CBA 111 cello
4 231 C NULL 12-1 hello
5 221 C NULL 121 bello
Use a left join between the tables on a unique column like the CustomerID.
A left join is used instead of a inner join so you get any missing values as a null instead of missing a record.