how to select sum of values by id's? SQL [closed] - mysql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
So, i have a table with records like this:
ID ORDERS
1 4
1 1
2 4
3 1
3 2
I need to create a query that will lead to this situation:
ID ORDERS
1 5
2 4
I tried with select id, count(orders) from table group by id having count(orders) > 3
but all I get is a strange result in which my ids are strangely numbered.

You need to sum and not count:
select id, sum(orders)
from table
group by id
having sum(orders) > 3

MySQL supports summing up values via sum like follows:
select id, sum(orders) from test group by id;
See also DB Fiddle.

Change your attempt so that you SUM(orders) not COUNT(orders)
Count doesn't work because it increments a counter by one for any non null value, and doesn't increment for a null value. In the the following table:
ID, name
1, John
2, null
3, Jane
null, null
COUNT(id) will return 3 - there are 3 non null ID values. COUNT(name) will return 2 - there are two non null values. SUM(ID) will return 6 - the sum of the numeric values of ID is 6. SUM looks at what the values are, COUNT doesn't. SUM will treat nulls as 0. The special case COUNT(*) will count the presence of the row even if all its values are null. COUNT(*) will return 4 - there are 4 rows in the results

Related

Count and sum up all duplicate records in MySQL

I have table with, following structure.
id name
1 john
2 ana
3 john
4 ana
5 peter
6 ana
7 Abrar
8 Raju
Duplicate entries in the table are as follows
john(2) duplicate
ana(3) duplicate
The names which are duplicates are john and ana.
My question is how would I count the records in total which are duplicate here it is '5' records
Note : I also followed the similar question in community but it explains how we can add the number of duplicates exists for that particular name in the table and adds up the third column in table representing the duplicates records with same name but in my case I wanted to know the number of all duplicates exist in the table (here the result of the query is just number "5") irrespective of the names.
Just take a count subquery on the query you already have in mind (or perhaps have already written):
SELECT SUM(cnt) AS total_duplicates
FROM
(
SELECT COUNT(*) AS cnt
FROM yourTable
GROUP BY name
HAVING COUNT(*) > 1
) t;
Demo

MySQL Breaking down Group By results to add more details to grouping [duplicate]

This question already has answers here:
need to return two sets of data with two different where clauses
(2 answers)
Closed 6 years ago.
I have a mysql table that has columns for 'created_at' and 'type', among others. What I am trying to do is get the total number of rows created each week, and further have that number broken down by 'type'.
That is, if my data is as follows:
created_at | type
10/15/2015 car
10/15/2015 bike
10/16/2015 car
10/24/2015 car
10/24/2015 car
10/25/2015 car
10/26/2015 bike
I want my query to return something like:
Week of | Total | car | bike
10/15/2015 3 2 1
10/22/2015 4 3 1
From this stackoverflow question (How to group by week in MySQL?), I am able to group by week, but I am having difficulty further breaking down the results by 'type'. The closest I have gotten is:
SELECT
FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7)) AS week_of,
type,
COUNT(*) AS 'rows'
FROM mytable
GROUP BY FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7)), type
ORDER BY FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7));
Which is a bit different then what I was trying to get, but its something I can still work with. However, it doesn't include the total per week (would prefer having all the data I want returned in the result set). How would I go about doing this?
Since I don't know the values in 'type', I don't want to specify those in the query directly, especially since those can be changed over time.
Use group by and week
select week(created_at), type, count(*)
from my_table
where year(created_at) = year(curdate())
group by week(created_at), type

Simple SQL Union 2 queries on the same table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
Trying to get all rows where userid = 'me'
Then 2 newest rows where userid <> 'me'
Items:
Userid time
other2 11
other3 10
me 10
me 8
other1 8
other3 7
me 6
would return
Userid time
me 10
me 8
me 6
other2 11
other3 10
The results don't have to be in any order
(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items WHERE userid<>'me'
ORDER BY time DESC LIMIT 2)
This only outputs 2 rows
The sql was correct as shown above
(SELECT * FROM Items WHERE userid='me' )
UNION ALL
(SELECT * FROM Items WHERE userid<>'me'
ORDER BY time DESC LIMIT 2)
Union All concatenates the two queries, and the parenthesis allow different WHERE/ORDER BY/LIMIT clauses.
Demonstrated here:
http://sqlfiddle.com/#!9/ca405/1/0

Executing "simple" sql query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here is data in my table :
id name surname place
1 test1 isdad YES
2 test2 spreda YES
3 test3 me NO
4 test4 smallvile YES
What I'd like to get, all ids where place='YES'. So Ids are
1
2
4
and the result count is 3. I want to append that number to the end of the results, so the result should look like this:
1
2
4
3
There is almost definitely a better way to do this. That said, you could do:
SELECT id FROM myTable WHERE place = 'YES'
UNION ALL
SELECT COUNT(*) FROM myTable WHERE place = 'YES'
UNION ALL will append the result number as a new row rather than attempting to add it as another column.
Please don't do this; the results will be mixed, it stands in stalwart defiance of good software design and will be baffling to anyone who comes along later and has to work on this piece of code.
In MySQL, you can do something similar to this in a single query using WITH ROLLUP:
select id, count(*)
from myTable
where place='YES'
group by id
with rollup
SELECT id FROM TABLE WHERE place='YES'
will return with the ids what you want, and the rowcount will store the count number, if you use this with php or c++/c#

Mysql Covert rows to columns

I have a table with order numbers, first name, last name, question and answers. There are 5 questions asked to the user, each answer to a question generates 1 row of data, which produces 5 rows per user. I need a query that returns order number, first name, last name and the questions and answers converted to columns, returning 1 row per user.
Any help would be appreciated
Thanks,
Larry
Seems like you want to join the table to itself 5 times.
Something like
select q1.first_name, q1.last_name, max(q1.question), max(q1.answer), max(q2.question), max(q2.answer),max(q3.question), max(q3.answer),...
from questions q1
join questions q2 on q1.first_name=q2.first_name and q1.last_name=q2.last_name
join questions q3 on q1.first_name=q3.first_name and q1.last_name=q3.last_name
where q1.order_number = 1 and q2.order_number = 2 and q3.order_number = 3 ...
group by q1.first_name, q1.last_name
Using max will collapse down the rows into unique first name/last name pairs.