MySQL sorting by 3 columns or UNION them - mysql

I have a problem understandint ORDER BY in MySQL. I have to sort a table by 3 criteria
1 - first i want to sort by TYPE_OF_WORK so all data must be alphabetical, like
dech_rap_bus
dech_rap_bus
ger_dem_dech_bus
ger_dem_dech_bus
ger_dem_stp_pp
...
RESULT => http://sqlfiddle.com/#!2/b2a858/6
2 - second i want to sort by PROJECT_VERSION so all data must be alphabetical, but respecting the 1 criteria, like
dech_rap_bus V123-V1234
dech_rap_bus V300
ger_dem_dech_bus V123-V1234
ger_dem_dech_bus V300
ger_dem_stp_pp V123-V1234
RESULT => http://sqlfiddle.com/#!2/b2a858/7
So 1 and 2 are working perfectly.
3 - and after this i want to sort by the column not_existing
RESULT => http://sqlfiddle.com/#!2/b2a858/5
and i don't know what it really do, but i see no results ... i just want that the
dech_rap_bus V300
where the NOT_EXISTING column is 1 to be at the end, and when are more of NOT_EXISTING = 1 to sort them all but at the end of the table.
I tought to myself that a UNION of 2 selects would help me
/* Selecting all data where not_existing is not 1 or NULL ---> working good! */
(
SELECT
*
FROM
atm_mti_view
WHERE
project_function='FRS01' AND
on_big_project_id = 12 AND
(not_existing != 1 OR not_existing IS NULL)
ORDER BY
type_of_work ASC,
project_version ASC
)
UNION
/* Selecting all data where not_existing is 1 ---> working good! */
(
SELECT
*
FROM
atm_mti_view
WHERE
project_function='FRS01' AND
on_big_project_id = 12 AND
not_existing = 1
ORDER BY
type_of_work ASC,
project_version ASC
)
but what this piece of code does, is putting the not existing dech_rap_bus at the end, good, but it messes up the version sorting, WHY???
SEE RESULT HERE => http://sqlfiddle.com/#!2/b2a858/8
Why is that? I just want to MERGE two select results, what i am doing wrong?

Doesn't this give you what you want?
http://sqlfiddle.com/#!2/b2a858/26
Just sort by not_existing first?
What's the problem here? You have your sort but with not_existing records at the end - these are also sorted in the same way

If you do
order by
(case when not_existing is null then 0 else not_existing end) desc
,type_of_work ASC,
project_version ASC
It will come first.
your query is not ordering because you have different project value for dech_rap_bus
TYPE_OF_WORK PROJECT_VERSION
dech_rap_bus V123-V1234
dech_rap_bus V300

You don't need UNION you can achieve this with this query:
SELECT *
FROM atm_mti_view
WHERE project_function='FRS01' AND
on_big_project_id = 12
ORDER BY IF(not_existing = 1, 1, 0) ASC,
type_of_work ASC,
project_version ASC;

Related

Order by value decrement to one + mysql

Ex : I have V001, V002, V003, V004 records,
tid is_premium
---- ----------
V001 0
V002 0
V003 0
V004 1
V005 1
How to get records order by below like this,
V001
V004
V005
V002
V003
2nd Ex : I have V006, V007, V008, V009 records,
tid is_premium
---- ----------
V006 0
V007 0
V008 1
V009 0
How to get records order by below like this,
V006
V008
V007
V009
I want to above order in MySQL, I have to write but not possible, I tried both multiple column order by using Mysql, but I am not getting correct response. Can anyone help in this., I want to above order in MySQL,
Well, what this query does is to sort it in descending order by is_premium and then displays it simply that way
SELECT * FROM CLIENTE ORDER BY `tid`='V001' DESC, `is_premium` DESC;
Note : Where it says table put your table
I put a link with the test code for you to try it. : https://www.db-fiddle.com/f/3PnzHErrf2fZFGZY67K12X/57
Probably, #Chinnu the most efficient way to do what you need is get all from your DB and do some logic in your backend instead use mysql-query. Once you have an object with everything, you can apply conditions that will make sure if some patient is already in the queue and if that patient is premium or not and so on.
(SELECT * FROM PATIENTS WHERE `is_serial` = 0 LIMIT 1)
UNION
(SELECT * FROM PATIENTS ORDER BY `is_serial` = 0 DESC, `is_premium` DESC LIMIT 0, 1000);
Test here: https://www.db-fiddle.com/f/3PnzHErrf2fZFGZY67K12X/93
Picture with is_premium=1 first
Picture with is_premium=0 first
That should resolve your problem but it will put all is_premium = 1 before the others keeping the first row intact.
For patients with checkup completed too.
If you want to select only unchecked patients:
(SELECT * FROM PATIENTS WHERE `is_serial` = 0 LIMIT 1)
UNION
(SELECT * FROM PATIENTS WHERE `is_serial` = 0 ORDER BY `is_premium` DESC LIMIT 0, 1000);

MySQL ORDER BY CASE + operator

I am trying to do a search that would be sorted by relevance.
Let's say the search term contains 3 words: A, B and C. What I am trying to do is to check if the search term is present in the SELECT result and if yes that would increase its rank.
ORDER BY CASE
(
WHEN search_word_A_is_present THEN +1
WHEN search_word_B_is_present THEN +1
WHEN search_word_C_is_present THEN +1
ELSE 0
END
)
DESC
While there is no syntax error and the search runs and sorts by something (that seems different from what I want) but I am not sure what is being added up if anything. How would I go about seeing what the final rank (sum) is at the end for each result? Is this the correct way to do it?
Since in MySQL boolean conditions result in 1 and 0, you can simply add those up
ORDER BY search_word_A_is_present + search_word_B_is_present + search_word_C_is_present
DESC
A more practical example:
ORDER BY col1 = 1 + col2 = 'A' + col3 = 44 DESC

Order by date and field

I have SELECT query with LEFT JOINT (SELECT) and order by COALESCE(SELECT2.date, SELECT1.date) but i need to order it too with important field.
So: if row have important = 1 it need to be first, then important = 0 but ordered by coleasce date so as so. Is this even posible?
You could place the important column first in the order by list. Sorting desc (for descending) will place 1 before 0:
order by
important DESC
, coalesce(select2.date, select1.date)
You can even use case to sort the important column in a custom way:
order by
case
when important = 'SENATOR' then 1
when important = 'PATRICIAN' then 2
when important = 'PLEBS' then 3
else 4
end
, coalesce(select2.date, select1.date)

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10

MySQL How To Use Main Select Value As Subquery Argument?

I am trying to construct a query that is a bit more complicated than anything I've done in my limited experience with databases.
TABLE:
id - data - type - data3
1 - hello - 1 - 1
2 - goodbye - 1 - 1
3 - goodbye - 1 - 2
4 - goodbye - 2 - 1
5 - hello - 2 - 1
The goal is to do 4 things:
GROUP the results by "data", but only return one result/row of each
data type.
COUNT the total number of each "data GROUP and return this number.
Do this for both "type"=1 and "type"=2, though I only need each
"data" GROUP item once.
the ability to sort results based on each SELECT item.
So the final result returned should be (sorry to be confusing!):
data, COUNT(data["type"]=1), COUNT(data["type"]=2 AND data["data"] = data)
So, for the sample table above, desired results would be:
loop 1 - hello, 2, 1
loop 2 - goodbye, 3, 1
Then, ideally, I could sort results by any of these.
This is the query I was trying to construct before resorting to posting this, I don't think it's even close to being correct, but it may help illustrate what I'm trying to achieve a bit better:
SELECT
(
SELECT `clicks_network_subid_data`, COUNT(*)
FROM track_clicks
WHERE `clicks_campaign_id`='$id' AND `clicks_click_type` = '1'
) AS keywords,
(
SELECT COUNT(*)
FROM track_clicks
WHERE `clicks_campaign_id`='$id' AND `clicks_click_type` = '2' AND `clicks_network_subid_data` = keywords.clicks_network_subid_data
) AS offer_clicks
GROUP BY keywords.clicks_network_subid_data
ORDER BY keywords.COUNT(*) DESC
I also need to do a JOIN on another table to grab one more piece of data, but I think I can handle that once I get this part figured out.
You can use an IF-function for this
SELECT `clicks_network_subid_data`,
SUM(IF(clicks_click_type` == '1',1,0)) as keywords,
SUM(IF(clicks_click_type` == '2',1,0)) as offer_clicks,
FROM track_clicks
GROUP BY clicks_network_subid_data
ORDER BY clicks_network_subid_data DESC
You can do this using GROUP BY:
SELECT data, COUNT(*) AS cnt FROM `table` GROUP BY type ORDER BY COUNT(*)
Ordering might become a little slow, as this is a calculated field, but if you don't have a large result set then you are good to go.
First of all your question is bit not clear , However, check this query . what I suspect is that you need count results in columns (single ) instead of rows .
select * , count(type_one) as t1_count , count(type_two) as t2_count from (
select data,if(tmp.type=1,1,0) as type_one, if(tmp.type=2,1,0) as type_two from (
select 1 as id , 'hello' as data , 1 as type , 1 as data3 union
select 2 as id , 'goodbye' as data , 1 as type , 1 as data3 union
select 3 as id , 'goodbye' as data , 1 as type , 2 as data3 union
select 4 as id , 'goodbye' as data , 2 as type , 1 as data3 union
select 5 as id , 'hello' as data , 2 as type , 1 as data3
) tmp
) tmp2
group by tmp2.type_one ;
let me know if this works for you
cheers :)