I have a table which manage a status data: status could be 1, 2 or 3 .
I need to order the SELECT QUERY, first by 2 then 3 then 1 .... or any order what i want.
You can use mysql's FIELD() function for custom ordering
SELECT * FROM table
ORDER BY FIELD(`status`,2,3,1)
or you could use CASE
SELECT * FROM table
ORDER BY
(CASE WHEN `status` =2 THEN 1
WHEN `status` =3 THEN 2
WHEN `status` =1 THEN 3
ELSE END) ASC
FIELD(str,str1,str2,str3,...)
Related
I have a mysql query where I am trying to fetch DISTINCT elements:
SELECT DISTINCT(`Prev Date`), `Current Date`
FROM `electric_transaction`
WHERE `Flag`=1
AND `ipas_flag`=1
AND `SlNo` != (
SELECT MAX(`SlNo`) FROM `electric_transaction`
);
I want to fetch all the rows except the last one.
How do I proceed? Please guide me.
You could use an offset of 1 record in your LIMIT clause
SELECT DISTINCT `Prev Date`,`Current Date`
FROM `electric_transaction`
WHERE `Flag` = 1
AND `ipas_flag` = 1
ORDER BY SlNo DESC
LIMIT 1, 1000000
SELECT max(sum(`orderquantity`)), `medicinename`
FROM `orerdetails`
WHERE `OID`=
(
SELECT `OrderID`
FROM `order`
where `VID` = 5 AND `OrerResponse` = 1
)
GROUP BY `medicinename`
i want to get the max of the result(sum of the order quantity) but it gives error any soultion to solve this
You don't need Max() here. Instead sort your recordset by that Sum('orderquantity') descending, and take the first record returned:
SELECT sum(`orderquantity`) as sumoforderqty, `medicinename`
FROM `orerdetails`
WHERE `OID`=
(
SELECT `OrderID`
FROM `order`
where `VID` = 5 AND `OrerResponse` = 1
)
GROUP BY `medicinename`
ORDER BY sumoforderqty DESC
LIMIT 1
Here is my table
Which have field type which means 1 is for income and 2 is for expense
Now requirement is for example in table there is two transaction made on 2-10-2018 so i want data as following
Expected Output
id created_date total_amount
1 1-10-18 10
2 2-10-18 20(It calculates all only income transaction made on 2nd date)
3 3-10-18 10
and so on...
it will return an new field which contains only incom transaction made on perticulur day
What i had try is
SELECT * FROM `transaction`WHERE type = 1 ORDER BY created_date ASC
UNION
SELECT()
//But it wont work
SELECT created_date,amount,status FROM
(
SELECT COUNT(amount) AS totalTrans FROM transaction WHERE created_date = created_date
) x
transaction
You can Also See Schema HERE http://sqlfiddle.com/#!9/6983b9
You can Count() the total number of expense transactions using conditional function If(), on a group of created_date.
Similarly, you can Sum() the amount of expense done using If(), on a created_date.
Try the following:
SELECT
`created_date`,
SUM(IF (`type` = 2, `amount`, 0)) AS total_expense_amount,
COUNT(IF (`type` = 2, `id`, NULL)) AS expense_count
FROM
`transaction`
GROUP BY `created_date`
ORDER BY `created_date` ASC
Do you just want a WHERE clause?
SELECT t.created_date, SUM(amount) as total_amount
FROM transaction t
WHERE type = 2
GROUP BY t.created_date
ORDER BY created_date ASC ;
I want to sum 4 cells for each row based on a previous query that will reduce the selection down to the important rows.
Basically I need to combine those two queries (which work on their own):
SELECT columnx, SUM(`column1`+ `column2` + `column3` + `column4`) as total
FROM table GROUP BY columnx
SELECT * FROM (SELECT * FROM table ORDER BY columny DESC LIMIT 5) t
ORDER BY CASE
when `pos` = 'PG' then 1
when `pos` = 'SG' then 2
when `pos` = 'SF' then 3
when `pos` = 'PF' then 4
else 5
end asc
I tried to replace "table" with the second query but it's probably not the right way, since I'm getting errors here.
SELECT columnx, SUM(`column1`+ `column2` + `column3` + `column4`) as total FROM
(( SELECT * FROM (SELECT * FROM table ORDER BY columny DESC LIMIT 5) t
ORDER BY CASE
when `pos` = 'PG' then 1
when `pos` = 'SG' then 2
when `pos` = 'SF' then 3
when `pos` = 'PF' then 4
else 5
end asc)
GROUP BY columnx
You were supposed to create an alias name for the block replacing table in your first query.
SELECT u.columnx, SUM(u.`column1`+ u.`column2` + u.`column3` + u.`column4`) as total
FROM (SELECT t.* FROM (SELECT * FROM table ORDER BY columny DESC LIMIT 5) t
ORDER BY CASE
WHEN t.`pos`='PG' THEN 1
WHEN t.`pos`='SG' THEN 2
WHEN t.`pos`='SF' THEN 3
WHEN t.`pos`='PF' THEN 4
ELSE 5
END ASC) u GROUP BY u.columnx
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='7' ORDER BY `counter` DESC LIMIT 75
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='4' ORDER BY `counter` DESC LIMIT 75
I want to see 2 avg easy to compare in my phpmyadmin. Singe line above will work but i can't combine them :/
Use conditional aggregation if you want the results in two columns:
SELECT AVG(CASE WHEN id = 7 THEN tmp END) as avg_7,
AVG(CASE WHEN id = 4 THEN tmp END) as avg_4
FROM monitor
WHERE id IN (4, 7);
The ORDER BY and LIMIT` are non-sensical in your question because the queries return only one row.
If you prefer two rows, then you have several choices. I think I would go with aggregation:
SELECT id, AVG(tmp) as average
FROM monitor
WHERE id IN (4, 7)
GROUP BY id;
Try:
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='7' ORDER BY `counter` DESC LIMIT 75
UNION
SELECT AVG(`tmp`) FROM `monitor` WHERE `id`='4' ORDER BY `counter` DESC LIMIT 75;