DUPLICATE ENTRIES IN T-SQL











up vote
0
down vote

favorite












I have a serious problem here. I got this result from my query.



Please follow this link for the screenshot- I can't upload an image yet:
https://i.stack.imgur.com/MgLJR.png



I tried the code shown here to get rid of the duplicates entries but I get an error




Column 'clients.client_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause




My code:



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT
client_name, articles.article_id, article_name,
article_price, entry_number,
entry_number * article_price AS montant,
@BcNumber AS bc_number
FROM
articles
JOIN
entries ON articles.article_id = entries.article_id
JOIN
commandes ON commandes.bc_number = entries.bc_number
JOIN
clients ON clients.client_id = commandes.client_id
WHERE
commandes.bc_number = @BcNumber
GROUP BY
articles.article_id
END


Does someone have a solution?










share|improve this question
























  • You don't have any aggregate function like SUM or COUNT in your SELECT list - so why are you trying to use a GROUP BY then??? Doesn't make any sense .....
    – marc_s
    Nov 11 at 8:43










  • Are you looking for distinct?
    – Zohar Peled
    Nov 11 at 10:07










  • Yeah, thanks the distinct works.
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:21










  • Tip: Reasons not to use images are here.
    – HABO
    Nov 11 at 18:09















up vote
0
down vote

favorite












I have a serious problem here. I got this result from my query.



Please follow this link for the screenshot- I can't upload an image yet:
https://i.stack.imgur.com/MgLJR.png



I tried the code shown here to get rid of the duplicates entries but I get an error




Column 'clients.client_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause




My code:



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT
client_name, articles.article_id, article_name,
article_price, entry_number,
entry_number * article_price AS montant,
@BcNumber AS bc_number
FROM
articles
JOIN
entries ON articles.article_id = entries.article_id
JOIN
commandes ON commandes.bc_number = entries.bc_number
JOIN
clients ON clients.client_id = commandes.client_id
WHERE
commandes.bc_number = @BcNumber
GROUP BY
articles.article_id
END


Does someone have a solution?










share|improve this question
























  • You don't have any aggregate function like SUM or COUNT in your SELECT list - so why are you trying to use a GROUP BY then??? Doesn't make any sense .....
    – marc_s
    Nov 11 at 8:43










  • Are you looking for distinct?
    – Zohar Peled
    Nov 11 at 10:07










  • Yeah, thanks the distinct works.
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:21










  • Tip: Reasons not to use images are here.
    – HABO
    Nov 11 at 18:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a serious problem here. I got this result from my query.



Please follow this link for the screenshot- I can't upload an image yet:
https://i.stack.imgur.com/MgLJR.png



I tried the code shown here to get rid of the duplicates entries but I get an error




Column 'clients.client_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause




My code:



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT
client_name, articles.article_id, article_name,
article_price, entry_number,
entry_number * article_price AS montant,
@BcNumber AS bc_number
FROM
articles
JOIN
entries ON articles.article_id = entries.article_id
JOIN
commandes ON commandes.bc_number = entries.bc_number
JOIN
clients ON clients.client_id = commandes.client_id
WHERE
commandes.bc_number = @BcNumber
GROUP BY
articles.article_id
END


Does someone have a solution?










share|improve this question















I have a serious problem here. I got this result from my query.



Please follow this link for the screenshot- I can't upload an image yet:
https://i.stack.imgur.com/MgLJR.png



I tried the code shown here to get rid of the duplicates entries but I get an error




Column 'clients.client_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause




My code:



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT
client_name, articles.article_id, article_name,
article_price, entry_number,
entry_number * article_price AS montant,
@BcNumber AS bc_number
FROM
articles
JOIN
entries ON articles.article_id = entries.article_id
JOIN
commandes ON commandes.bc_number = entries.bc_number
JOIN
clients ON clients.client_id = commandes.client_id
WHERE
commandes.bc_number = @BcNumber
GROUP BY
articles.article_id
END


Does someone have a solution?







sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 8:41









marc_s

565k12610921245




565k12610921245










asked Nov 10 at 22:49









Armand Mamitiana Rakotoarisoa

53




53












  • You don't have any aggregate function like SUM or COUNT in your SELECT list - so why are you trying to use a GROUP BY then??? Doesn't make any sense .....
    – marc_s
    Nov 11 at 8:43










  • Are you looking for distinct?
    – Zohar Peled
    Nov 11 at 10:07










  • Yeah, thanks the distinct works.
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:21










  • Tip: Reasons not to use images are here.
    – HABO
    Nov 11 at 18:09


















  • You don't have any aggregate function like SUM or COUNT in your SELECT list - so why are you trying to use a GROUP BY then??? Doesn't make any sense .....
    – marc_s
    Nov 11 at 8:43










  • Are you looking for distinct?
    – Zohar Peled
    Nov 11 at 10:07










  • Yeah, thanks the distinct works.
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:21










  • Tip: Reasons not to use images are here.
    – HABO
    Nov 11 at 18:09
















You don't have any aggregate function like SUM or COUNT in your SELECT list - so why are you trying to use a GROUP BY then??? Doesn't make any sense .....
– marc_s
Nov 11 at 8:43




You don't have any aggregate function like SUM or COUNT in your SELECT list - so why are you trying to use a GROUP BY then??? Doesn't make any sense .....
– marc_s
Nov 11 at 8:43












Are you looking for distinct?
– Zohar Peled
Nov 11 at 10:07




Are you looking for distinct?
– Zohar Peled
Nov 11 at 10:07












Yeah, thanks the distinct works.
– Armand Mamitiana Rakotoarisoa
Nov 11 at 11:21




Yeah, thanks the distinct works.
– Armand Mamitiana Rakotoarisoa
Nov 11 at 11:21












Tip: Reasons not to use images are here.
– HABO
Nov 11 at 18:09




Tip: Reasons not to use images are here.
– HABO
Nov 11 at 18:09












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You should add all the columns you want to remove the duplicate to the GROUP BY.



Try this (Keep in mind the format of your code, this way you improve readability.):



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT client_name
, articles.article_id
, article_name
, article_price
, entry_number
, entry_number*article_price AS montant
, @BcNumber AS bc_number
FROM articles
JOIN entries on articles.article_id = entries.article_id
JOIN commandes ON commandes.bc_number=entries.bc_number
JOIN clients ON clients.client_id=commandes.client_id
WHERE commandes.bc_number = @BcNumber
GROUP BY client_name
, articles.article_id
, article_name
, article_price
, entry_number
END


Note that the calculated column (montant) and bc_number column doesn't go in the GROUP BY, since you already grouping by article_price and entry_number.






share|improve this answer























  • It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:26










  • The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
    – Freddy G
    Nov 11 at 11:46










  • If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
    – Zohar Peled
    Nov 12 at 7:47











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244191%2fduplicate-entries-in-t-sql%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








up vote
0
down vote



accepted










You should add all the columns you want to remove the duplicate to the GROUP BY.



Try this (Keep in mind the format of your code, this way you improve readability.):



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT client_name
, articles.article_id
, article_name
, article_price
, entry_number
, entry_number*article_price AS montant
, @BcNumber AS bc_number
FROM articles
JOIN entries on articles.article_id = entries.article_id
JOIN commandes ON commandes.bc_number=entries.bc_number
JOIN clients ON clients.client_id=commandes.client_id
WHERE commandes.bc_number = @BcNumber
GROUP BY client_name
, articles.article_id
, article_name
, article_price
, entry_number
END


Note that the calculated column (montant) and bc_number column doesn't go in the GROUP BY, since you already grouping by article_price and entry_number.






share|improve this answer























  • It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:26










  • The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
    – Freddy G
    Nov 11 at 11:46










  • If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
    – Zohar Peled
    Nov 12 at 7:47















up vote
0
down vote



accepted










You should add all the columns you want to remove the duplicate to the GROUP BY.



Try this (Keep in mind the format of your code, this way you improve readability.):



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT client_name
, articles.article_id
, article_name
, article_price
, entry_number
, entry_number*article_price AS montant
, @BcNumber AS bc_number
FROM articles
JOIN entries on articles.article_id = entries.article_id
JOIN commandes ON commandes.bc_number=entries.bc_number
JOIN clients ON clients.client_id=commandes.client_id
WHERE commandes.bc_number = @BcNumber
GROUP BY client_name
, articles.article_id
, article_name
, article_price
, entry_number
END


Note that the calculated column (montant) and bc_number column doesn't go in the GROUP BY, since you already grouping by article_price and entry_number.






share|improve this answer























  • It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:26










  • The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
    – Freddy G
    Nov 11 at 11:46










  • If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
    – Zohar Peled
    Nov 12 at 7:47













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You should add all the columns you want to remove the duplicate to the GROUP BY.



Try this (Keep in mind the format of your code, this way you improve readability.):



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT client_name
, articles.article_id
, article_name
, article_price
, entry_number
, entry_number*article_price AS montant
, @BcNumber AS bc_number
FROM articles
JOIN entries on articles.article_id = entries.article_id
JOIN commandes ON commandes.bc_number=entries.bc_number
JOIN clients ON clients.client_id=commandes.client_id
WHERE commandes.bc_number = @BcNumber
GROUP BY client_name
, articles.article_id
, article_name
, article_price
, entry_number
END


Note that the calculated column (montant) and bc_number column doesn't go in the GROUP BY, since you already grouping by article_price and entry_number.






share|improve this answer














You should add all the columns you want to remove the duplicate to the GROUP BY.



Try this (Keep in mind the format of your code, this way you improve readability.):



ALTER PROCEDURE dbo.spGetBcByBcNumber
@BcNumber INT
AS
BEGIN
SELECT client_name
, articles.article_id
, article_name
, article_price
, entry_number
, entry_number*article_price AS montant
, @BcNumber AS bc_number
FROM articles
JOIN entries on articles.article_id = entries.article_id
JOIN commandes ON commandes.bc_number=entries.bc_number
JOIN clients ON clients.client_id=commandes.client_id
WHERE commandes.bc_number = @BcNumber
GROUP BY client_name
, articles.article_id
, article_name
, article_price
, entry_number
END


Note that the calculated column (montant) and bc_number column doesn't go in the GROUP BY, since you already grouping by article_price and entry_number.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 23:05

























answered Nov 10 at 22:54









Freddy G

25119




25119












  • It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:26










  • The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
    – Freddy G
    Nov 11 at 11:46










  • If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
    – Zohar Peled
    Nov 12 at 7:47


















  • It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
    – Armand Mamitiana Rakotoarisoa
    Nov 11 at 11:26










  • The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
    – Freddy G
    Nov 11 at 11:46










  • If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
    – Zohar Peled
    Nov 12 at 7:47
















It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
– Armand Mamitiana Rakotoarisoa
Nov 11 at 11:26




It works both with your code and with adding distinct articles.article_id and remove the Group by. So what's the best practice between em??
– Armand Mamitiana Rakotoarisoa
Nov 11 at 11:26












The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
– Freddy G
Nov 11 at 11:46




The GROUP BY allows you to use aggregate functions (SUM, COUNT, MAX, etc.) in your SELECT. If you're not going to use any aggregate functions, both are the same.
– Freddy G
Nov 11 at 11:46












If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
– Zohar Peled
Nov 12 at 7:47




If you are not using aggregation, using distinct is better than group by because it conveys the intent of the author better meaning it's more readable and maintainable. The execution plan of both queries will very likely be the exactly the same, but if I see a query using group by with no aggregating functions I immediately suspect that this is either a mistake or a query that once had an aggregating function and it was removed, or that the query is incomplete.
– Zohar Peled
Nov 12 at 7:47


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244191%2fduplicate-entries-in-t-sql%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine