MySQL - sort result by specific record - mysql

Perhaps someone could tell me, how to order MySQL output in this specific scenario:
I have table like this:
| id | Value1 | Value2 | ...More values that doesn`t matter in this example
^-----^--------^--------^
| 1 | 1 | X |
| 2 | 2 | X |
| 3 | 3 | 2 |
| 4 | 1 | X |
| 5 | 2 | X |
| 6 | 3 | 3 |
| 7 | 1 | X |
| 8 | 2 | X |
| 9 | 3 | 1 |
I want to get values from this table, and I want to order them by Value2, but only there, where Value1 is 3 (X values doesn't matter).
What`s the best way to do this, with good performance?
Thanks in advance!

Hmmm, do you want where and order by?
select t.*
from t
where value1 = 3
order by value2;
EDIT:
Based on the comment:
select t.*
from t
order by (value1 = 3) desc, -- put value 3 first
value2

SELECT * FROM TABLE WHERE Value1=3 ORDER BY Value2

Try with this please:
SELECT * FROM TABLE WHERE Value1=3 ORDER BY Value2 UNION SELECT * FROM TABLE WHERE Value1!=3

Related

SQL: Create Column (MySQL/PHPMyAdmin)

With the query:
SELECT TableA.ID, TableA.SensorID, TableA.Value, SensorIDs.Name, TableA.timestamp
FROM TableA
JOIN SensorIDs
ON TableA.SensorID = SensorIDs.ID // column 'Name' is in 'SensorIDs'
My result table looks like this:
ID | SensorID | Value | Name | timestamp
1 | 1 | 5 | A | 1000
2 | 2 | 10 | B | 1000
3 | 3 | 0 | C | 1000
4 | 1 | 1 | A | 2000
5 | 2 | 2 | B | 2000
6 | 3 | 6 | C | 2000
[..]
Is there a way to change my SQL query to get a table like this:
A | B | C | timestamp
5 | 10 | 0 | 1000
1 | 2 | 6 | 2000
Something with GROUP BY maybe?
EDIT: In the forseeable future there will be only these 3 values for 'Name'.
EDIT: RDBMS: MySQL-native (InnoDB), PHPMyAdmin
EDIT: Forgot to add column "SensorID" in the result.
I found the answer, by creating a PIVOT table with the tutorial I found here:
https://ubiq.co/database-blog/how-to-create-pivot-table-in-mysql/
SELECT time,
sum(IF(SensorID=1, Value, NULL)) AS Sensor1,
sum(IF(SensorID=2, Value, NULL)) AS Sensor2,
sum(IF(SensorID=3, Value, NULL)) AS Sensor3,
sum(IF(SensorID=4, Value, NULL)) AS Sensor4
FROM TableA
GROUP BY time

Geting latest "value" of secondary identifier

i need to get into select last values (with highest IDa) with specific identifier (IDb).
For example - input:
|IDa|IDb| text
================
| 1 | 1 | What
| 2 | 2 | code
| 3 | 1 | should
| 4 | 2 | I
| 5 | 2 | write
| 6 | 3 | here
| 7 | 2 | ?
wanted output:
|IDa|IDb| text
================
| 3 | 1 | should
| 7 | 2 | ?
| 6 | 3 | here
Thank you for any help.
If IDa is set to auto_increment you can use a self join
select t.*
from t
join (select max(IDa) IDa,IDb from t group by IDb ) t1
using(IDa,IDb) /* is equivalent to on(t.IDa = t1.IDa and t.IDb = t1.IDb) */
order by t.IDb
Also in your expected result set you have mentioned what for IDb 1 and according to your question there should be the word should because its the last one for IDb 1
Fiddle Demo

MySQL Multiple Row Compare

I'm trying to run a query that will compare multiple rows in the same table and shows me all results that match.
For example, my data will look like this:
+-------------+-----------+---------+------------+-------+
| activity_id | d_id | tech_id | timestamp | value |
+-------------+-----------+---------+------------+-------+
| 39248078 | 1 | 1 | 2014-03-09 | 1 |
| 39248079 | 2 | 1 | 2014-03-06 | 1 |
| 39248082 | 3 | 1 | 2014-04-09 | 0 |
| 39248085 | 1 | 2 | 2014-03-13 | 1 |
| 39248088 | 3 | 2 | 2014-07-17 | 1 |
| 39248091 | 1 | 3 | 2014-02-07 | 1 |
| 39248093 | 2 | 3 | 2014-12-02 | 0 |
+-------------+-----------+---------+------------+-------+
The goal is to get all d_ids where tech_id = 3 AND (tech_id = 1 OR tech_id = 2). So in this case, the result should be 1 and 2 but not 3.
I've looked into subqueries but wasn't able to get it to work. Any help would be much appreciated.
SELECT
d_id
FROM table
WHERE d_id IN(
SELECT
d_id
FROM table
WHERE tech_id=3
) AND tech_id=1 OR tech_id=2
Let me know if it didn't work!
maybe something like this?
I havent tested it but from what I understand something like this should work
SELECT * FROM (
SELECT activity_id, d_id, tech_id, timestamp, value FROM test
WHERE tech_id = 3
) as temp
JOIN test t on t.d_id = temp.d_id
WHERE t.tech_id = 1 OR t.tech_id = 2
with CTE as
(
select * from table
where tech_id = 3
)
select c.d_id
from CTE c
where tech_id = 2 or tech_id = 3
Edited:
with CTE as
(
select * from table
where tech_id = 3
)
select c.d_id
from CTE c
where tech_id = 2 or tech_id = 3

MySQL sum per IDs without subqueries

I'm working on a huge dataset, with a table that looks like this :
+----+---------+--------+--------+
| id | otherid | value1 | value2 |
+----+---------+--------+--------+
| 1 | 1 | 2 | 5 |
| 1 | 1 | 4 | 8 |
| 1 | 2 | 3 | 6 |
| 2 | 123 | 1 | 4 |
+----+---------+--------+--------+
I need to multiply value1 and value2 for each row, and sum values per id and otherid. A result table might be:
+----+---------+-----+
| id | otherid | sum |
+----+---------+-----+
| 1 | 1 | 42 | ((2*5)+(4*8))
| 1 | 2 | 18 | (3*6)
| 2 | 123 | 4 | (1*4)
+----+---------+-----+
My question is if it is possible to avoid subqueries to do this, I only found solutions that used them.
Thanks!
it's easy.
SELECT id,
otherid,
SUM(value1*value2) AS sum
FROM your_table
GROUP BY id, otherid;
Try Below Query
SELECT ID,otherid ,SUM(value1 * value2) sum
FROM TABLE1
GROUP BY ID,otherid

Distinct on specific column in Hive

I am running Hive 071
I have a table, with mulitple rows, with the same column value
e.g.
x | y |
---------
1 | 2 |
1 | 3 |
1 | 4 |
2 | 2 |
3 | 2 |
3 | 1 |
I want to have the x column unique, and remove rows that have the same x val
e.g.
x | y |
---------
1 | 2 |
2 | 2 |
3 | 2 |
or
x | y |
---------
1 | 4 |
2 | 2 |
3 | 1 |
are both good
as distinct works only on the whole rs in hive, I couldn't find a way to do it
help please
Tx
You can use the distinct keyword:
SELECT DISTINCT x FROM table
try following query to get result :
select A.x , A.y from (select x , y , rank() over ( partition by x order by y) as ranked from testingg)A where ranked=1;