Im a stranger to SQL, I have somehow managed to get this table out but, its still far away from what I need. My table looks like,
Location Bus Type Colour Count Capacity
1. hartford volvo 20 Seater Red 10 5000cc
2. hartford ford 10 seater blue 12 2000cc
3. hartford Merc 20 seater green 12 2000cc
4. kansas lambo 16 Seater Red 13 1000cc
5. kansas banbo 15 Seater blue 13 1000cc
6. kansas babho 17 Seater green 13 1000cc
I want to change the layout to
http://i.stack.imgur.com/MMUgf.png
Please suggest me how can I get the desired layout.Im trying to use pivot function in sql
Related
I have a dataframe of User IDs and Tags as shown below under 'Current Data' .
The Goal:
I want to be able to duplicate records per each value under the tags column. As you can see in the target output, user ID 21 is repeated 3x for each of the three tags that are in the source 'TAGS' - everything is duplicated except the Tag column - 1 Record per item in the comma separated list.
Issue:
I looked at using the SPLIT_TO_TABLE functionality in snowflake but it doesn't work in my use case as not all the tags are consistently in some kind of order and in some cases, the cell is also blank.
Current Data:
USER_ID CITY STATUS PPL TAGS
21 LA checked 6 bad ui/ux,dashboards/reporting,pricing
32 SD checked 9 buggy,laggy
21 ATL checked 9
234 MIA checked 5 glitchy, bad ui/ux, horrible
The target:
USER_ID CITY STATUS PPL TAGS
21 LA checked 6 bad ui/ux
21 LA checked 6 dashboards/reporting
21 LA checked 6 Pricing
32 SD checked 9 buggy
32 SD checked 9 laggy
21 ATL checked 9
234 MIA checked 5 glitchy
234 MIA checked 5 bad ui/ux
234 MIA checked 5 horrible
Sql:
select table1.value
from table(split_to_table('a.b', '.')) as table1
SPLIT_TO_TABLE works. Below is the query using your sample data:
select USER_ID, CITY, STATUS, PPL, VALUE
from (values
(21,'LA','checked',6,'bad ui/ux,dashboards/reporting,pricing')
,(32,'SD','checked',9,'buggy,laggy')
,(21,'ATL','checked',9,'')
,(234,'MIA','checked',5,'glitchy, bad ui/ux, horrible')
) as tbl (USER_ID,CITY,STATUS,PPL,TAGS)
, lateral split_to_table(tbl.tags,',');
Result:
USER_ID CITY STATUS PPL VALUE
21 LA checked 6 bad ui/ux
21 LA checked 6 dashboards/reporting
21 LA checked 6 pricing
32 SD checked 9 buggy
32 SD checked 9 laggy
21 ATL checked 9
234 MIA checked 5 glitchy
234 MIA checked 5 bad ui/ux
234 MIA checked 5 horrible
I'm trying to achieve a result where only one result for each TEAM and each PLACE is returned.
The twist is that the highest result should from each place should have priority.
My table currently looks something like this:
ENTRY_ID TEAM_ID DATE PLACE SCORE
1 1 2021-10-12 Ireland 64
2 2 2021-10-12 Ireland 31
3 3 2021-10-12 France 137
4 2 2021-10-12 France 61
5 5 2021-10-12 France 38
6 1 2021-10-12 France 66
7 2 2021-10-12 Italy 17
8 3 2021-10-12 Italy 61
9 1 2021-10-12 Italy 74
The competition is held at three different places at the same time, with technically all teams being able to have people playing in all of them at the same time.
Each team however can only win one point so, in the example, it's possible to see that Team 1 would win both in Italy and Ireland, but it should be awarded only one point for the highest score, so only Italy. The point in Ireland should go to the second place.
I've tried over 30 queries I've found in several correlated questions, but none of them seems to be applicable to my situation.
Basically:
"Return the highest score on each PLACE, but only calls each TEAM once.
If that certain TEAM was already called, ignore it, get the second place."
So I could retrieve all three winners with no further processing. The results I'm trying to achieve should repeat neither the TEAM_ID nor PLACE, in this particular example it should output:
3 FRANCE (Since it has the highest score in France at 137)
1 ITALY (For the highest score in Italy at 74)
2 IRELAND (For the second-highest score in Ireland, since Team 1 already won in Italy)
The production model of this table has far more entries so it's unlikely there would be any clashes with too many second-places.
How can I achieve that?
I am trying to create a function that averages over both row and column. For example:
**State** **1943 1944 1945 1946 1947 (1947_AVG) 1948 (1948_AVG)**
Alaska 1 2 3 4 5 2 6 3
CA 234 234 234 6677 34
I want a code that will give me an average for 1947 using 1943, 1944, and 1945. Something that gives me 1948 using 1944, 1945, 1946, ect, ect.
I currently have:
d3['pandas_SMA_Year'] = d3.iloc[:,1].rolling(window=3).mean()
But this is simply working over the rows, not the columns, and it doesn't take into account the fact that I'm looking 2 years back. Please and thank you for any guidance!
I have a dataset that looks like this:
ID Item No of items No of items ordered
1 Vauxhall
2 Vauxhall 40
3 Vauxhall 30
4 Vauxhall 60
6 Vauxhall
7 Vauxhall 40
8 Vauxhall 25
9 Vauxhall
10 BMW 30
11 BMW
12 BMW 25
13 BMW
14 BMW 55
15 BMW
16 BMW
17 BMW 20
18 BMW
Each item has nine rows. I would like VBA code or a formula to look at the 'Item' above active cell and if the 'Item' is the same and the 'No of Items' field is blank, I would like it to copy the next available value in 'No of Items' to the cells above which are empty. If there is no next available 'No of Items' for one item then put default value of 100.
I would like the result to look:
ID Item No of items No of Items ordered
1 Vauxhall 40
2 Vauxhall 40
3 Vauxhall 30
4 Vauxhall 60
6 Vauxhall 40
7 Vauxhall 40
8 Vauxhall 25
9 Vauxhall 100
10 BMW 30
11 BMW 25
12 BMW 25
13 BMW 55
14 BMW 55
15 BMW 20
16 BMW 20
17 BMW 20
18 BMW 100
If ID is in A1 then you might try in D2 copied down to suit:
=IF(B1=B2,"Do something","Do something else")
The general principle is the same for VBA (say If ... Else) but VBA has much more scope for what to do or what else to do.
Concerning your edit you'd need to adapt the above formula so it differentiates the last in each group, rather than the first, say in E2 copied down to suit:
=B2<>B3
Filter ColumnE to select TRUE and filter ColumnC to select (Blanks). Fill those with 100 and then fill ColumnC by selecting it, Go To Special..., Blanks (only), =, Down, Ctrl+Enter (see).
Edit:
SQLZoo More Join Operations problem 15 has changed since I asked this question. It now states: "List the films released in the year 1978 ordered by the number of actors in the cast, then by title."
I give thanks to all who tried to help with the original phrasing. I've updated the accepted answer to match the current problem.
Original Question:
I'm trying to solve problem number 15 under SQLZoo More Join Operations (I'm brushing up for an interview tomorrow)
The question is: "List the 1978 films by order of cast list size. "
My answer is:
SELECT movie.title, count(casting.actorid)
FROM movie INNER JOIN casting
ON movie.id=casting.movieid
WHERE movie.yr=1978
GROUP BY movie.id
ORDER BY count(casting.actorid) desc
This is essentially identical to the answer given by Gideon Dsouza except that my solution does not assume titles are unique:
SELECT m.title, Count(c.actorid)
FROM casting c JOIN movie m ON
m.id = c.movieid
WHERE m.yr = 1978
GROUP BY m.title
ORDER BY Count(c.actorid) DESC
Neither my solution nor his is marked correct.
The results from my solution and the "correct" solution are given at the end. My list has two movies ("Piranha" and "The End") that the "correct" solution lacks. And the "correct" solution has two movies ("Force 10 From Navarone" and "Midnight Express") that mine lacks.
Since these movies are all in the smallest cast size, I hypothesized that SQLZoo is cutting off the query at 50 rows and it was an ordering irregularity that causes the difference. However, I tried adding ,fieldname to the end of my order by clause for all values of fieldname but none yielded an identical answer.
Am I doing something wrong or is SQLZoo broken?
Result Listings
My solution yields (after using libreoffice to make a fixed width column):
The Bad News Bears Go to Japan 50
The Swarm 37
Grease 28
American Hot Wax 27
The Boys from Brazil 26
Heaven Can Wait 25
Big Wednesday 21
Orchestra Rehearsal 19
A Night Full of Rain 19
A Wedding 19
The Cheap Detective 19
Go Tell the Spartans 18
Superman 17
Movie Movie 17
The Driver 17
The Cat from Outer Space 17
Death on the Nile 17
The Star Wars Holiday Special 17
Blue Collar 16
J.R.R. Tolkien's The Lord of the 16
Ice Castles 16
International Velvet 16
Coming Home 15
Revenge of the Pink Panther 15
The Brink's Job 15
David 15
The Chant of Jimmie Blacksmith 15
The Water Babies 15
Violette Nozière 15
Occupation in 26 Pictures 15
Without Anesthesia 15
Bye Bye Monkey 15
Alexandria... Why? 15
Who'll Stop The Rain 15
Gray Lady Down 15
Damien: Omen II 14
The Empire of Passion 14
Bread and Chocolate 14
I Wanna Hold Your Hand 14
Closed Circuit 14
Almost Summer 13
Goin' South 13
An Unmarried Woman 13
The Left-Handed Woman 13
Foul Play 13
The End 12
California Suite 12
In Praise of Older Women 12
Jaws 2 12
Piranha 12
The correct answer is given as:
The Bad News Bears Go to Japan 50
The Swarm 37
Grease 28
American Hot Wax 27
The Boys from Brazil 26
Heaven Can Wait 25
Big Wednesday 21
A Wedding 19
A Night Full of Rain 19
Orchestra Rehearsal 19
The Cheap Detective 19
Go Tell the Spartans 18
Superman 17
The Star Wars Holiday Special 17
Death on the Nile 17
The Cat from Outer Space 17
Movie Movie 17
The Driver 17
Blue Collar 16
Ice Castles 16
J.R.R. Tolkien's The Lord of the 16
International Velvet 16
Coming Home 15
The Brink's Job 15
Gray Lady Down 15
Bye Bye Monkey 15
Without Anesthesia 15
Violette Nozière 15
The Water Babies 15
Revenge of the Pink Panther 15
Who'll Stop The Rain 15
Alexandria... Why? 15
Occupation in 26 Pictures 15
David 15
The Chant of Jimmie Blacksmith 15
The Empire of Passion 14
Damien: Omen II 14
Closed Circuit 14
Bread and Chocolate 14
I Wanna Hold Your Hand 14
An Unmarried Woman 13
Almost Summer 13
Goin' South 13
Foul Play 13
The Left-Handed Woman 13
Jaws 2 12
California Suite 12
In Praise of Older Women 12
Force 10 From Navarone 12
Midnight Express 12
There is no problem on SQL ZOO but you just need to add title to ORDER By clause
because the requirement is to order count then title. Below is the modified version of your sql:
SELECT m.title, Count(c.actorid)
FROM casting c JOIN movie m ON
m.id = c.movieid
WHERE m.yr = 1978
GROUP BY m.title
ORDER BY Count(c.actorid) DESC, title
I do think SQLZoo is coding their answer a little bit differently than OP causing ties to be non-ordered.. as far as I can tell. I was stuck on this problem as well with an answer similar to OP.
I did try different combinations of GROUP BY and JOIN (per comments) before I resorted to custom ordering.. maybe I missed the correct combination..
So, in order to get SQLZoo's answer (the "smiley face"), I had to use CASE title WHEN to put in a custom order for ties:
SELECT title, COUNT(actorid)
FROM movie JOIN casting
ON (movieid=movie.id)
WHERE yr=1978
GROUP BY title
ORDER BY COUNT(actorid) DESC,
CASE title
WHEN 'A Wedding' THEN 1
WHEN 'A Night Full of Rain' THEN 2
WHEN 'Orchestra Rehearsal' THEN 3
WHEN 'The Cheap Detective' THEN 4
WHEN 'The Driver' THEN 1
WHEN 'Movie Movie' THEN 2
WHEN 'Superman' THEN 3
WHEN 'The Star Wars Holiday Special' THEN 4
WHEN 'Death on the Nile' THEN 5
WHEN 'The Cat from Outer Space' THEN 6
WHEN 'Blue Collar' THEN 1
WHEN 'Ice Castles' THEN 2
WHEN "J.R.R Tolkien's The Lord of the Rings" THEN 3
WHEN 'International Velvet' THEN 4
WHEN 'Alexandria... Why?' THEN 1
WHEN 'Occupation in 26 Pictures' THEN 2
WHEN 'The Chant of Jimmie Blacksmith' THEN 3
WHEN 'David' THEN 4
WHEN "The Brink's Job" THEN 5
WHEN 'Coming Home' THEN 6
WHEN 'Gray Lady Down' THEN 7
WHEN 'Bye Bye Monkey' THEN 8
WHEN 'Without Anesthesia' THEN 9
WHEN 'Violette Nozière' THEN 10
WHEN 'The Water Babies' THEN 11
WHEN 'Revenge of the Pink Panther' THEN 12
WHEN "Who'll Stop The Rain" THEN 13
WHEN 'The Empire of Passion' THEN 1
WHEN 'Damien: Omen II' THEN 2
WHEN 'Closed Circuit' THEN 3
WHEN 'Bread and Chocolate' THEN 4
WHEN 'I Wanna Hold Your Hand' THEN 5
WHEN 'Foul Play' THEN 1
WHEN 'The Left-Handed Woman' THEN 2
WHEN 'An Unmarried Woman' THEN 3
WHEN 'Almost Summer' THEN 4
WHEN "Goin' South" THEN 5
WHEN 'Piranha' THEN 1
WHEN 'Jaws 2' THEN 2
WHEN 'California Suite' THEN 3
WHEN 'In Praise of Older Woman' THEN 4
WHEN 'Force 10 From Navarone' THEN 5
WHEN 'Midnight Express' THEN 6
WHEN 'The End' THEN 7
END ASC
It is super cumbersome.. you should understand OP's answer before you copy/paste the CASE title WHEN to get SQLZoo's "smiley face".
A helpful SORTresource: tutorialspoint.com -- scroll down to:
To fetch the rows with own preferred order, the SELECT query would as follows:
I have had the same problem with this exercise about ordering the answer so, I go trying each column rs. This works for the site:
ORDER BY count(c.actorid) DESC, budget DESC
It seems that sqlzoo.net is being really cerebral about using an alias for the count of actors on the casting crew. Here is the query I've finally arrived at, and it's worked. I've used actors as alias, but it will accept the answer as correct with cast or any other alias for the count.
SELECT title, count(actorid) AS actors
FROM movie
JOIN casting ON movie.id = movieid
WHERE yr = 1978
GROUP BY title
ORDER BY actors DESC
Please note that sqlzoo.net may or may not accept the correct answer right away. Try it a few times; you might even need to navigate away and then back to that page to get it working and see the smiley face. The page behaves weirdly to say the least, but I hope it will help others looking for the correct answer. :)
SELECT movie.title, COUNT(*) AS actors
FROM movie
JOIN casting
ON movie.id = casting.movieid
WHERE movie.yr = 1978
GROUP BY title
ORDER BY actors DESC, title