For example, I have the following table in SQL Server:
name
from
to
traveling date
Mike
london
Paris
5/jan/2022
Mike
Paris
Barcelona
5/jan/2022
Sam
Cairo
Riyadh
6/Mar/2022
Sam
Riyadh
Dubai
6/Mar/2022
Sam
Dubai
Maldives
7/Mar/2022
Sam
Maldives
Riyadh
13/Mar/2022
Sam
Riyadh
Cairo
13/Mar/2022
And the result I want must have new columns based on other columns and rows, like below:
name
from
to
traveling date
Route
Date
Mike
London
Paris
5/Jan/2022
London-Barcelona
5/Jan/2022
Mike
Paris
Barcelona
5/Jan/2022
Sam
Cairo
Riyadh
6/Mar/2022
Cairo-Maldives
6/Mar/2022
Sam
Riyadh
Dubai
6/Mar/2022
Sam
Dubai
Maldives
7/Mar/2022
Sam
Maldives
Riyadh
13/Mar/2022
Maldives-Cairo
13/Mar/2022
Sam
Riyadh
Cairo
13/Mar/2022
As you can see Sam had Round ticket he reached the next day of traveling and stayed for couple of days in Maldives.
All I care about is to consider Route as 1 ticket and have the traveling date from 'Date'.
Help please.
Related
I have a table called delegation_table as follows:
delegator | delegatee
josh | ryan
carl | nick
sam | john
There is another table called permission_table as follows:
username | location | branch | office
josh | usa |New York | downtown
carl | asia | India | north
sam | usa |California| midtown
For each of the delegators from the delegation_table, I want to get their details from the permission_table and insert a row with the same details with username as the respective delegatee.
For example - I want to find details of josh which is:
josh | usa |New York | downtown
and insert this row in the permission_table:
ryan | usa |New York | downtown
Something like this:
insert into permission_table
(username, location, branch, office)
select 'ryan' as username, location, branch, office
from permission_table
where username = 'josh'
But I don't want to hard code the values and want to run this for every delegator in delegation_table. How do I do that??
This seems like a join:
insert into permission_table (username, location, branch, office)
select dt.delegatee, location, branch, office
from permission_table pt join
delegation_table dt
on dt.delegator = pt.username;
I would like to get a cartesian product of several tables in SQL (which are actually only one column, so no common key). For example:
TABLE A
Robert
Pierre
Samuel
TABLE B
Montreal
Chicago
TABLE C
KLM
AIR FRANCE
FINAL TABLE (CROSS PRODUCT)
Robert | Montreal | KLM
Pierre | Montreal | KLM
Samuel | Montreal | KLM
Robert | Chicago | KLM
Pierre | Chicago | KLM
Samuel | Chicago | KLM
Robert | Montreal | AIR FRANCE
Pierre | Montreal | AIR FRANCE
Samuel | Montreal | AIR FRANCE
Robert | Chicago | AIR FRANCE
Pierre | Chicago | AIR FRANCE
Samuel | Chicago | AIR FRANCE
I tried CROSS JOIN, but I couldn't find an example with multiple tables. Is the only way to do it is nesting? What if we have 15 tables to join that way... it creates a very long code.
Thank you!
You would simply use:
select *
from a cross join b cross join c;
Do note that if any of the tables are empty (i.e. no rows), you will get no results.
I'm trying to see if I can assign an id to a name every time that same record comes in. For instance, if I assign an ID E001 to John Doe, I want to be able to assign that same value to John every time I see a record of John. And if a new person comes into the picture, I want to pick the next ID from a list I created (table ID) and assign it to that person and so on. I'm open to suggestions or ideas or better ways on how to go about doing it either SQL (preferably) or some ETL approach. Thanks
Month 1 file (or another table in oracle)
Name Addr Location
------------------------------
John Doe 123 Main NA
John Doe 123 Main NA
Bob Lava 456 Tel US
ORACLE_TABLE in month 1
ID Name Addr Location
-------------------------------------
E001 John Doe 123 Main NA
E001 John Doe 123 Main NA
E002 Bob Lava 456 Tel US
Month 2 files (or oracle table)
Name Addr Location
-------------------------------
Denny Fry 456 str AB
John Doe 123 Main NA
John Doe 123 Main NA
Bob Lava 456 Tel US
ORACLE TABLE in month 2 (simply append month 2 files to existing data but using previously assigned ID for existing records and a new id for a new record)
ID Name Add Location
------------------------------------
E001 John Doe 123 Main NA
E001 John Doe 123 Main NA
E002 Bob Lava 456 Tel US
E003 Denny Fry 456 str AB
E001 John Doe 123 Main NA
E001 John Doe 123 Main NA
E002 Bob Lava 456 Tel US
ID table list (used to assign IDs to records)
E001
E002
E003
E004
B001
I can suggest a SQL solution in which first do union all and then you can use dense_rank() as following
select
Name,
Addr,
Location,
'E00' || dense_rank() over (order by Name) as EmpId
from
(
select
Name,
Addr,
Location
from monthOne
union all
select
Name,
Addr,
Location
from monthTwo
) val
order by
EmpId
output:
*-----------------------------------*
| NAME ADDR LOCATION EMPID |
*-----------------------------------*
|Bob Lava 456 Tel US E001 |
|Bob Lava 456 Tel US E001 |
|Denny Fry 456 str AB E002 |
|John Doe 123 Main NA E003 |
|John Doe 123 Main NA E003 |
|John Doe 123 Main NA E003 |
|John Doe 123 Main NA E003 |
*-----------------------------------*
How can I get all the countries from the DB, from this table:
city | country | info
Jerusalem | Israel | Capital
Tel Aviv | Israel |
New York | USA | Biggest
Washington DC | USA | Capital
Berlin | Germany | Capital
How can I get, using SQL, the countries only: Israel, USA, Germany?
Which database server are you using?
Assuming that the top row is the column name and you are using MySQL then you should be able to just do
"SELECT distinct(country) FROM <table-name>;"
This is probably in the documentation for the database software that you are using.
I have 3 tables. One with user names, one with a bunch of data, and then one that links the 2 tables together via their ID's. When I try and join the 3 with an INNER JOIN, I get a bunch of duplicate rows. For example, if the data table only has 2000 rows, once I try and use the inner join to connect the 3 tables, I get an output that shows 100,000 rows. Does this have something to do with the fact that my Data table has more rows than my User name table and the inner join doesn't play nicely? Is it possible to inner join one table that has more rows than the other?
EDIT FOR CLEARITY
For this database I import data that looks something like this:
-Names-----------Numbers-----------Item---------------Cost---
John Smith 111-1111 Pencils 100.75
Joe Stevens 222-2222 Paper 35.34
Mike Jones 333-3333 Staples 23.86
John Smith 111-1111 Boxes 11.76
As you can see John Smith is listed twice, for two separate items. I would like to optimize this, but separating the original data and putting things in their own table with only one iteration.
ID---Name--- ID----Numbers ID---Items------Cost
1 John Smith 1 111-1111 1 Pencil 100.75
2 Joe Stevens 2 222-2222 2 Paper 35.34
3 Mike Jones 3 333-3333 3 Staples 23.86
4 Boxes 11.76
Now that things are optimized, I need to be able to still run a query that could match John Smith with both the pencils and the boxes. This is why I created the link table that holds the IDs in the correct sequence they need to be in.
Name.ID Number.ID Item.ID
1 1 1
2 2 2
3 3 3
1 1 4
Now if I wanted to run a report that recreates the original data dump, I would run a query that just joins the 3 tables with an inner join. The issue though, is that I'm getting results with tons of duplicates, rather than just simply outputting that data as it should be. I get something like this:
-Names-----------Numbers-----------Item---------------Cost---
John Smith 111-1111 Pencils 100.75
John Smith 111-1111 Pencils 100.75
John Smith 111-1111 Pencils 100.75
John Smith 111-1111 Pencils 100.75
John Smith 111-1111 Pencils 100.75
Joe Stevens 222-2222 Paper 35.34
Joe Stevens 222-2222 Paper 35.34
Joe Stevens 222-2222 Paper 35.34
Joe Stevens 222-2222 Paper 35.34
Mike Jones 333-3333 Staples 23.86
Mike Jones 333-3333 Staples 23.86
Mike Jones 333-3333 Staples 23.86
Mike Jones 333-3333 Staples 23.86
John Smith 111-1111 Boxes 11.76
John Smith 111-1111 Boxes 11.76
John Smith 111-1111 Boxes 11.76
John Smith 111-1111 Boxes 11.76
The desired output would simply be:
-Names-----------Numbers-----------Item---------------Cost---
John Smith 111-1111 Pencils 100.75
Joe Stevens 222-2222 Paper 35.34
Mike Jones 333-3333 Staples 23.86
John Smith 111-1111 Boxes 11.76
try this one
Select Distinct t1.Names,t2.Number,t3.Item,t3.Cost
From
tableName t1
inner join tableNum t2
on t1.ID = t2.ID
inner join tableItem t3
on t1.ID = t3.ID