Order by value decrement to one + mysql - 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);

Related

Mysql limit when the whole list may change

I have a query that returns 10 items of a big list. For example:
SELECT * FROM users
WHERE user_is_block = 0
AND user_is_paid = 1
ORDER BY user_post_hour ASC, id ASC"
When I put limit ?, ? to get a part of this list each time, a problem happens. Imagine we get the first 10. The row with id 11, is the first one in the next request. But as we use ORDER BY user_post_hour, the id 11 may go up and become 10. So when second request is sent, we never see id 11.
Any idea?
It might be the problem with datatype soring, again cast your column value to an integer explicitly
SELECT * FROM users
WHERE user_is_block = 0
AND user_is_paid = 1
ORDER BY cast(user_post_hour as unsigned) ASC, id ASC
Use this trick.
SELECT * FROM users
WHERE user_is_block = 0
AND user_is_paid = 1
ORDER BY user_post_hour+0 ASC, id ASC

How to get time value of MAX(speed) in MySQL?

I have a table with the following structure:
Id | Speed | Time
I want to get the time of the row that contains the maximum speed.
My example query doesn't return the correct record.
SELECT MAX(speed), time FROM info WHERE id = 1 AND time > 1234
You could order the result and take the first record:
SELECT speed, time
FROM info
WHERE id = 1 AND time > 1234
ORDER BY speed DESC
LIMIT 1
SELECT MAX(speed) as speed, time FROM info WHERE id = 1 AND time > 1234 ORDER BY id
Probably you try to get the column bad in the fetch. So try giving an alias an using it as the column name.
Or try it as getting max as a condition value in a subquery:
SELECT * FROM info WHERE id = 1 AND speed = (SELECT MAX(speed) FROM info) AND time>1234;
select id, speed, time
from info
where speed = (select max(speed)
from info);

MYSQL Select single row by multiple parameters returning unexpected result

I have a table offers with five relevant columns, offer_id, offer_type, offer_amount, end_date and offer_status
I am trying to select the row with the highest offer_amount that matches the correct offer_type and an offer_status of 1 (active).
The query I am using is
SELECT * FROM offers_tbl WHERE offer_status = 1 AND offer_type = 'site-wide' AND offer_amount = (SELECT MAX(offer_amount) FROM offers_tbl )
Whats happening is that this is not returning any results if the MAX(offer_amount) happens to have a offer_status of 0 (inactive).
If the particular row with the highest MAX(offer_amount) happens to have an offer_status of 1, this works fine. Its only when the offer status is 0 that this breaks.
Try making it find MAX out of active ones:
SELECT * FROM offers_tbl WHERE offer_status = 1 AND offer_type = 'site-wide' AND offer_amount = (SELECT MAX(offer_amount) FROM offers_tbl WHERE offer_status = 1)
Otherwise, it's finding MAX offer amounts that could have a status of 0, which makes the outside query not find a match of status of 1 AND that amount.

SQL Sum() - 0 in field excluding row from aggregation?

Consider the following query:
$query = "
SELECT a_orders.id, a_orders.billing,
SUM(a_order_rows.quant_refunded*a_order_rows.price*((100-a_orders.discount)*.01)) as refund_total,
SUM(a_order_rows.quant*a_order_rows.price*((100-a_orders.discount)*.01)) as order_total
FROM a_order_rows JOIN a_orders
ON a_order_rows.order_id = a_orders.id
WHERE a_order_rows.quant_refunded > 0
GROUP BY a_orders.id, a_orders.billing
ORDER BY a_orders.id DESC
LIMIT 50";
The two uses of SUM() are attempting to aggregate the order total and the total amount that has been refunded. They are coming out correctly when when the quant_refunded field is not 0... for example take this data (excluding primary key, item_id for simplicity's sake, assuming each row is a unique item):
Table: a_order_rows
Fields: order_id, quant, quant_refunded
1, 1, 1
1, 2, 1
2, 1, 1
2, 1, 0
In the case of "order 1" the two aggregations are different and behave as expected. But for "order 2" the two numbers are coming out the same - both numbers are what I am expecting refund_total to be. It appears that the row with "0" in quant_refunded is being excluded from the order_total aggregation.
Hopefully this is explained thoroughly enough. Please let me know if you need more info and I will revise. THANKS!
$query = "
SELECT a_orders.id, a_orders.billing,
SUM(a_order_rows.quant_refunded*a_order_rows.price*((100-a_orders.discount)*.01)) as refund_total,
SUM(a_order_rows.quant*a_order_rows.price*((100-a_orders.discount)*.01)) as order_total
FROM a_order_rows JOIN a_orders
ON a_order_rows.order_id = a_orders.id
GROUP BY a_orders.id, a_orders.billing
HAVING MAX(a_order_rows.quant_refunded) > 0
ORDER BY a_orders.id DESC
LIMIT 50";
Change that to a HAVING clause. If any quant_refunded rows are > 0, the HAVING MAX will retain it.

MySQL sorting by 3 columns or UNION them

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;