Is there a Teradata equivalent to R’s rbind? - sql

I’m trying to combine table1 (with columns “id”, “state”, “cost”) and table2 (with same columns “id”, “state”, “cost”) such that all rows are combined into a single table3. There may be duplicates for “id” and/or “state” but all duplicates should be retained.
Table1:
Id
State
Cost
1
IL
50
3
CA
10
2
WY
70
Table2:
Id
State
Cost
4
NY
100
3
PA
15
6
FL
5
Goal table3:
Id
State
Cost
1
IL
50
3
CA
10
2
WY
70
4
NY
100
3
PA
15
6
FL
5
This would be a simple rbind using R but I’m probably overthinking it in Teradata.
Thanks!

Related

Use LEFT OUTER JOIN to include NULL values in query

I want the final query to include manufacturer_id | manufacturer_name | ice_cream_id | ice_cream_name so that the print includes also those manufacturer_names, which are included in the database but do not have any ice creams (NULL ice_cream_names). Then I want the results in ascending order by manufacturer.manufacturer_id, ice_cream.ice_cream_id which i already managed to do.
Here is my sample code and sample header of the dataset I deal with:
SELECT manufacturer.manufacturer_id, manufacturer.manufacturer_name, ice_cream.ice_cream_id, ice_cream.ice_cream_name
FROM ice_cream LEFT OUTER JOIN manufacturer
ON ice_cream.manufacturer_id = manufacturer.manufacturer_id OR manufacturer.manufacturer_name IS NULL
ORDER BY manufacturer.manufacturer_id, ice_cream.ice_cream_id ASC;
manufacturer
manufacturer_id manufacturer_name country
--------------- ----------------- ----------
1 Jen & Berry Canada
2 4 Friends Finland
3 Gelatron Italy
ice_cream
ice_cream_id ice_cream_name manufacturer_id manufacturing_cost
------------ ---------------- --------------- ------------------
1 Plain Vanilla 1 1
2 Vegan Vanilla 2 0.89
3 Super Strawberry 2 1.44
4 Very plain 2 1.2
ingredient
ingredient_id ingredient_name kcal protein plant_based
------------- --------------- ---------- ---------- -----------
1 Cream 400 3 0
2 Coconut cream 230 2.3 1
3 Sugar 387 0 1
4 Vanilla extract 12 0 1
5 Strawberry 33 0.7 1
6 Dark chocolate 535 8 1
contains
ice_cream_id ingredient_id quantity
------------ ------------- ----------
1 1 70
1 3 27
1 4 3
2 2 74
2 3 21
2 4 5
3 1 60
3 3 10
3 5 30
4 2 95
4 4 5
I wonder what's the logic between FROM table1 LEFT OUTER JOIN table 2; Are those in right order? And I think I do something extra in the "ON" stage that should be done in WHERE?
You want to keep all manufacturers according to your description. Hence, that table should be the first table in the LEFT JOIN. I would also suggest using table aliases:
SELECT m.manufacturer_id, m.manufacturer_name, i.ice_cream_id, i.ice_cream_name
FROM manufacturer m LEFT JOIN
ice_cream ic
ON ic.manufacturer_id = m.manufacturer_id
ORDER BY m.manufacturer_id, ic.ice_cream_id ASC;
This doesn't require any fiddling with the ON clause, just proper use of the LEFT JOIN.

Joining player and game tables to get player points

I have the following SQL tables and I'm basically trying to pull a table of every game that Ralph played in for 2018, and the amount of points scored.
Ralph has a unique_id, but may play on multiple teams, or in different positions. Each year that he plays has a new record entered into the player info table for each of those teams and/or positions.
The games data table's player ID may use both of Ralph's player info records, so for instance, records 1 and 2 of game data are both for Ralph, and his actual total points scored is 18 (12 + 6). I don't need those points to be added together, as that can be done easier in PHP, but I do need both records pulled.
------------------------------
Player Info as pi
------------------------------
id | unique_id | year | name | team | pos
1 5000 2018 Ralph 5 F
2 5000 2018 Ralph 5 C
3 5600 2018 Bill 5 G
4 5000 2017 Ralph 4 F
5 2688 2016 Mike 6 G
------------------------------
Game Info as gi
------------------------------
id | team 1 | team 2
1 5 6
2 6 5
3 8 3
4 6 2
------------------------------
Game Data as gd
------------------------------
id | game_info_id | player_id | Points
1 1 1 12
2 1 2 6
3 2 1 4
4 4 5 6
The table should show pi.id, pi.unique_id, gi.id, gd.* WHERE gd.player_id = Any of Ralph's pi.id's AND pi.year=2018
Any help here is appreciated, this seems a bit out of my wheelhouse.
Join the tables like this:
select
pi.id, pi.unique_id, gi.id, gd.*
from playerinfo pi
inner join gameinfo gi on pi.team in (gi.team1, gi.team2)
inner join gamedata gd on gd.game_info_id = gi.id and gd.player_id = pi.id
where pi.name = 'Ralph' and pi.year = 2018

How to implement Relay Teams in a Track & Field Database

I have a track and Field Database with these tables (simplified):
Performance Table
Row Athlete Event Mark Meet
1 1 3 0:55 A
2 2 2 2:25 A
3 3 3 0:54 A
4 4 4 4:10 A
5 2 2 2:11 A
6 3 2 2:12 B
7 1 1 10 C
Athlete Table
Row Name Age Sex
1 Joe 13 M
2 Amy 15 F
3 John 16 M
4 Tim 17 M
So I understand how to implement this for an event with only 1 athlete (e.g. 100 m dash), but how would I include a relay event with 4 athletes. So, for example a 4x400 relay would need 4 athletes. In other words, some events have only 1 athlete and some have more than one. I am not sure if I should use:
Linking Table
Add 4 Columns
Do a table like below.
Other
Option 3 Table
Performance Table (Event 5 is a relay)
Row Athlete Event Mark Meet
1 1 3 0:55 A
2 2 2 2:25 A
3 3 3 0:54 A
4 4 4 4:10 A
5 2 2 2:11 A
6 3 2 2:12 B
7 1 5 9:34 C
8 2 5 9:34 C
9 3 5 9:34 C
10 4 5 9:34 C
Are you going to have events in the system before they are finished? For example, today's meet will include a 4x400 and here are the runners...
If that's the case then you'll need the linking table that you referred to because you want to be able to have that data stand on its own. It would just have the event_id and athlete_id in it so that you could have that set up. That would also be the PK (Primary Key) for the table and you would then use those two columns as the FK (Foreign Key) to the Performance table that you have at the end. If the data will never exist without times then you could just skip the link table and have the Performance table, although having the link table still wouldn't hurt in that case.

How to create a view using relation from a different table

There are two tables STUDENT and RELATION
Student
Roll No | Name | Marks
1 Peter 40
2 Daniel 45
3 Emma 43
4 Drake 47
5 John 49
Relation
Roll No | Younger Sibling
1 2
2 NULL
3 NULL
4 3
5 NULL
Now i want to create a view which shows data like this
Roll No | Marks | Sibling Marks
1 40 45
2 45 0
3 43 0
4 47 43
5 49 0
Assume that there can only be max two siblings in the table and Roll No is the primary key for both tables. Relation between siblings is only one way (younger sibling). I am really new to SQL, any help would be much appreciated.
Try this:
SELECT
R.RolNo,
COALESCE(S1.Marks, 0) Marks,
COALESCE(S2.Marks, 0) SiblingMarks
FROM Relation R
LEFT JOIN Student S1
ON R.RolNo = S.RolNo
LEFT JOIN Student S2
ON R.YoungerSibling = S2.RolNo

SQL - conditional statements in crosstab queries - say what

I am working with MS Access 2007. I have 2 tables: Types of Soda, and Likeability.
Types of Soda are: Coke, Pepsi, Dr. Pepper, and Mello Yellow
Likeability is a lookup with these options: Liked, Disliked, No preference
I know how to count the number of Cokes or Mello Yellows in the table using DCount("[Types]", "[Types of Soda]", "[Types]" = 'Coke')
I also know how to count the number of Liked, Disliked, No preference.
("[Perception]", "[Likeability]", "[Perception]" = 'Liked')
But, what if I need to count the number of "Likes" by Type.
i.e. the table should look like this:
Coke | Pepsi | Dr. Pepper | Mello Yellow
Likes 9 2 12 19
Dislikes 2 45 1 0
No Preference 0 12 14 15
I know in Access I can create a cross tab queries, but my tables are joined by an ID. So my [Likeability] table has an ID column, which is the same as the ID column in my [Types] table. That's the relationship, and that's what connects my tables.
My problem is that I don't know how to apply the condition for counting the likes, dislikes, etc, for ONLY the Types that I specify. It seems like I first have to check the [Likeability] table for "Likes", and cross reference the ID with the ID in the [Types] table.
I am very confused, and you may be too, now. But all I want to do is count the # of Likes and Dislikes for each type of soda.
Please help.
Its not really clear (to me anyway) what your tables look like so lets assume the following
tables
Soda
------
Soda_ID (Long Integer (Increment))
Soda_Name (Text(50)
Perception
------
Perception_ID (Long Integer (Increment))
Perception_Name (Text(50)
Likeability
-----------
Likeability_ID (Long Integer (Increment))
Soda_ID (Long Integer)
Perception_ID (Long Integer)
User_ID (Long Integer)
Data
Soda_Id Soda_Name
------- ---------
1 Coke
2 Pepsi
3 Dr. Pepper
4 Mello Yellow
Perception_ID Perception_Name
------------- ---------
1 Likes
2 Dislikes
3 No Preference
Likeability_ID Soda_ID Perception_ID User_ID
-------------- ------- ------------- -------
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 1 2 2
6 2 2 2
7 3 2 2
8 4 2 2
9 1 3 3
10 2 3 3
11 3 3 3
12 4 3 3
13 1 1 5
14 2 2 6
15 2 2 7
16 3 3 8
17 3 3 9
18 3 3 10
Transform query You could write a query like this
TRANSFORM
Count(l.Likeability_ID) AS CountOfLikeability_ID
SELECT
p.Perception_Name
FROM
Soda s
INNER JOIN (Perception p
INNER JOIN Likeability l
ON p.Perception_ID = l.Perception_ID)
ON s.Soda_Id = l.Soda_ID
WHERE
p.Perception_Name<>"No Preference"
GROUP BY
p.Perception_Name
PIVOT
s.Soda_Name;
query output
Perception_Name Coke Dr_ Pepper Mello Yellow Pepsi
--------------- ---- ---------- ------------ -----
Dislikes 1 1 1 3
Likes 2 1 1 1