How to find the sum of a temporary column in SQL? - mysql

I have this code:
SELECT ID, Name, 100 AS TempColumn
FROM MyTable;
And the table is this:
| ID | Name | TempColumn|
-------------------------
| 1 | A | 100 |
-------------------------
| 2 | B | 100 |
-------------------------
| 3 | C | 100 |
-------------------------
| 1 | A | 100 |
-------------------------
| 4 | D | 100 |
-------------------------
Now I want to find the sum of the |TempColumn| where ID=1.
So it should look like this:
| ID | Name | TempColumn|
-------------------------
| 1 | A | 200 |
-------------------------
How can I query this?

You can sum a constant:
SELECT ID, Name, SUM(100) AS SumOfTempColumn
FROM MyTable
WHERE ID = 1
GROUP BY ID, Name;
Example on SQL Fiddle

Should this not be reasonably straight forward using an aggregate query?
SELECT ID,
Name,
SUM(100) AS TempColumn
FROM MyTable
GROUP BY ID, Name;

SELECT ID, Name,SUM(TempColumn) AS TempColumn
FROM Table
WHERE ID = 1
GROUP BY ID, Name

Related

How to select only id by the unique value of other table

I have a table customers. How can I select only ids for the distinct values in the email column?
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | sergiy#gmail.com |
| 2 | bob#gmail.com |
| 3 | anre#gmail.com |
| 4 | sergiy#gmail.com |
| 5 | antony#gmail.com |
+----+------------------+
So the result will be:
[1,2,3,5]
So far, I've tried:
select distinct id, email from customers group by id;
select min(id) from Customers group by email;

MySQL, get a sum from column after grouped by another column

I have a DB table votes that looks like
| id | val | against_id | created_at |
-----------------------------------------------------
A | 1 | B | xxx
B | -1 | A | xxx
C | 1 | B | xxx
B | -1 | C | xxx
...
For each "vote" there are 2 rows.
I want to get a result that lists every id distinctly with the sum() of val, like:
| id | score |
-----------------
A | 42 |
B | 5 |
C | -15 |
D | 150 |
Is there an equivalent of foreach in mysql?
Your question isn't that clear but have you tried a simple, SUM/GROUP BY?
SELECT ID, SUM(Score) FROM MyTable GROUP BY ID

Find the maximum value(s) from a column and selecting their rows

After looking at other examples I still have not been able to find a solution, that is why I am asking for some help.
My table structure:
V_id | name | group_id | other columns
----------------------
1 | | 1
2 | | 1
3 | | 2
4 | | 3
5 | | 3
I have been struggling to build a query, to select all the rows which have the maximum value from the group_id column.
therefore output should be like this:
V_id | name | group_id | other columns
----------------------
4 | | 3
5 | | 3
which I believe can be solved by selecting all records where group_id is the highest.
and also need a query to get all the other remaining rows.
which in this case, should be like this:
V_id | name | group_id | other columns
----------------------
1 | | 1
2 | | 1
3 | | 2
which I believe can be done by selecting all records where group_id < Max(group_id)
for the first part of the problem,
SELECT *
FROM tableName
WHERE group_id = (SELECT MAX(group_ID) FROM TableName)
and for the second part,
SELECT *
FROM tableName
WHERE group_id < (SELECT MAX(group_ID) FROM TableName)
You can use JOIN for that:
SELECT a.*
FROM Table1 a
JOIN (SELECT MAX(Group_ID) AS MAXID
FROM Table1) B
ON a.Group_id = B.MaxID;
Result:
| V_ID | NAME | GROUP_ID |
----------------------------
| 4 | (null) | 3 |
| 5 | (null) | 3 |
For the remaining rows use LEFT JOIN with a condition like this:
SELECT a.*
FROM Table1 a
LEFT JOIN (SELECT MAX(Group_ID) AS MAXID
FROM Table1) B
ON a.Group_id = B.MaxID
WHERE B.MaxID IS NULL;
Result:
| V_ID | NAME | GROUP_ID |
----------------------------
| 1 | (null) | 1 |
| 2 | (null) | 1 |
| 3 | (null) | 2 |
See this SQLFiddle

MySQL GROUP BY "and filter"

Let's say i have query like this:
SELECT name, GROUP_CONCAT(number)
FROM objects
GROUP BY name
And it outputs:
+----------+----------------------+
| NAME | GROUP_CONCAT(NUMBER) |
+----------+----------------------+
| false_1 | 2,1 |
| false_2 | 3,4 |
| true_1 | 4,3,2,1 |
| true_2 | 2,3 |
+----------+----------------------+
Now how can i return rows having 2 AND 3 as number?
Note: This query is grouped - table has 10 rows, like so:
+---------+--------+
| NAME | NUMBER |
+---------+--------+
| true_1 | 1 |
| true_1 | 2 |
| true_1 | 3 |
| ... | ... |
+---------+--------+
[Link to SQLFiddle]
SELECT name, GROUP_CONCAT(number)
FROM objects
WHERE number IN (2,3)
GROUP BY name
HAVING COUNT(*) = 2
SEE SQLFiddle Demo
or if you want to retain all value on which the name has,
SELECT a.name, GROUP_CONCAT(A.number)
FROM objects a
INNER JOIN
(
SELECT name
FROM objects
WHERE number IN (2,3)
GROUP BY name
HAVING COUNT(*) = 2
) b ON a.Name = b.Name
GROUP BY a.name
SEE SQLFiddle Demo

Sort GROUP BY agregats

Imagine this table t1,
+----------+-------+--------+
| group_id | name | age |
+----------+-------+--------+
| 1 | A1 | 1 |
| 1 | A2 | 2 |
| 1 | A3 | 3 |
| 2 | B1 | 4 |
+----------+-------+--------+
Using the following query in MySQL,
SELECT group_id, name, COUNT(*) FROM t1 GROUP BY group_id
we get,
+----------+-------+--------+----------+
| group_id | name | age | COUNT(*) |
+----------+-------+--------+----------+
| 1 | A1 | 2 | 3 |
| 2 | B1 | 4 | 1 |
+----------+-------+--------+----------+
As you can see here, it's possible that values name=A1 and age=2 are not from the same record.
My question is, how can I control which single results form the name and age columns are shown, so the content is from one record? Is there a way to sort them in some way? Fro example sorting by age in reverse order would give
+----------+-------+--------+----------+
| group_id | name | age | COUNT(*) |
+----------+-------+--------+----------+
| 1 | A3 | 3 | 3 |
| 2 | B1 | 4 | 1 |
+----------+-------+--------+----------+
Thanks.
I don't know why do you say that your query works. You should also group by name...
SELECT group_id, name, COUNT(*) FROM t1 GROUP BY group_id, name
If you want to get only one of them, try:
SELECT group_id, MIN(name), COUNT(*) FROM t1 GROUP BY group_id
I don't know about full control, but you can do like this
SELECT student_name, MIN(test_score), MAX(test_score)
FROM student
GROUP BY student_name;
SELECT group_id, name, COUNT(*)
FROM t1
WHERE name IN ( 'xxx', 'yyy', ..., 'zzz' )
GROUP BY group_id
SORT BY COUNT(*)