SQL How to access element of a different table? - sql

I have this table [pets]
Animal
prev_store
curr_store
Cat
ABC
DEF
Dog
ABC
GHI
Fish
DEF
XYZ
Snake
XYZ
JKM
I also have this other table [pet_store]
Store
Country
ABC
England
DEF
Denmark
GHI
England
XYZ
Denmark
JKM
Denmark
I want to check for each animal in pets table, whether the prev_store and curr_store is in the same Country, and if so, make a record of the Country.
Country
Occurrences
England
1
Denmark
2
SELECT pet_store.Country, count(pet_store.Country)
FROM pet_store, pets
WHERE pets.prev_store = pet_store.Store
and pets.curr_store = pet_store.Store
GROUP BY pet_store.Country
Unsure about how I would then select the animals's Country in relevance to the prev and curr store.

You will have to use pets_store twice, in two roles, in the query:
WITH
-- your data, don't use in query ..
pets(Animal,prev_store,curr_store) AS (
SELECT 'Cat' ,'ABC','DEF'
UNION ALL SELECT 'Dog' ,'ABC','GHI'
UNION ALL SELECT 'Fish' ,'DEF','XYZ'
UNION ALL SELECT 'Snake','XYZ','JKM'
)
,
pets_store(Store,Country) AS (
SELECT 'ABC','England'
UNION ALL SELECT 'DEF','Denmark'
UNION ALL SELECT 'GHI','England'
UNION ALL SELECT 'XYZ','Denmark'
UNION ALL SELECT 'JKM','Denmark'
)
-- real query starts here ...
SELECT
prev_stores.country AS country
, COUNT(*) AS occurrences
FROM pets
JOIN pets_store prev_stores ON prev_store = prev_stores.store
JOIN pets_store curr_stores ON curr_store = curr_stores.store
WHERE prev_stores.country = curr_stores.country
GROUP BY prev_stores.country;
-- out country | occurrences
-- out ---------+-------------
-- out Denmark | 2
-- out England | 1

Related

Dynamic Pivoting in SQL - Snowflake

I have an input file
Customer PhoneNum Location Brand
John 1234 ABC Oppo
John 1234 DEF MI
John 1234 KLM RealMe
John 1234 LKM 1+
Joe 9934 ABC Apple
Joe 9934 DEF Samsung
The same phone number can be listed to multiple phone brands and the number of brands per phone number can be dynamic i.e. some can have 2 brands some can have 4 some 8 etc. I can pass the list of unique brands in the pivot query but that would create columns which might not have values.
the result i want is
Customer PhoneNum Brand1 Brand1Location Brand2 Brand2Location Brand3 Brand3Location Brand4 Brand4Location
John 1234 Oppo ABC MI DEF RealMe KLM 1+ LKM
Joe 9934 Apple ABC Samsung DEF
```
Here i dont need the list of brands but if i know the maximum record per number is say 4 i can have the output in above format, which I believe is a good way to read the result.
Is there any way in SQL to get the above result.
select * from phone_multiple_make
pivot(max(location),max(brand) for brand in
('MI','Oppo','RealMe') )as p;
If you're ok with not using a pivot function you can acheive your results like this:
WITH CTE AS (
SELECT 'John' CUSTOMER,1234 PHONENUM,'ABC' LOCATION,'Oppo' BRAND
UNION
SELECT 'John' CUSTOMER,1234 PHONENUM,'DEF' LOCATION,'MI' BRAND UNION
SELECT 'John' CUSTOMER,1234 PHONENUM,'KLM' LOCATION,'RealMe' BRAND UNION
SELECT 'John' CUSTOMER,1234 PHONENUM,'LKM' LOCATION,'1+' BRAND UNION
SELECT 'Joe' CUSTOMER,9934 PHONENUM,'ABC' LOCATION,'Apple' BRAND UNION
SELECT 'Joe' CUSTOMER,9934 PHONENUM,'DEF' LOCATION,'Samsung' BRAND )
SELECT CUSTOMER, PHONENUM
,J:BRAND1:BRAND::STRING BRAND1, J:BRAND1:LOCATION::STRING LOCATION1
,J:BRAND2:BRAND::STRING BRAND2, J:BRAND2:LOCATION::STRING LOCATION2
,J:BRAND3:BRAND::STRING BRAND3, J:BRAND3:LOCATION::STRING LOCATION3
,J:BRAND4:BRAND::STRING BRAND4, J:BRAND4:LOCATION::STRING LOCATION4
FROM (
SELECT CUSTOMER, PHONENUM, OBJECT_AGG(KEY,OBJ) J FROM (
SELECT CUSTOMER, PHONENUM
,'BRAND'||ROW_NUMBER()OVER(PARTITION BY CUSTOMER,PHONENUM ORDER BY BRAND)::STRING KEY
,OBJECT_CONSTRUCT( 'LOCATION', LOCATION, 'BRAND',BRAND) OBJ FROM CTE) GROUP BY 1,2)

How to search a string inside a string in Oracle

I have a table A with columns Country ID ,Country. and data is like this
US United States
IN India
JP Japan
NP Nepal
etc .
I have different table B which has a column Country which mostly has free text data . Data is like
Texas United States
India KA
XYS Japan WYS
EverestNepal
XYZ
etc.
What i want is that if country column in Table B has a country matching from any column of country in Table A , it should return Country from Table A.
So for the example i gave
Table B has Texas United States --- there is a match in Table A with 'United States' : It should print United States
Table B has India KA ---- there is a match in Table A with 'India':it should print India
EverestNepal --- there is a match in Table A with 'Nepal': it should print Nepal
Table B has
and so on.
To summarize : If the exact match of country in Table B from anywhere in the string is found in Table A, it should print country from Table A
I dont think Like , IN , Substring will work in such situation
INSTR (line #17) is what you probably need.
SQL> with
2 ta (country_id, country) as
3 (select 'US', 'United States' from dual union all
4 select 'IN', 'India' from dual union all
5 select 'JP', 'Japan' from dual union all
6 select 'NP', 'Nepal' from dual
7 ),
8 tb (country) as
9 (select 'Texas United States' from dual union all
10 select 'India KA' from dual union all
11 select 'XYS Japan WYS' from dual union all
12 select 'EverestNepal' from dual union all
13 select 'XYZ' from dual
14 )
15 select b.country B_country,
16 a.country A_country
17 from ta a right join tb b on instr(b.country, a.country) > 0;
B_COUNTRY A_COUNTRY
------------------- -------------
Texas United States United States
India KA India
XYS Japan WYS Japan
EverestNepal Nepal
XYZ
SQL>
You can use like operator to join between the tables:
SELECT
A.COUNTRY
FROM
TABLE_A A
JOIN TABLE_B B ON ( A.COUNTRY LIKE '%'
|| B.COUNTRY
|| '%' );
Cheers!!

Ordering Query for Country & City data for the scenario given

My input data is below :
**Country city**
Australia Sydney
Australia melbourne
India Delhi
India Chennai
India Bangalore
Afghanistan Kabul
Output expected is:
Afghanistan
Kabul
Australia
melbourne
syndey
India
Bangalore
Chennai
Delhi
The data in both columns should be arranged alphabetically(both city level and country level) and result should be single column with above values. The country should be alphabetically ordered and the corresponding cities should go below them which should also be alphabetically ordered.
How can this be done without using an intermediate table in a single query?
You need a UNION ALL query to get one row per country and one row per city in your result:
select coalesce(city, country) as location
from
(
select distinct country, null as city from mytable
union all
select country, city from mytable
)
order by country, city nulls first;
This has a single table scan and also does not need to use UNION to get distinct results:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE cities ( Country, city ) AS
SELECT 'Australia', 'Sydney' FROM DUAL UNION ALL
SELECT 'Australia', 'melbourne' FROM DUAL UNION ALL
SELECT 'India', 'Delhi' FROM DUAL UNION ALL
SELECT 'India', 'Chennai' FROM DUAL UNION ALL
SELECT 'India', 'Bangalore' FROM DUAL UNION ALL
SELECT 'Afghanistan', 'Kabul' FROM DUAL;
Query 1:
SELECT value
FROM (
SELECT c.*,
country AS ctry,
ROW_NUMBER() OVER ( PARTITION BY Country ORDER BY city ) AS rn
FROM cities c
)
UNPIVOT( value FOR key IN ( Country AS 1, City AS 2 ) )
WHERE rn = 1 OR key = 2
ORDER BY ctry, rn, key
Results:
| VALUE |
|-------------|
| Afghanistan |
| Kabul |
| Australia |
| Sydney |
| melbourne |
| India |
| Bangalore |
| Chennai |
| Delhi |

How to join two table in sql server only with one row (see details) [duplicate]

Imagine I have two tables, food and people, and I indicate who likes which food with a link table. So:
foods
-----
sausages
pie
Mars bar
people
------
john
paul
george
ringo
person | food (link table)
-------+-----
john | pie
john | sausage
paul | sausage
I'd like to get a list of foods, along with a person who likes that food. So I'd like a table like this:
food | a randomly chosen liker
---------+------------------------
sausage | john (note: this could be "paul" instead)
pie | john (note: must be john; he's the only liker)
Mars bar | null (note: nobody likes it)
Is it possible to do this in one query?
Obviously, I can do:
select
f.food, p.person
from
food f inner join link l
on f.food = l.food
inner join person p
on l.person = p.person
but that will give me two sausage rows, because two people like it, and I'll have to deduplicate the rows myself.
Do LEFT JOINs to also get food that no-one likes. GROUP BY to get each food only once, use MIN to pick first person that likes that food.
select f.food, min(p.person)
from food f
left join linktable l on f.id = l.food_id
left join people p on p.id = l.person_id
group by f.food
select f.food, min(l.person)
from food f
left join link l on f.foods = l.food
group by f.food
I would use a partition to do that, example:
WITH ORDERED AS
(
SELECT
PERSON,
FOOD,
ROW_NUMBER() OVER (PARTITION BY lower(FOOD) ORDER BY lower(PERSON) DESC) AS RN
FROM
(
SELECT 'john' AS PERSON ,'pie' AS FOOD FROM DUAL UNION
SELECT 'john1' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john2' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john3' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john4' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john5' AS PERSON ,'eggs' AS FOOD FROM DUAL UNION
SELECT 'john6' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'dada' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'paul' AS PERSON ,'sausage' AS FOOD FROM DUAL
-- Your select statement here that links the two tables
) PERSON_FOOD
)
SELECT
FOOD,
PERSON
FROM
ORDERED
WHERE
RN = 1
This will get you the following:
FOOD | PERSON
-------------------
eggs | john
pie | john
sausage | paul
This is in oracle syntax
Another variant.. (Assuming it is SQL Server )
Select
a.Food, b.Person
from
foods a
outer apply
(
Select top 1 Person from linkTable b where a.Food = b.Food
) b

SQL: Fetching one row across a link table join

Imagine I have two tables, food and people, and I indicate who likes which food with a link table. So:
foods
-----
sausages
pie
Mars bar
people
------
john
paul
george
ringo
person | food (link table)
-------+-----
john | pie
john | sausage
paul | sausage
I'd like to get a list of foods, along with a person who likes that food. So I'd like a table like this:
food | a randomly chosen liker
---------+------------------------
sausage | john (note: this could be "paul" instead)
pie | john (note: must be john; he's the only liker)
Mars bar | null (note: nobody likes it)
Is it possible to do this in one query?
Obviously, I can do:
select
f.food, p.person
from
food f inner join link l
on f.food = l.food
inner join person p
on l.person = p.person
but that will give me two sausage rows, because two people like it, and I'll have to deduplicate the rows myself.
Do LEFT JOINs to also get food that no-one likes. GROUP BY to get each food only once, use MIN to pick first person that likes that food.
select f.food, min(p.person)
from food f
left join linktable l on f.id = l.food_id
left join people p on p.id = l.person_id
group by f.food
select f.food, min(l.person)
from food f
left join link l on f.foods = l.food
group by f.food
I would use a partition to do that, example:
WITH ORDERED AS
(
SELECT
PERSON,
FOOD,
ROW_NUMBER() OVER (PARTITION BY lower(FOOD) ORDER BY lower(PERSON) DESC) AS RN
FROM
(
SELECT 'john' AS PERSON ,'pie' AS FOOD FROM DUAL UNION
SELECT 'john1' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john2' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john3' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john4' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'john5' AS PERSON ,'eggs' AS FOOD FROM DUAL UNION
SELECT 'john6' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'dada' AS PERSON ,'sausage' AS FOOD FROM DUAL UNION
SELECT 'paul' AS PERSON ,'sausage' AS FOOD FROM DUAL
-- Your select statement here that links the two tables
) PERSON_FOOD
)
SELECT
FOOD,
PERSON
FROM
ORDERED
WHERE
RN = 1
This will get you the following:
FOOD | PERSON
-------------------
eggs | john
pie | john
sausage | paul
This is in oracle syntax
Another variant.. (Assuming it is SQL Server )
Select
a.Food, b.Person
from
foods a
outer apply
(
Select top 1 Person from linkTable b where a.Food = b.Food
) b