How to order by max(a,b) in SQL? - mysql

Consider the following table:
id a b
--------------
1 5 1
2 2 3
3 4 2
4 3 6
5 0 1
6 2 2
I would like to order it by max(a,b) in descending order, so that the result will be:
id a b
--------------
4 3 6
1 5 1
3 4 2
2 2 3
6 2 2
5 0 1
What will be the SQL query to perform such ordering ?

Use GREATEST :
SELECT *
FROM table
ORDER BY GREATEST(a, b) DESC

Related

MS Access Sum and Averaging Columns with Same Information as New Column

So I have the following chart here where I have columns:
a b c d
1 1 1 3
1 1 1 4
1 1 1 5
2 2 2 1
2 2 2 3
2 2 2 3
3 3 3 4
3 3 3 5
3 3 3 6
What I want to do is add and average column d where columns a,b,c contain the same values. How would I go about doing this?
I imagine it would be something like
Select SUM(Table.d) where a = b AND b = c AS e
Try this:
Select SUM(Table.d), AVG(Table.d) from Table Group by Table.a, Table.b, Table.c

MySQL: Group By Max Value Returns Duplicate Results

I have below customer table with max qty.
I need a unique user who has max qty order by its group id.
Input:
id, customer_id, customer_group_id qty
1 1 3 1
2 1 3 10
3 1 3 5
4 2 2 10
5 2 2 1
6 2 2 2
7 3 1 5
8 3 1 10
9 4 4 1
10 4 4 2
11 4 4 2
Output should be:
id, customer_id, customer_group_id, qty
11 4 4 2
10 4 4 2 - This should be not selected
2 1 3 10
4 2 2 10
8 3 1 10
Query:
SELECT * FROM customer
WHERE qty IN ( SELECT MAX(qty) FROM customer GROUP BY customer_id)
ORDER BY customer_group_id DESC;
I tried above query but seems not working.

Multiple row show in one row as sum in sql statement

ID |A |B
1 1 3
1 412 2
2 567 3
2 567 1
3 2 3
3 5 4
4 6 1
4 8 2
4 2 3
I want to get table:
ID |A |B
1 413 5
2 1134 4
3 7 7
4 16 6
As was pointed out, you want to use a group by clause for aggregate functions. In this case, you are summing the values for A and B, and grouping them by ID. The syntax is as follows:
SELECT ID, SUM(A), SUM(B)
FROM tablename
GROUP BY ID

Tag duplicates with first occurrence ID

I'm using the clustercommand and am having difficulties due to insufficient memory. To get around this problem I would like to delete all duplicate observations.
I would like to cluster via the variables A, B and C and I identify duplicate values as so:
/* Create dummy data */
input id A B C
1 1 1 1
2 1 1 1
3 1 1 1
4 2 2 2
5 2 2 2
6 2 2 2
7 2 2 2
8 3 3 3
9 3 3 3
10 4 4 4
end
sort A B C id
duplicates tag A B C, gen(dup_tag)
I would like to add a variable dup_ID which tells me that ids 2 and 3 are duplicates of id 1, ids 5 and 6 of id 4, and so on. How could I do this?
/* Desired result */
id A B C dup_id
1 1 1 1 1
2 1 1 1 1
3 1 1 1 1
4 2 2 2 4
5 2 2 2 4
6 2 2 2 4
7 2 2 2 4
8 3 3 3 8
9 3 3 3 8
10 4 4 4 10
duplicates is a wonderful command (see its manual entry for why I say that), but you can do this directly:
bysort A B C : gen tag = _n == 1
tags the first occurrence of duplicates of A B C as 1 and all others as 0. For the other way round use _n > 1, _n != 1, or whatever.
EDIT:
So then the id of tagged observations is just
by A B C: gen dup_id = id[1]
For basic technique with by: see (e.g.) this discussion
You can refer to the first observation in each group of A B C using the subscript [1] on ID. Note the (id) argument in bysort, which sorts by id, but identifies the groups by A, B, and C only.
clear
input id A B C
1 1 1 1
2 1 1 1
3 1 1 1
4 2 2 2
5 2 2 2
6 2 2 2
7 2 2 2
8 3 3 3
9 3 3 3
10 4 4 4
end
bysort A B C (id): gen dup_id = id[1]
li, noobs sepby(dup_id)
yielding
+-------------------------+
| id A B C dup_id |
|-------------------------|
| 1 1 1 1 1 |
| 2 1 1 1 1 |
| 3 1 1 1 1 |
|-------------------------|
| 4 2 2 2 4 |
| 5 2 2 2 4 |
| 6 2 2 2 4 |
| 7 2 2 2 4 |
|-------------------------|
| 8 3 3 3 8 |
| 9 3 3 3 8 |
|-------------------------|
| 10 4 4 4 10 |
+-------------------------+

How to get differents values 'types' from table creating virtual columns - MySQL

I have these tables.
tb_employee:
ID_EMP NAME_EMP
1 Employee 1
2 Employee 2
3 Employee 3
4 Employee 4
tb_requirements:
ID_REQ DESCRIPTION_REQ TYPE_REQ
1 Requirement 1 1
2 Requirement 2 1
3 Requirement 3 1
4 Requirement 4 2
5 Requirement 5 2
6 Requirement 6 2
7 Requirement 7 2
tb_detail:
ID_DET ID_EMP ID_REQ
1 1 1
2 1 2
3 1 4
4 2 1
5 2 6
6 3 4
7 3 7
I need to make a SELECT QUERY to count how many requirements each employee has got and which type, like this:
ID_EMP NAME_EMP TYPE_REQ1(virtual column) TYPE_REQ2 (virt. c.)
1 Employee 1 2 4
2 Employee 2 1 1
3 Employee 3 0 2
4 Employee 4 0 0
I really don't know how to do it.
Try this one
SELECT
e.ID_EMP
,e.NAME_EMP
,(CASE WHEN SUM(r.TYPE_REQ=1) IS NULL THEN 0 ELSE SUM(r.TYPE_REQ=1) END ) TYPE_REQ1
,(CASE WHEN SUM(r.TYPE_REQ=2) IS NULL THEN 0 ELSE SUM(r.TYPE_REQ=2) END ) TYPE_REQ2
FROM
tb_employee e
LEFT JOIN tb_detail d ON (e.ID_EMP=d.ID_EMP)
LEFT JOIN tb_requirements r ON (d.ID_REQ=r.ID_REQ)
GROUP BY e.ID_EMP