Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have table of the form:
A 2
A 2
A 2
A 2
A 3
A 4
A 4
A 4
I want output of the form:
A 2 3
A 3 1
A 4 2
What will be the most efficient query for this problem ?
select col1, col2, greatest(count(*) - 1, 1)
from your_table
group by col1, col2
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Let's say i have a table Task_status like below:
id TaskId SubTaskId status
1 1 1 Complete
2 1 2 Complete
3 1 3 Complete
4 2 4 InProgress
5 2 5 Complete
I want to find all the taskId whose all child tasks are Complete. How can I write that query?
Use group by and having to check if the number of rows per task equal the number of rows with Complete status.
select taskId
from tbl
group by taskId
having count(*) = sum(status='Complete')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table as follows:
name week effort
quentin 1 1
quentin 1 2
quentin 2 1
tracy 1 1
joe 2 2
There will only be a handful of unique names so it doesn't need to be dynamic
And I would like to query it to return something like
week QuentinEffortSum TracyEffortSum JoeEffortSum
1 3 1 0
2 1 0 2
I have tried something along the lines of
SELECT SUM(Effort) AS JoeEffort, Min (Week) AS week FROM [Group$]
WHERE name = "Joe"
GROUP BY week
ORDER By week
which returned:
week JoeEffort
1 3
2 1
and now I need the other columns and imagine in involves joins but am not sure how to complete the task
Please help
Thanks
I think a PIVOT table would work, like so:
SELECT *
FROM (
SELECT
week,name,effort
FROM [Group$]
) as s
PIVOT
(
SUM(effort)
FOR [name] IN ('quentin','joe','tracy')
)AS pvt
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My mySQL look like this
id visits
3 0
3 0
3 1
4 1
4 1
4 0
2 1
How can I have something more like group for all my entries id :
id visits
3 1
4 2
2 1
Use this query:
SELECT ID, SUM(visits) as visit FROM YOURTABLE GROUP BY ID;
SQLFIDDEL is here.
Use Group By Clause
SELECT ID, SUM(Visits) FROM YOURTABLE GROUP BY ID;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a database like this:
column1 column2 column3
15 21 19
17 12 13
15 24 14
Now I want to get a list of every single number like this:
x value
1 15
2 21
3 19
4 17
5 12
...
Thank you!
Try this:
SELECT (#auto:=#auto+1) AS num, val
FROM (SELECT column1 AS val
FROM tableA
UNION
SELECT column2 AS val
FROM tableA
UNION
SELECT column3 AS val
FROM tableA
) AS A, (SELECT #auto:=0) AS B
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this table with 2 columns of interest: ID, Memo_No plus some other columns.
ID is primary key of int type and auto-increment. Memo_No. is also int.
I need query, such that if memo no. is occuring only once in the table, the ID associated with it have to be selected.Plus, if same Memo has two different ID's, ID which is greater has to be selected.
For example i have table like this:
ID || Memo_No
1 2
2 3
3 4
4 5
5 4
6 6
7 2
From above table, I want to select rows whose ID's are 2,4,5,6,7 .
Thanks.
This is quite easy as an aggregation:
select max(id), memo_no
from t
group by memo_no;