Shorten a queryInserting multiple rows in a single SQL query?How to use count and group by at the same select statementHow to query MongoDB with “like”?Trying to retrieve information from table and compile in top 10 ListSQL count by groupSQL Select Single most from a count - ALL opereatorSimplifying MYSQL queryHow to print out the number of records given a conditionHow to use group by in this scenario with case expression?Query to output max movie tickets for each genre
Generate points for smooth movement between two given points
Is there any reason to change the ISO manually?
How to describe hit point damage without talking about wounds
Are kids with daycare background antisocial?
Which is the best password hashing algorithm in .NET Core?
Has Rey's new lightsaber been seen before in canon or legends?
std::tuple sizeof, is it a missed optimization?
Do 643,000 Americans go bankrupt every year due to medical bills?
Are there photos of the Apollo LM showing disturbed lunar soil resulting from descent engine exhaust?
Adding transparency to ink drawing
Why did the VIC-II and SID use 6 µm technology in the era of 3 µm and 1.5 µm?
To which airspace does the border of two adjacent airspaces belong to?
Go for an isolated pawn
What happens if I double Meddling Mage's 'enter the battlefield' trigger?
Do index funds really have double-digit percents annual return rates?
IEEE Registration Authority mac prefix
Question about derivation of kinematics equations
How many days for hunting?
MOSFET broke after attaching capacitor bank
What are the main differences in the druid class between 5th edition and 3.5 edition?
How to find better food in airports
What is hot spotting in the context of adding files to tempdb?
a harmful idea/plan
Are language and thought the same?
Shorten a query
Inserting multiple rows in a single SQL query?How to use count and group by at the same select statementHow to query MongoDB with “like”?Trying to retrieve information from table and compile in top 10 ListSQL count by groupSQL Select Single most from a count - ALL opereatorSimplifying MYSQL queryHow to print out the number of records given a conditionHow to use group by in this scenario with case expression?Query to output max movie tickets for each genre
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have to write a query that would calculate number of tickets purchased consisting only of movie genre of that type. At the end, I have to return movie genre and number of tickets bought for that genre. I have written a query but I was wondering if it can be made shorter and more compact?
Following is the database scheme:
movies(movieId, movieGenre, moviePrice)
tickets(ticketId, ticketDate, customerId)
details(ticketId, movieId, numOfTickets)
Here is my query:
select m.genre, count(*)
from(select t.ticketId, m.genre
from(select d.ticketId
from(select m.genre, t.ticketId
from tickets t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by m.genre, t.ticketId) d
group by d.ticketId
having count(*) = 1) as t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by t.ticketId, m.genre) m
group by m.genre;
This runs on a database so I am only able to post sample output:
comedy 29821
action 27857
rom-com 19663
sql postgresql
|
show 1 more comment
I have to write a query that would calculate number of tickets purchased consisting only of movie genre of that type. At the end, I have to return movie genre and number of tickets bought for that genre. I have written a query but I was wondering if it can be made shorter and more compact?
Following is the database scheme:
movies(movieId, movieGenre, moviePrice)
tickets(ticketId, ticketDate, customerId)
details(ticketId, movieId, numOfTickets)
Here is my query:
select m.genre, count(*)
from(select t.ticketId, m.genre
from(select d.ticketId
from(select m.genre, t.ticketId
from tickets t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by m.genre, t.ticketId) d
group by d.ticketId
having count(*) = 1) as t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by t.ticketId, m.genre) m
group by m.genre;
This runs on a database so I am only able to post sample output:
comedy 29821
action 27857
rom-com 19663
sql postgresql
Isn't this just supposed to be aSUM(numOfTickets)
with aGROUP BY movieGenre
over a double join? Don't see the need forCOUNT
and subqueries.
– Robby Cornelissen
Mar 28 at 3:25
I haven't tried doing that. Would you mind showing what it would look like as a query? Specifically what you mean by double join in this case? @RobbyCornelissen
– Jap Fios
Mar 28 at 3:36
From the top of my head:SELECT m.movieGenre, SUM(d.numOfTickets) FROM details d LEFT JOIN tickets t ON d.ticketId = t.ticketId LEFT JOIN movies m ON d.movieId = m.movieId GROUP BY m.movieGenre
Haven't done SQL in a while so your mileage may vary...
– Robby Cornelissen
Mar 28 at 3:43
I tried it but I get a table with millions of entries for each genre so I'm assuming its taking in duplicates as well. Any way to get around that using the query you mentioned?
– Jap Fios
Mar 28 at 3:53
Could you try anINNER JOIN
instead of aLEFT JOIN
? After that, I'm all out of ideas...
– Robby Cornelissen
Mar 28 at 4:00
|
show 1 more comment
I have to write a query that would calculate number of tickets purchased consisting only of movie genre of that type. At the end, I have to return movie genre and number of tickets bought for that genre. I have written a query but I was wondering if it can be made shorter and more compact?
Following is the database scheme:
movies(movieId, movieGenre, moviePrice)
tickets(ticketId, ticketDate, customerId)
details(ticketId, movieId, numOfTickets)
Here is my query:
select m.genre, count(*)
from(select t.ticketId, m.genre
from(select d.ticketId
from(select m.genre, t.ticketId
from tickets t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by m.genre, t.ticketId) d
group by d.ticketId
having count(*) = 1) as t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by t.ticketId, m.genre) m
group by m.genre;
This runs on a database so I am only able to post sample output:
comedy 29821
action 27857
rom-com 19663
sql postgresql
I have to write a query that would calculate number of tickets purchased consisting only of movie genre of that type. At the end, I have to return movie genre and number of tickets bought for that genre. I have written a query but I was wondering if it can be made shorter and more compact?
Following is the database scheme:
movies(movieId, movieGenre, moviePrice)
tickets(ticketId, ticketDate, customerId)
details(ticketId, movieId, numOfTickets)
Here is my query:
select m.genre, count(*)
from(select t.ticketId, m.genre
from(select d.ticketId
from(select m.genre, t.ticketId
from tickets t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by m.genre, t.ticketId) d
group by d.ticketId
having count(*) = 1) as t join details d on t.ticketId =
d.ticketId join movies m on d.movieId = m.movieId
group by t.ticketId, m.genre) m
group by m.genre;
This runs on a database so I am only able to post sample output:
comedy 29821
action 27857
rom-com 19663
sql postgresql
sql postgresql
asked Mar 28 at 3:19
Jap FiosJap Fios
62 bronze badges
62 bronze badges
Isn't this just supposed to be aSUM(numOfTickets)
with aGROUP BY movieGenre
over a double join? Don't see the need forCOUNT
and subqueries.
– Robby Cornelissen
Mar 28 at 3:25
I haven't tried doing that. Would you mind showing what it would look like as a query? Specifically what you mean by double join in this case? @RobbyCornelissen
– Jap Fios
Mar 28 at 3:36
From the top of my head:SELECT m.movieGenre, SUM(d.numOfTickets) FROM details d LEFT JOIN tickets t ON d.ticketId = t.ticketId LEFT JOIN movies m ON d.movieId = m.movieId GROUP BY m.movieGenre
Haven't done SQL in a while so your mileage may vary...
– Robby Cornelissen
Mar 28 at 3:43
I tried it but I get a table with millions of entries for each genre so I'm assuming its taking in duplicates as well. Any way to get around that using the query you mentioned?
– Jap Fios
Mar 28 at 3:53
Could you try anINNER JOIN
instead of aLEFT JOIN
? After that, I'm all out of ideas...
– Robby Cornelissen
Mar 28 at 4:00
|
show 1 more comment
Isn't this just supposed to be aSUM(numOfTickets)
with aGROUP BY movieGenre
over a double join? Don't see the need forCOUNT
and subqueries.
– Robby Cornelissen
Mar 28 at 3:25
I haven't tried doing that. Would you mind showing what it would look like as a query? Specifically what you mean by double join in this case? @RobbyCornelissen
– Jap Fios
Mar 28 at 3:36
From the top of my head:SELECT m.movieGenre, SUM(d.numOfTickets) FROM details d LEFT JOIN tickets t ON d.ticketId = t.ticketId LEFT JOIN movies m ON d.movieId = m.movieId GROUP BY m.movieGenre
Haven't done SQL in a while so your mileage may vary...
– Robby Cornelissen
Mar 28 at 3:43
I tried it but I get a table with millions of entries for each genre so I'm assuming its taking in duplicates as well. Any way to get around that using the query you mentioned?
– Jap Fios
Mar 28 at 3:53
Could you try anINNER JOIN
instead of aLEFT JOIN
? After that, I'm all out of ideas...
– Robby Cornelissen
Mar 28 at 4:00
Isn't this just supposed to be a
SUM(numOfTickets)
with a GROUP BY movieGenre
over a double join? Don't see the need for COUNT
and subqueries.– Robby Cornelissen
Mar 28 at 3:25
Isn't this just supposed to be a
SUM(numOfTickets)
with a GROUP BY movieGenre
over a double join? Don't see the need for COUNT
and subqueries.– Robby Cornelissen
Mar 28 at 3:25
I haven't tried doing that. Would you mind showing what it would look like as a query? Specifically what you mean by double join in this case? @RobbyCornelissen
– Jap Fios
Mar 28 at 3:36
I haven't tried doing that. Would you mind showing what it would look like as a query? Specifically what you mean by double join in this case? @RobbyCornelissen
– Jap Fios
Mar 28 at 3:36
From the top of my head:
SELECT m.movieGenre, SUM(d.numOfTickets) FROM details d LEFT JOIN tickets t ON d.ticketId = t.ticketId LEFT JOIN movies m ON d.movieId = m.movieId GROUP BY m.movieGenre
Haven't done SQL in a while so your mileage may vary...– Robby Cornelissen
Mar 28 at 3:43
From the top of my head:
SELECT m.movieGenre, SUM(d.numOfTickets) FROM details d LEFT JOIN tickets t ON d.ticketId = t.ticketId LEFT JOIN movies m ON d.movieId = m.movieId GROUP BY m.movieGenre
Haven't done SQL in a while so your mileage may vary...– Robby Cornelissen
Mar 28 at 3:43
I tried it but I get a table with millions of entries for each genre so I'm assuming its taking in duplicates as well. Any way to get around that using the query you mentioned?
– Jap Fios
Mar 28 at 3:53
I tried it but I get a table with millions of entries for each genre so I'm assuming its taking in duplicates as well. Any way to get around that using the query you mentioned?
– Jap Fios
Mar 28 at 3:53
Could you try an
INNER JOIN
instead of a LEFT JOIN
? After that, I'm all out of ideas...– Robby Cornelissen
Mar 28 at 4:00
Could you try an
INNER JOIN
instead of a LEFT JOIN
? After that, I'm all out of ideas...– Robby Cornelissen
Mar 28 at 4:00
|
show 1 more comment
1 Answer
1
active
oldest
votes
I see no reason to use the table tickets, because the results do not filter or aggregate by ticketDate or customerID. Thus, a shorter sql is
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM details d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
HAVING SumNum > 0
ORDER BY m.moviegenre
added 3/28 am
I am not sure what is meant by Duplicates?? In table = details(ticketId, movieId, numOfTickets) ??
I would expect that ticketId is unique, so what would explain duplicates?
Is the same ticketId being printed twice, repeatedly??
Determine what number of ticketId are duplicates--
SELECT ticketId, count(*) as cnt
FROM details d
GROUP By ticketId
HAVING count(*) > 1
Determine what number of "details" rows are duplicates--
SELECT ticketId, movieId, numOfTickets, count(*) as cnt
FROM details d
GROUP By ticketId, movieId, numOfTickets
HAVING count(*) > 1
Then again, it may be that table = movies(movieId, movieGenre, moviePrice) is the one with duplicates??
Determine what number of movieId are duplicates--
SELECT movieId, count(*) as cnt
FROM movies m
GROUP BY movieId
HAVING count(*) > 1
Remove duplicates from details--
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM
(Select Distinct * From details) d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
ORDER BY m.moviegenre
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389654%2fshorten-a-query%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I see no reason to use the table tickets, because the results do not filter or aggregate by ticketDate or customerID. Thus, a shorter sql is
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM details d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
HAVING SumNum > 0
ORDER BY m.moviegenre
added 3/28 am
I am not sure what is meant by Duplicates?? In table = details(ticketId, movieId, numOfTickets) ??
I would expect that ticketId is unique, so what would explain duplicates?
Is the same ticketId being printed twice, repeatedly??
Determine what number of ticketId are duplicates--
SELECT ticketId, count(*) as cnt
FROM details d
GROUP By ticketId
HAVING count(*) > 1
Determine what number of "details" rows are duplicates--
SELECT ticketId, movieId, numOfTickets, count(*) as cnt
FROM details d
GROUP By ticketId, movieId, numOfTickets
HAVING count(*) > 1
Then again, it may be that table = movies(movieId, movieGenre, moviePrice) is the one with duplicates??
Determine what number of movieId are duplicates--
SELECT movieId, count(*) as cnt
FROM movies m
GROUP BY movieId
HAVING count(*) > 1
Remove duplicates from details--
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM
(Select Distinct * From details) d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
ORDER BY m.moviegenre
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
add a comment |
I see no reason to use the table tickets, because the results do not filter or aggregate by ticketDate or customerID. Thus, a shorter sql is
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM details d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
HAVING SumNum > 0
ORDER BY m.moviegenre
added 3/28 am
I am not sure what is meant by Duplicates?? In table = details(ticketId, movieId, numOfTickets) ??
I would expect that ticketId is unique, so what would explain duplicates?
Is the same ticketId being printed twice, repeatedly??
Determine what number of ticketId are duplicates--
SELECT ticketId, count(*) as cnt
FROM details d
GROUP By ticketId
HAVING count(*) > 1
Determine what number of "details" rows are duplicates--
SELECT ticketId, movieId, numOfTickets, count(*) as cnt
FROM details d
GROUP By ticketId, movieId, numOfTickets
HAVING count(*) > 1
Then again, it may be that table = movies(movieId, movieGenre, moviePrice) is the one with duplicates??
Determine what number of movieId are duplicates--
SELECT movieId, count(*) as cnt
FROM movies m
GROUP BY movieId
HAVING count(*) > 1
Remove duplicates from details--
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM
(Select Distinct * From details) d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
ORDER BY m.moviegenre
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
add a comment |
I see no reason to use the table tickets, because the results do not filter or aggregate by ticketDate or customerID. Thus, a shorter sql is
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM details d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
HAVING SumNum > 0
ORDER BY m.moviegenre
added 3/28 am
I am not sure what is meant by Duplicates?? In table = details(ticketId, movieId, numOfTickets) ??
I would expect that ticketId is unique, so what would explain duplicates?
Is the same ticketId being printed twice, repeatedly??
Determine what number of ticketId are duplicates--
SELECT ticketId, count(*) as cnt
FROM details d
GROUP By ticketId
HAVING count(*) > 1
Determine what number of "details" rows are duplicates--
SELECT ticketId, movieId, numOfTickets, count(*) as cnt
FROM details d
GROUP By ticketId, movieId, numOfTickets
HAVING count(*) > 1
Then again, it may be that table = movies(movieId, movieGenre, moviePrice) is the one with duplicates??
Determine what number of movieId are duplicates--
SELECT movieId, count(*) as cnt
FROM movies m
GROUP BY movieId
HAVING count(*) > 1
Remove duplicates from details--
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM
(Select Distinct * From details) d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
ORDER BY m.moviegenre
I see no reason to use the table tickets, because the results do not filter or aggregate by ticketDate or customerID. Thus, a shorter sql is
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM details d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
HAVING SumNum > 0
ORDER BY m.moviegenre
added 3/28 am
I am not sure what is meant by Duplicates?? In table = details(ticketId, movieId, numOfTickets) ??
I would expect that ticketId is unique, so what would explain duplicates?
Is the same ticketId being printed twice, repeatedly??
Determine what number of ticketId are duplicates--
SELECT ticketId, count(*) as cnt
FROM details d
GROUP By ticketId
HAVING count(*) > 1
Determine what number of "details" rows are duplicates--
SELECT ticketId, movieId, numOfTickets, count(*) as cnt
FROM details d
GROUP By ticketId, movieId, numOfTickets
HAVING count(*) > 1
Then again, it may be that table = movies(movieId, movieGenre, moviePrice) is the one with duplicates??
Determine what number of movieId are duplicates--
SELECT movieId, count(*) as cnt
FROM movies m
GROUP BY movieId
HAVING count(*) > 1
Remove duplicates from details--
SELECT m.moviegenre,
Sum(d.numoftickets) as SumNum
FROM
(Select Distinct * From details) d
LEFT JOIN movies m
ON d.movieid = m.movieid
GROUP BY m.moviegenre
ORDER BY m.moviegenre
edited Mar 28 at 17:20
answered Mar 28 at 4:43
donPablodonPablo
1,3821 gold badge8 silver badges15 bronze badges
1,3821 gold badge8 silver badges15 bronze badges
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
add a comment |
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
it gives me the same output as the query mentioned in the comments of the post, so I'm assuming it takes in account duplicates as well. Not sure if I can use distinct in your query
– Jap Fios
Mar 28 at 5:06
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
I think putting a LEFT JOIN then HAVING sumNumOfTickets > 0 is kind of useless. You might want to have result with 0 on top of it... so I would remove the HAVING personally. PS. I slightly edited your query to respect column name and have a more explicit alias, awaiting approval.
– Michael Muryn
Mar 28 at 6:04
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
@JapFios Please share with us some of the output from this query, and explain why resultset is not right. See my additions above. (I have removed the Having per Michael)
– donPablo
Mar 28 at 17:21
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389654%2fshorten-a-query%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Isn't this just supposed to be a
SUM(numOfTickets)
with aGROUP BY movieGenre
over a double join? Don't see the need forCOUNT
and subqueries.– Robby Cornelissen
Mar 28 at 3:25
I haven't tried doing that. Would you mind showing what it would look like as a query? Specifically what you mean by double join in this case? @RobbyCornelissen
– Jap Fios
Mar 28 at 3:36
From the top of my head:
SELECT m.movieGenre, SUM(d.numOfTickets) FROM details d LEFT JOIN tickets t ON d.ticketId = t.ticketId LEFT JOIN movies m ON d.movieId = m.movieId GROUP BY m.movieGenre
Haven't done SQL in a while so your mileage may vary...– Robby Cornelissen
Mar 28 at 3:43
I tried it but I get a table with millions of entries for each genre so I'm assuming its taking in duplicates as well. Any way to get around that using the query you mentioned?
– Jap Fios
Mar 28 at 3:53
Could you try an
INNER JOIN
instead of aLEFT JOIN
? After that, I'm all out of ideas...– Robby Cornelissen
Mar 28 at 4:00