How to get different row counts of two different tables in one query with help of group by clause
up vote
0
down vote
favorite
I have one Parent table(Order) and one child table(Item). One user can order multiple orders in one day and one order may have multiple Item.
My table structure like,
I would like to get result like, in one how many orders and Items have ordered by particular user.
I need result like
Thank you.
mysql database oracle11g database-administration
add a comment |
up vote
0
down vote
favorite
I have one Parent table(Order) and one child table(Item). One user can order multiple orders in one day and one order may have multiple Item.
My table structure like,
I would like to get result like, in one how many orders and Items have ordered by particular user.
I need result like
Thank you.
mysql database oracle11g database-administration
Image not opening.. Cant help.
– Krishna
Nov 22 at 13:40
1
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Nov 22 at 13:40
Can you just mention column names in text for used tables so that we can help.
– Krishna
Nov 22 at 13:42
please decide about the database system.
– Barbaros Özhan
Nov 22 at 15:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have one Parent table(Order) and one child table(Item). One user can order multiple orders in one day and one order may have multiple Item.
My table structure like,
I would like to get result like, in one how many orders and Items have ordered by particular user.
I need result like
Thank you.
mysql database oracle11g database-administration
I have one Parent table(Order) and one child table(Item). One user can order multiple orders in one day and one order may have multiple Item.
My table structure like,
I would like to get result like, in one how many orders and Items have ordered by particular user.
I need result like
Thank you.
mysql database oracle11g database-administration
mysql database oracle11g database-administration
edited Nov 22 at 13:40
Salman A
174k66334421
174k66334421
asked Nov 22 at 13:35
SGK
11
11
Image not opening.. Cant help.
– Krishna
Nov 22 at 13:40
1
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Nov 22 at 13:40
Can you just mention column names in text for used tables so that we can help.
– Krishna
Nov 22 at 13:42
please decide about the database system.
– Barbaros Özhan
Nov 22 at 15:54
add a comment |
Image not opening.. Cant help.
– Krishna
Nov 22 at 13:40
1
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Nov 22 at 13:40
Can you just mention column names in text for used tables so that we can help.
– Krishna
Nov 22 at 13:42
please decide about the database system.
– Barbaros Özhan
Nov 22 at 15:54
Image not opening.. Cant help.
– Krishna
Nov 22 at 13:40
Image not opening.. Cant help.
– Krishna
Nov 22 at 13:40
1
1
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Nov 22 at 13:40
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Nov 22 at 13:40
Can you just mention column names in text for used tables so that we can help.
– Krishna
Nov 22 at 13:42
Can you just mention column names in text for used tables so that we can help.
– Krishna
Nov 22 at 13:42
please decide about the database system.
– Barbaros Özhan
Nov 22 at 15:54
please decide about the database system.
– Barbaros Özhan
Nov 22 at 15:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Try to use distinct count
for the count columns
, and grouping by user_id
is enough as :
select o.user_id as "User Id",
count(distinct o.id_order) as "Count(Order)",
count(distinct i.id_item) as "Count(Item)",
max("date") as "Date"
from orders o
join item i on o.id_order = i.id_order
group by user_id;
User Id Count(Order) Count(Item) Date
------- ------------ ----------- ----------
1 3 7 22.11.2018
Rextester Demo
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
add a comment |
up vote
0
down vote
You have to use INNER JOIN for user,order and item table. also use GROUP BY clause for group each date and order.
SELECT user_id,
count(o.id_order),
count(item.id_item)
FROM USER
INNER JOIN
`ORDER` o ON user.id=o.id_order
INNER JOIN item i ON o.id_order=o.id_order
GROUP BY o.Date, o.id_order
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try to use distinct count
for the count columns
, and grouping by user_id
is enough as :
select o.user_id as "User Id",
count(distinct o.id_order) as "Count(Order)",
count(distinct i.id_item) as "Count(Item)",
max("date") as "Date"
from orders o
join item i on o.id_order = i.id_order
group by user_id;
User Id Count(Order) Count(Item) Date
------- ------------ ----------- ----------
1 3 7 22.11.2018
Rextester Demo
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
add a comment |
up vote
0
down vote
accepted
Try to use distinct count
for the count columns
, and grouping by user_id
is enough as :
select o.user_id as "User Id",
count(distinct o.id_order) as "Count(Order)",
count(distinct i.id_item) as "Count(Item)",
max("date") as "Date"
from orders o
join item i on o.id_order = i.id_order
group by user_id;
User Id Count(Order) Count(Item) Date
------- ------------ ----------- ----------
1 3 7 22.11.2018
Rextester Demo
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try to use distinct count
for the count columns
, and grouping by user_id
is enough as :
select o.user_id as "User Id",
count(distinct o.id_order) as "Count(Order)",
count(distinct i.id_item) as "Count(Item)",
max("date") as "Date"
from orders o
join item i on o.id_order = i.id_order
group by user_id;
User Id Count(Order) Count(Item) Date
------- ------------ ----------- ----------
1 3 7 22.11.2018
Rextester Demo
Try to use distinct count
for the count columns
, and grouping by user_id
is enough as :
select o.user_id as "User Id",
count(distinct o.id_order) as "Count(Order)",
count(distinct i.id_item) as "Count(Item)",
max("date") as "Date"
from orders o
join item i on o.id_order = i.id_order
group by user_id;
User Id Count(Order) Count(Item) Date
------- ------------ ----------- ----------
1 3 7 22.11.2018
Rextester Demo
answered Nov 22 at 16:11
Barbaros Özhan
11.7k71530
11.7k71530
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
add a comment |
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
Thanks!!. this works for me. Only I need a data date wise also. So i have removed max(date) and keep it in group by clause.
– SGK
Nov 23 at 8:56
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
@swapnil you're welcome friend. Don't forget to mark the answer please.
– Barbaros Özhan
Nov 23 at 9:33
add a comment |
up vote
0
down vote
You have to use INNER JOIN for user,order and item table. also use GROUP BY clause for group each date and order.
SELECT user_id,
count(o.id_order),
count(item.id_item)
FROM USER
INNER JOIN
`ORDER` o ON user.id=o.id_order
INNER JOIN item i ON o.id_order=o.id_order
GROUP BY o.Date, o.id_order
add a comment |
up vote
0
down vote
You have to use INNER JOIN for user,order and item table. also use GROUP BY clause for group each date and order.
SELECT user_id,
count(o.id_order),
count(item.id_item)
FROM USER
INNER JOIN
`ORDER` o ON user.id=o.id_order
INNER JOIN item i ON o.id_order=o.id_order
GROUP BY o.Date, o.id_order
add a comment |
up vote
0
down vote
up vote
0
down vote
You have to use INNER JOIN for user,order and item table. also use GROUP BY clause for group each date and order.
SELECT user_id,
count(o.id_order),
count(item.id_item)
FROM USER
INNER JOIN
`ORDER` o ON user.id=o.id_order
INNER JOIN item i ON o.id_order=o.id_order
GROUP BY o.Date, o.id_order
You have to use INNER JOIN for user,order and item table. also use GROUP BY clause for group each date and order.
SELECT user_id,
count(o.id_order),
count(item.id_item)
FROM USER
INNER JOIN
`ORDER` o ON user.id=o.id_order
INNER JOIN item i ON o.id_order=o.id_order
GROUP BY o.Date, o.id_order
edited Nov 22 at 13:50
answered Nov 22 at 13:45
Sadikhasan
13.9k125891
13.9k125891
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53432193%2fhow-to-get-different-row-counts-of-two-different-tables-in-one-query-with-help-o%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
Image not opening.. Cant help.
– Krishna
Nov 22 at 13:40
1
See: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
– Strawberry
Nov 22 at 13:40
Can you just mention column names in text for used tables so that we can help.
– Krishna
Nov 22 at 13:42
please decide about the database system.
– Barbaros Özhan
Nov 22 at 15:54