My nycgrid table with the following schema id,x1,x2,y1,y2. And it looks like following examples:
22,910000,920000,120000,130000
67,930000,940000,170000,180000
171,980000,990000,210000,220000
Another table, nyccrash, contains tuples with information about the car acciddents that occured from 1989 through 2007 (12 thousand records in total). The first attribute is crash_year, followed by type of accident, weather condition...etc and ending in x_coordinate and y_coordinate where the car crash has occurred in NYC.
2007,2,9,4,1,1028977,202232
2004,1,1,1,4,1012600,214101
2003,1,9,1,1,958775,156149
1999,1,1,1,1,997349,175503
This is an extension question to the one I've asked earlier on stackoverflow.
I am trying to find the square grids (Grid ID) such that they have
less car crashes occurred in 2007 than in 1989. There are about 100
rows in the nycgrid table. I need only those rows that have had less
accidents happened in the year in 2007 than in the year 1989.
And second part, which is probably slightly easier is how could I find
the number of car crashes per each square grid (nycgrid.id)? In other
words, how display how many crashes occurred on each grid id? Each
crash has associated x and y coordinate. Each grid has a x1-x2-y1-y2
coordinates that make up a square.
Starting with RBarryYoung's answer to your last question we just pivot on the year using SUM/CASE and then compare the values
SELECT ID
FROM
(SELECT
grid.ID,
SUM(CASE WHEN yearCol = 1989 THEN 1 ELSE 0 END) CrashCount_1989,
SUM(CASE WHEN yearCol = 2007 THEN 1 ELSE 0 END) CrashCount_2007
FROM crashes
INNER JOIN grid
ON crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
And crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
WHERE crashes.yearCol IN(1989, 2007)
GROUP BY grid.ID) t
WHERE CrashCount_2007 < CrashCount_1989
Related
AS a newcomer I am trying to get my head around MS Access to query
The database is a racing one and in the results I want to find the total times a trainer has a favourite that wins as a percentage.
The trainer has trained 22 favourites and 9 have won.
I have written the SQL to get the 9 won figure but struggling to calculate the percentage of 9/22. What is the calculation to add to the query
[2020].FPos = finishing position & [2020].Favs = Favourite type
Total PerCent ???
===========================
SELECT [2020].FPos, [2020].Trainer, [2020].Favs, [2020].Favs
FROM 2020
WHERE ((([2020].FPos)="1") AND (([2020].Trainer) Like "*Eve *")
AND (([2020].Favs)="Fav"));
Answering my own question as it might be useful for others
SELECT 2020.Trainer, Count(2020.Favs) AS [Count of Fav with Win]
FROM 2020
GROUP BY 2020.Trainer, 2020.Favs, 2020.FPos
HAVING (((2020.Favs)="Fav") AND ((2020.FPos)="1"));
I'm trying to GROUP BY one categorical variable but also show another corresponding variable in my output in SQL. Here is what the table looks like:Original Data Table
There are three main variables I'm working with here: Game, Platform and Week. Week variable states the position of the game under the global charts, so 1 would mean its the number 1 game. I want to find the game with the most weeks inside the top 10 BY platform, so I'm trying to get my table to look like this:
Platform | Game | Most_weeks_top10
Right now, I tried the following steps:
SELECT platform, game, COUNT(*) AS total
FROM global_weekly_charts_2013_2014
WHERE week <= 10
GROUP BY game, platform;
Which returns this:
Table Grouped By Platform AND Game
However, I only want the game, platform and total weeks of the game with max number of weeks in top 10. I tried
SELECT game, platform, Max(total) OVER (PARTITION BY platform)
from the derived table but did not get the desired output. I feel like the solution is right there and not that difficult but I can't seem to get the answer.
please add “ORDER BY total DESC LIMIT 10”.try again
I am two days into MySQL. While working on a database of movies, I decided to try and answer a specific question: what movies from a list of 1000 made a negative profit, but had an IMDB score between 8 and 9?
Ideally, I wanted to return only a list of movies that meet that criteria, but at first, I had not known the WHERE statement. So I used CASE, so that I could simply mark those movies with 'Quality Anomaly'.
Then, when I learned about WHERE, I implemented it. However, the results were different. The query using CASE only marked 6 movies, and the query using WHERE returned 51 movies (rows). I added the appropriate columns so I could check to make sure it was right, and it all checked out, but there is something weird. When I do a query of SELECT *, to return all movies, there are many missing from the complete list of 1000 that are in the list of 51 from the query using WHERE.
Here is the code:
/**
This query is supposed to return all movie titles while indicating
the profit from the movie, or 'Quality Anomaly' if profit is
negative while imdb score is between 8 and 9. It only indicates
'Quality Anomaly' for 6 movies.
**/
select
movie_title,
imdb_score,
case
when (gross < budget) and (imdb_score between 8 and 9) then 'Quality Anomaly'
else gross - budget
end as profit
from movies;
/**
The next query is supposed to only list the movies that meet
the 'Quality Anomaly' condition described above, but instead
of returning only 6 movies, it returns 51.
**/
select
movie_title,
(gross - budget) as profit,
imdb_score
from movies
where (gross < budget) and (imdb_score between 8 and 9);
Link to output data via github.
I'm doing a left join on a table to get the number of leads we've generated today and how many times we've called those leads. I figured a left join would be the best thing to do, so I wrote the following query:
SELECT
COUNT(rad.phone_number) as lead_number, rals.lead_source_name as source, COUNT(racl.phone_number) as calls, SUM(case when racl.contacted = 1 then 1 else 0 end) as contacted
FROM reporting_app_data rad
LEFT JOIN reporting_app_call_logs racl ON rad.phone_number = racl.phone_number, reporting_app_lead_sources rals
WHERE DATE(rad.created_at) = CURDATE() AND rals.list_id = rad.lead_source
GROUP BY rad.lead_source;
But the problem with that, is that if in the reporting_app_call_logs table, there are multiple entries for the same phone number (so a phone number has been called multiple times that day), the lead_number (which I want to count how many leads were generated on the current day grouped by lead_source) equals how many calls there are. So the count from the LEFT table equals the count from the RIGHT table.
How do I write a SQL query that gets the number of leads and the total number of calls per lead source?
Try COUNT(DISTINCT expression)
In other words, change COUNT(rad.phone_number) to COUNT(DISTINCT rad.phone_number)
I'm fairly new to php / mysql programming and I'm having a hard time figuring out the logic for a relational database that I'm trying to build. Here's the problem:
I have different leaders who will be in charge of a store anytime between 9am and 9pm.
A customer who has visited the store can rate their experience on a scale of 1 to 5.
I'm building a site that will allow me to store the shifts that a leader worked as seen below.
When I hit submit, the site would take the data leaderName:"George", shiftTimeArray: 11am, 1pm, 6pm (from the example in the picture) and the shiftDate and send them to an SQL database.
Later, I want to be able to get the average score for a person by sending a query to mysql, retrieving all of the scores that that leader received and averaging them together. I know the code to build the forms and to perform the search. However, I'm having a hard time coming up with the logic for the tables that will relate the data. Currently, I have a mysql table called responses that contains the following fields,
leader_id
shift_date // contains the date that the leader worked
shift_time // contains the time that the leader worked
visit_date // contains the date that the survey/score was given
visit_time // contains the time that the survey/score was given
score // contains the actual score of the survey (1-5)
I enter the shifts that the leader works at the beginning of the week and then enter the survey scores in as they come in during the week.
So Here's the Question: What mysql tables and fields should I create to relate this data so that I can query a leader's name and get the average score from all of their surveys?
You want tables like:
Leader (leader_id, name, etc)
Shift (leader_id, shift_date, shift_time)
SurveyResult (visit_date, visit_time, score)
Note: omitted the surrogate primary keys for Shift and SurveyResult that I would probably include.
To query you join shifts and surveys group on leader and taking the average then jon that back to leader for a name.
The query might be something like (but I haven;t actually built it in MySQL to verify syntax)
SELECT name
,AverageScore
FROM Leader a
INNER JOIN (
SELECT leader_id
, AVG(score) AverageScore
FROM Shift
INNER JOIN
SurveyResult ON shift_date = visit_date
AND shift_time = visit_time --depends on how you are recording time what this really needs to be
GROUP BY leader ID
) b ON a.leader_id = b.leader_id
I would do the following structure:
leaders
id
name
leaders_timetabke (can be multiple per leader)
id,
leader_id
shift_datetime (I assume it stores date and hour here, minutes and seconds are always 0
survey_scores
id,
visit_datetime
score
SELECT l.id, l.name, AVG(s.score) FROM leaders l
INNER JOIN leaders_timetable lt ON lt.leader_id = l.id
INNER JOIN survey_scores s ON lt.shift_datetime=DATE_FORMAT('Y-m-d H:00:00', s.visit_datetime)
GROUP BY l.id
DATE_FORMAT here helps to cut hours and minutes from visit_datetime so that it could be matched against shift_datetime. This is MYSQL function, so if you use something else you'll need to use different function
Say you have a 'leader' who has 5 survey rows with scores 1, 2, 3, 4 and 5.
if you select all surveys from this leader, sum the survey scores and divide them by 5 (the total amount of surveys that this leader has). You will have the average, in this case 3.
(1 + 2 + 3 + 4 + 5) / 5 = 3
You wouldn't need to create any more tables or fields, you have what you need.