Select unique values of 3 columns from a sql table which has 7 columns
This is what my table looks like.
I want to get unique 'sets' of origin, destination, and flight_date.
So, for the table shown, my query should return {['BLR','DEL', '2019-01-10'], ['BLR','DEL', '2019-01-11']}
javascript mysql node.js
add a comment |
This is what my table looks like.
I want to get unique 'sets' of origin, destination, and flight_date.
So, for the table shown, my query should return {['BLR','DEL', '2019-01-10'], ['BLR','DEL', '2019-01-11']}
javascript mysql node.js
add a comment |
This is what my table looks like.
I want to get unique 'sets' of origin, destination, and flight_date.
So, for the table shown, my query should return {['BLR','DEL', '2019-01-10'], ['BLR','DEL', '2019-01-11']}
javascript mysql node.js
This is what my table looks like.
I want to get unique 'sets' of origin, destination, and flight_date.
So, for the table shown, my query should return {['BLR','DEL', '2019-01-10'], ['BLR','DEL', '2019-01-11']}
javascript mysql node.js
javascript mysql node.js
edited 4 hours ago
Khadar111
asked Jan 8 at 12:55
Khadar111Khadar111
146
146
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In MySQL this should do the trick:
SELECT DISTINCT origin, destination, flight_date FROM flights
Alternatively:
SELECT origin, destination, flight_date
FROM flights
GROUP BY origin, destination, flight_date
DISTINCT basically means "Make sure the rows are unique", wheras the advantage of using GROUP BY is, that you can use aggregate functions with it (summing, average etc). Also you can add additional conditions using HAVING.
Example:
SELECT origin, destination, flight_date, SUM(time_bucket) AS time_bucket_total
FROM flights
GROUP BY origin, destination, flight_date
HAVING SUM(time_bucket) > 3
This will return the summed time_bucket values along each row, and will also return only those entries where that sum is greater than 3.
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
add a comment |
Add group by the columns you want to select unique values of
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%2f54092278%2fselect-unique-values-of-3-columns-from-a-sql-table-which-has-7-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In MySQL this should do the trick:
SELECT DISTINCT origin, destination, flight_date FROM flights
Alternatively:
SELECT origin, destination, flight_date
FROM flights
GROUP BY origin, destination, flight_date
DISTINCT basically means "Make sure the rows are unique", wheras the advantage of using GROUP BY is, that you can use aggregate functions with it (summing, average etc). Also you can add additional conditions using HAVING.
Example:
SELECT origin, destination, flight_date, SUM(time_bucket) AS time_bucket_total
FROM flights
GROUP BY origin, destination, flight_date
HAVING SUM(time_bucket) > 3
This will return the summed time_bucket values along each row, and will also return only those entries where that sum is greater than 3.
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
add a comment |
In MySQL this should do the trick:
SELECT DISTINCT origin, destination, flight_date FROM flights
Alternatively:
SELECT origin, destination, flight_date
FROM flights
GROUP BY origin, destination, flight_date
DISTINCT basically means "Make sure the rows are unique", wheras the advantage of using GROUP BY is, that you can use aggregate functions with it (summing, average etc). Also you can add additional conditions using HAVING.
Example:
SELECT origin, destination, flight_date, SUM(time_bucket) AS time_bucket_total
FROM flights
GROUP BY origin, destination, flight_date
HAVING SUM(time_bucket) > 3
This will return the summed time_bucket values along each row, and will also return only those entries where that sum is greater than 3.
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
add a comment |
In MySQL this should do the trick:
SELECT DISTINCT origin, destination, flight_date FROM flights
Alternatively:
SELECT origin, destination, flight_date
FROM flights
GROUP BY origin, destination, flight_date
DISTINCT basically means "Make sure the rows are unique", wheras the advantage of using GROUP BY is, that you can use aggregate functions with it (summing, average etc). Also you can add additional conditions using HAVING.
Example:
SELECT origin, destination, flight_date, SUM(time_bucket) AS time_bucket_total
FROM flights
GROUP BY origin, destination, flight_date
HAVING SUM(time_bucket) > 3
This will return the summed time_bucket values along each row, and will also return only those entries where that sum is greater than 3.
In MySQL this should do the trick:
SELECT DISTINCT origin, destination, flight_date FROM flights
Alternatively:
SELECT origin, destination, flight_date
FROM flights
GROUP BY origin, destination, flight_date
DISTINCT basically means "Make sure the rows are unique", wheras the advantage of using GROUP BY is, that you can use aggregate functions with it (summing, average etc). Also you can add additional conditions using HAVING.
Example:
SELECT origin, destination, flight_date, SUM(time_bucket) AS time_bucket_total
FROM flights
GROUP BY origin, destination, flight_date
HAVING SUM(time_bucket) > 3
This will return the summed time_bucket values along each row, and will also return only those entries where that sum is greater than 3.
edited yesterday
answered Jan 8 at 12:59
Markus KleinMarkus Klein
112
112
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
add a comment |
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
It works! is either of the above more efficient than the other?
– Khadar111
Jan 8 at 13:07
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@Khadar111 the performance difference is trivial between the two. I like the top one personally, but the second one potentially provides more flexiblity
– NappingRabbit
Jan 8 at 13:13
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@NappingRabbit Can you elaborate on how the second one provides more flexibility?
– Khadar111
Jan 8 at 13:26
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
@Khadar111, I've edited and elaborated the post.
– Markus Klein
yesterday
add a comment |
Add group by the columns you want to select unique values of
add a comment |
Add group by the columns you want to select unique values of
add a comment |
Add group by the columns you want to select unique values of
Add group by the columns you want to select unique values of
answered Jan 8 at 13:03
BoscoBosco
294
294
add a comment |
add a comment |
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%2f54092278%2fselect-unique-values-of-3-columns-from-a-sql-table-which-has-7-columns%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