Get row from one table, plus COUNT from a related table
I'm trying to build an SQL query where I grab one table's information (WHERE shops.shop_domain = X
) along with the COUNT of the customers table WHERE customers.product_id = 4242451
.
The shops table DOES NOT have product.id
in it, but the customers table DOES HAVE the shop_domain
in it, hence my attempt to do some sort of join.
I essentially want to return the following:
- shops.id
- shops.name
- shops.shop_domain
COUNT OF CUSTOMERS WHERE customers.product_id = '4242451'
Here is my not so lovely attempt at the query.
I think I have the idea right (maybe...) but I can't wrap my head around building this query.
SELECT shops.id, shops.name, shops.shop_domain, COUNT(customers.customer_id)
FROM shops
LEFT JOIN customers ON shops.shop_domain = customers.shop_domain
WHERE shops.shop_domain = 'myshop.com' AND
customers.product_id = '4242451'
GROUP BY shops.shop_id
Relevant database schemas:
shops:
id, name, shop_domain
customers:
id, name, product_id, shop_domain
sql database postgresql
add a comment |
I'm trying to build an SQL query where I grab one table's information (WHERE shops.shop_domain = X
) along with the COUNT of the customers table WHERE customers.product_id = 4242451
.
The shops table DOES NOT have product.id
in it, but the customers table DOES HAVE the shop_domain
in it, hence my attempt to do some sort of join.
I essentially want to return the following:
- shops.id
- shops.name
- shops.shop_domain
COUNT OF CUSTOMERS WHERE customers.product_id = '4242451'
Here is my not so lovely attempt at the query.
I think I have the idea right (maybe...) but I can't wrap my head around building this query.
SELECT shops.id, shops.name, shops.shop_domain, COUNT(customers.customer_id)
FROM shops
LEFT JOIN customers ON shops.shop_domain = customers.shop_domain
WHERE shops.shop_domain = 'myshop.com' AND
customers.product_id = '4242451'
GROUP BY shops.shop_id
Relevant database schemas:
shops:
id, name, shop_domain
customers:
id, name, product_id, shop_domain
sql database postgresql
add a comment |
I'm trying to build an SQL query where I grab one table's information (WHERE shops.shop_domain = X
) along with the COUNT of the customers table WHERE customers.product_id = 4242451
.
The shops table DOES NOT have product.id
in it, but the customers table DOES HAVE the shop_domain
in it, hence my attempt to do some sort of join.
I essentially want to return the following:
- shops.id
- shops.name
- shops.shop_domain
COUNT OF CUSTOMERS WHERE customers.product_id = '4242451'
Here is my not so lovely attempt at the query.
I think I have the idea right (maybe...) but I can't wrap my head around building this query.
SELECT shops.id, shops.name, shops.shop_domain, COUNT(customers.customer_id)
FROM shops
LEFT JOIN customers ON shops.shop_domain = customers.shop_domain
WHERE shops.shop_domain = 'myshop.com' AND
customers.product_id = '4242451'
GROUP BY shops.shop_id
Relevant database schemas:
shops:
id, name, shop_domain
customers:
id, name, product_id, shop_domain
sql database postgresql
I'm trying to build an SQL query where I grab one table's information (WHERE shops.shop_domain = X
) along with the COUNT of the customers table WHERE customers.product_id = 4242451
.
The shops table DOES NOT have product.id
in it, but the customers table DOES HAVE the shop_domain
in it, hence my attempt to do some sort of join.
I essentially want to return the following:
- shops.id
- shops.name
- shops.shop_domain
COUNT OF CUSTOMERS WHERE customers.product_id = '4242451'
Here is my not so lovely attempt at the query.
I think I have the idea right (maybe...) but I can't wrap my head around building this query.
SELECT shops.id, shops.name, shops.shop_domain, COUNT(customers.customer_id)
FROM shops
LEFT JOIN customers ON shops.shop_domain = customers.shop_domain
WHERE shops.shop_domain = 'myshop.com' AND
customers.product_id = '4242451'
GROUP BY shops.shop_id
Relevant database schemas:
shops:
id, name, shop_domain
customers:
id, name, product_id, shop_domain
sql database postgresql
sql database postgresql
edited Nov 13 '18 at 21:55
Erwin Brandstetter
343k65627803
343k65627803
asked Nov 13 '18 at 19:26
Patrick BollenbachPatrick Bollenbach
6317
6317
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You are close. The condition on customers
needs to go in the ON
clause, because this is a LEFT JOIN
and customers
is the second table:
SELECT s.id, s.name, s.shop_domain, COUNT(c.customer_id)
FROM shops s LEFT JOIN
customers c
ON s.shop_domain = c.shop_domain AND c.product_id = '4242451'
WHERE s.shop_domain = 'myshop.com'
GROUP BY s.id, s.name, s.shop_domain;
I am also inclined to include all three columns in the GROUP BY
, although Postgres (and ANSI/ISO standards) are happy with just id
if it is declared as the primary key in the table.
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
Postgres is only happy with the PK in this case.UNIQUE
is not enough (short of fully implementing the standard).
– Erwin Brandstetter
Nov 13 '18 at 21:43
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
add a comment |
A correlated subquery should be substantially cheaper (and simpler) for the purpose:
SELECT id, name, shop_domain
, (SELECT count(*)
FROM customers
WHERE shop_domain = s.shop_domain
AND product_id = 4242451) AS special_count
FROM shops s
WHERE shop_domain = 'myshop.com';
This way you only need to aggregate in the subquery, and need not worry about undesired effects on the outer query.
Assuming product_id
is a numeric data type, so I use a numeric literal (4242451
) instead of a string literal '4242451'
- which might cause problems otherwise.
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%2f53288179%2fget-row-from-one-table-plus-count-from-a-related-table%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
You are close. The condition on customers
needs to go in the ON
clause, because this is a LEFT JOIN
and customers
is the second table:
SELECT s.id, s.name, s.shop_domain, COUNT(c.customer_id)
FROM shops s LEFT JOIN
customers c
ON s.shop_domain = c.shop_domain AND c.product_id = '4242451'
WHERE s.shop_domain = 'myshop.com'
GROUP BY s.id, s.name, s.shop_domain;
I am also inclined to include all three columns in the GROUP BY
, although Postgres (and ANSI/ISO standards) are happy with just id
if it is declared as the primary key in the table.
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
Postgres is only happy with the PK in this case.UNIQUE
is not enough (short of fully implementing the standard).
– Erwin Brandstetter
Nov 13 '18 at 21:43
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
add a comment |
You are close. The condition on customers
needs to go in the ON
clause, because this is a LEFT JOIN
and customers
is the second table:
SELECT s.id, s.name, s.shop_domain, COUNT(c.customer_id)
FROM shops s LEFT JOIN
customers c
ON s.shop_domain = c.shop_domain AND c.product_id = '4242451'
WHERE s.shop_domain = 'myshop.com'
GROUP BY s.id, s.name, s.shop_domain;
I am also inclined to include all three columns in the GROUP BY
, although Postgres (and ANSI/ISO standards) are happy with just id
if it is declared as the primary key in the table.
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
Postgres is only happy with the PK in this case.UNIQUE
is not enough (short of fully implementing the standard).
– Erwin Brandstetter
Nov 13 '18 at 21:43
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
add a comment |
You are close. The condition on customers
needs to go in the ON
clause, because this is a LEFT JOIN
and customers
is the second table:
SELECT s.id, s.name, s.shop_domain, COUNT(c.customer_id)
FROM shops s LEFT JOIN
customers c
ON s.shop_domain = c.shop_domain AND c.product_id = '4242451'
WHERE s.shop_domain = 'myshop.com'
GROUP BY s.id, s.name, s.shop_domain;
I am also inclined to include all three columns in the GROUP BY
, although Postgres (and ANSI/ISO standards) are happy with just id
if it is declared as the primary key in the table.
You are close. The condition on customers
needs to go in the ON
clause, because this is a LEFT JOIN
and customers
is the second table:
SELECT s.id, s.name, s.shop_domain, COUNT(c.customer_id)
FROM shops s LEFT JOIN
customers c
ON s.shop_domain = c.shop_domain AND c.product_id = '4242451'
WHERE s.shop_domain = 'myshop.com'
GROUP BY s.id, s.name, s.shop_domain;
I am also inclined to include all three columns in the GROUP BY
, although Postgres (and ANSI/ISO standards) are happy with just id
if it is declared as the primary key in the table.
edited Nov 13 '18 at 21:51
answered Nov 13 '18 at 19:28
Gordon LinoffGordon Linoff
768k35300402
768k35300402
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
Postgres is only happy with the PK in this case.UNIQUE
is not enough (short of fully implementing the standard).
– Erwin Brandstetter
Nov 13 '18 at 21:43
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
add a comment |
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
Postgres is only happy with the PK in this case.UNIQUE
is not enough (short of fully implementing the standard).
– Erwin Brandstetter
Nov 13 '18 at 21:43
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
Wow. That was incredibly quick. I just threw this in and I believe it works perfectly.... Give me a few minutes to just run a test through this. Thanks so much Gordon.
– Patrick Bollenbach
Nov 13 '18 at 19:31
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
You rock. Thanks so much Gordon, really appreciate it. Have a great day.
– Patrick Bollenbach
Nov 13 '18 at 19:40
Postgres is only happy with the PK in this case.
UNIQUE
is not enough (short of fully implementing the standard).– Erwin Brandstetter
Nov 13 '18 at 21:43
Postgres is only happy with the PK in this case.
UNIQUE
is not enough (short of fully implementing the standard).– Erwin Brandstetter
Nov 13 '18 at 21:43
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
@ErwinBrandstetter . . . Thank you for the clarifying. I see now that the documentation not only says "functional dependency" but also gives "primary key" as the definition.
– Gordon Linoff
Nov 13 '18 at 21:59
add a comment |
A correlated subquery should be substantially cheaper (and simpler) for the purpose:
SELECT id, name, shop_domain
, (SELECT count(*)
FROM customers
WHERE shop_domain = s.shop_domain
AND product_id = 4242451) AS special_count
FROM shops s
WHERE shop_domain = 'myshop.com';
This way you only need to aggregate in the subquery, and need not worry about undesired effects on the outer query.
Assuming product_id
is a numeric data type, so I use a numeric literal (4242451
) instead of a string literal '4242451'
- which might cause problems otherwise.
add a comment |
A correlated subquery should be substantially cheaper (and simpler) for the purpose:
SELECT id, name, shop_domain
, (SELECT count(*)
FROM customers
WHERE shop_domain = s.shop_domain
AND product_id = 4242451) AS special_count
FROM shops s
WHERE shop_domain = 'myshop.com';
This way you only need to aggregate in the subquery, and need not worry about undesired effects on the outer query.
Assuming product_id
is a numeric data type, so I use a numeric literal (4242451
) instead of a string literal '4242451'
- which might cause problems otherwise.
add a comment |
A correlated subquery should be substantially cheaper (and simpler) for the purpose:
SELECT id, name, shop_domain
, (SELECT count(*)
FROM customers
WHERE shop_domain = s.shop_domain
AND product_id = 4242451) AS special_count
FROM shops s
WHERE shop_domain = 'myshop.com';
This way you only need to aggregate in the subquery, and need not worry about undesired effects on the outer query.
Assuming product_id
is a numeric data type, so I use a numeric literal (4242451
) instead of a string literal '4242451'
- which might cause problems otherwise.
A correlated subquery should be substantially cheaper (and simpler) for the purpose:
SELECT id, name, shop_domain
, (SELECT count(*)
FROM customers
WHERE shop_domain = s.shop_domain
AND product_id = 4242451) AS special_count
FROM shops s
WHERE shop_domain = 'myshop.com';
This way you only need to aggregate in the subquery, and need not worry about undesired effects on the outer query.
Assuming product_id
is a numeric data type, so I use a numeric literal (4242451
) instead of a string literal '4242451'
- which might cause problems otherwise.
edited Nov 13 '18 at 21:58
answered Nov 13 '18 at 21:52
Erwin BrandstetterErwin Brandstetter
343k65627803
343k65627803
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%2f53288179%2fget-row-from-one-table-plus-count-from-a-related-table%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