How to remove decimal digit from result in mysql? - mysql

I want to remove digit after decimal how to solve it?
My query is:
SELECT city_name,
Assignedto,
COUNT(Assignedto) AS TC,
CONCAT(count(CASE
WHEN STATUS = 'CLOSED' THEN 1
ELSE NULL
END) * 100 / count(1), '%') AS SC,
CONCAT(count(CASE
WHEN STATUS = 'PENDING' THEN 1
ELSE NULL
END) * 100 / count(1), '%') AS PC,
SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 AS WH,
(154440-sum(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60) AS VH,
CONCAT(COUNT(Feedback_Rate)/COUNT(Assignedto)*100,'%') AS Feed_Percent,
SUM(Feedback_Rate)/(count(Feedback_Rate)*5)*5 AS AVG_Feedback
FROM `it_service_ticket`
INNER JOIN `it_problem`ON `it_service_ticket`.`it_problem_id`=`it_problem`.`it_problem_id`
INNER JOIN `city_master` ON `it_service_ticket`.cityid=`city_master`.city_id
WHERE `it_service_ticket`.`xetr` BETWEEN '2016-04-01 12:00:00 AM' AND '2017-02-28 12:00:00 PM'
GROUP BY Assignedto
ORDER BY city_name ASC;
Output
+-------------------------+-------------------------+-------+------------+----------+------------+--------------+-----------+---------+
| City_Name | AssigneeTo | TC | SC | PC | WH | VH | Feedback | Average |
+-------------------------+-------------------------+-------+------------+----------+------------+--------------+-----------+---------+
| Ahmedabad | mahesh.patel#corp.in | 297 | 100.0000% | 0.0000% | 147.0667 | 154292.9333 | 43.4343% | 4.4031 |
| Ahmedabad | mahesh.patel#corp.in | 297 | 100.0000% | 0.0000% | 147.0667 | 154292.9333 | 43.4343% | 4.4031 |

If you want to round off decimal places, use ROUND(yourColumn,0) function.
So 13.78 will become 14
If you want to get rid of decimal places, user FLOOR(yourColumn)
So 13.78 will become 13
So for example
SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60 AS WH
should be changed to
ROUND(SUM(TIMESTAMPDIFF(MINUTE,Request_Date, Xetr))/60,0) AS WH
Edit: This would take care of your %.
CONCAT(
ROUND(count(CASE
WHEN STATUS = 'PENDING' THEN 1
ELSE NULL
END) * 100 / count(1),0)
, '%') AS PC
Do the same for all the columns you need to remove decimal places.

You Should Use TRUNCATE() in Mysql for Example
SELECT TRUNCATE(525.668545, 3) -- 525.668

Related

How to display multiplication result with 2 decimal places

I am building a query which needs to output a multiplication result of two fields as a currency value. 0.00 - 9999.99.
I have attempted several suggested solutions and so far either they give syntax errors or will round to a whole number with no decimal
Do I need separate the multiplication from the formatted result?
My current query.
mysql> SELECT b.RowNumber,b.Email, z.PriceMultiplier * p.Price
-> FROM Production p JOIN Performace pe ON p.Title = pe.Title
-> JOIN Booking b ON b.PerfDate = pe.PerfDate AND b.Perftime = pe.PerfTime
-> JOIN Seat s ON s.RowNumber = b.RowNumber
-> JOIN Zone z ON z.Name = s.Zone;
+-----------+----------------------+-----------------------------+
| RowNumber | Email | z.PriceMultiplier * p.Price |
+-----------+----------------------+-----------------------------+
| Z16 | Jane.Dot#live.com | 60 |
| Z18 | ZP#email.com | 60 |
| Z19 | ZP#email.com | 60 |
| U19 | Jane.Dot#live.com | 26.999999284744263 |
| U20 | Jane.Dot#live.com | 26.999999284744263 |
| X13 | Mike.Stand#email.com | 26.999999284744263 |
| X14 | Mike.Stand#email.com | 26.999999284744263 |
| Z19 | qvf3#live.com | 60 |
+-----------+----------------------+-----------------------------+
Datatypes - PriceMultiplier FLOAT NOT NULL and Price DECIMAL(6,2) NOT NULL
The TRUNCATE command appears to be most recommended however it is rounding
26.999999284744263 - should become 26.99 , not 27
You need the function TRUNCATE():
TRUNCATE(p.BasicTicketPrice * z.PriceMultiplier, 2)
For example:
select truncate(26.999999284744263, 2)
returns
26.99
Use MySQL's Round function in your query - ROUND(number, decimals)
e.g. SELECT ProductName, Price, ROUND(Price, 1) AS RoundedPrice
FROM Products; - Taken from https://www.w3schools.com/sql/func_mysql_round.asp
For your particular query, try something like
SELECT b.RowNumber, b.Email, ROUND(p.BasicTicketPrice * z.PriceMultiplier,2) AS Price FROM Booking b
JOIN Performance pe ON pe.PerfDate = b.PerfDate AND b.PerfTime = pe.PerfTime
JOIN Production p ON pe.Title = p.Title JOIN Seat s ON b.RowNumber = s.RowNumber
JOIN Zone z ON s.Zone = z.Name WHERE s.Zone = z.Name;
Truncate also gives the answer you are after
SELECT Truncate(26.999999284744263, 2);
26.99
Good luck with your studies
After settings both data types to Float.
PriceMultiplier FLOAT NOT NULL and Price FLOAT NOT NULL
Truncate worked, without rounding.
U19 | Jane.Dot#live.com | 26.99 |

MYSQL: Tricky Query - need to build intermedia Variable?

let´s assume we have the following table
_________________________________________________________________________
|FlightNo | DepartureDate | Origin | Destination | Flightduration |
_________________________________________________________________________
| IB233 | 2018-12-31 09:30:45 | Berlin | Barcelona | 200 |
| IB222 | 2019-01-01 12:20:34 | Barcelona| Rome | 100 |
| LE111 | 2019-01-01 11:11:11 | Rome | NY | 400 |
__________________________________________________________________________
Now I want to calculate the timedifference between the Destination of NY and start in Berlin. I want to check whether the whole flightduration is < 660 min.
I worked out this, but it doesn´t work ..
SELECT
TIMEDIFF(
(SELECT adddate(DepartureDate, INTERVAL FlightDuration
MINUTE)
FROM flightexecution
where ICAO_Code_Origin = 'Berlin'),
(SELECT DepartureDate
FROM flightexecution
where ICAO_Code_Destination = 'NY' AND ICAO_Origin NOT LIKE 'Berlin' AND)
) from flightexecution ;
MySQL is giving me just an 'OK' as an result, which means that somehting is wrong...
Could you guys help me out?
You may try a self join query like this
SELECT
A.Origin,
(TIME_TO_SEC(TIME_DIFF(A.DepartureDate, C.DepartureDate))/60 - A.FlightDuration - B.FlightDuration) AS `Transfer time (minutes)`,
A.DepartureDate,
(TIME_TO_SEC(TIME_DIFF(A.DepartureDate, C.DepartureDate))/60 + C.FlightDuration) AS `Overall Duration (minutes)`
FROM
flightexecution A INNER JOIN
flightexecution B
ON A.Destination = B.Origin INNER JOIN
flightexecution C
ON B.Destination = C.Origin
WHERE
A.Origin = 'Berlin'
AND C.Destination = 'NY'

Combining sql select queries

I have 2 different queries, which both return results similar to below. For example
SELECT value
, date_format( statdate, "%Y-%m-%d" ) AS testdate
FROM stats
WHERE name LIKE 'myname1'
AND date >= "2014-09-20"
and date < "2014-09-24"
GROUP BY 2;
SELECT value
, date_format( statdate, "%Y-%m-%d" ) AS testdate
FROM stats
WHERE name LIKE 'myname2'
AND date >= "2014-09-20"
and date < "2014-09-24"
GROUP BY 2;
Information comes from the same table, but part of it is a different WHERE (with the date being the field which is the same in both).
Which both return information like..
value| testdate |
+--------+-------+
| 60 | 2014-09-20|
| 57 | 2014-09-21|
| 56 | 2014-09-22|
| 55 | 2014-09-23|
| 59 | 2014-09-24|
What I would like to do, is combine both sets of results into one (and then perform some maths like a ratio on the two numbers), so I want to end up with something like...
val1 | val2| ratio (val1/val2) | testdate |
+----+-------------------------|------------+
| 60 | 140 | 0.42 | 2014-09-20 |
| 57 | 180 | 0.31 | 2014-09-21 |
| 56 | 190 | 0.29 | 2014-09-22 |
| 55 | 10 | 5.5 | 2014-09-23 |
| 59 | 100 | 0.59 | 2014-09-24 |
I'm guessing I need a JOIN somewhere, but can't figure it, and how to use that value for some basic maths in there ?
You should use the "Self Join" to join the 2 instances of same table. like below
SELECT s1.value, date_format( s1.statdate, "%Y-%m-%d" ) AS testdate
s2.value, date_format( s2.statdate, "%Y-%m-%d" ) AS testdate2,
(s1.value/s2.value) as ration
FROM stats s1, stats s2
WHERE s1.id = s2.id
and s1.name LIKE 'myname1' AND s1.date >= "2014-09-20" and s1.date < "2014-09-24"
and s2.name LIKE 'myname2' AND s2.date >= "2014-09-20" and s2.date < "2014-09-24"
GROUP BY 2;
I would recommend conditional aggregation:
SELECT date_format(statdate, '%Y-%m-%d') AS testdate,
max(case when name = 'myname1' then value end) as val1,
max(case when name = 'myname2' then value end) as val2,
(max(case when name = 'myname1' then value end) /
max(case when name = 'myname2' then value end)
) as ratio,
FROM stats
WHERE name in ('myname1', 'myname2') AND date >= '2014-09-20' and date < '2014-09-24'
GROUP BY date_format(statdate, '%Y-%m-%d');

how to show virtual column with calculated runtime values(i.e not in database) in mysql?

I've following table stuinfo
+------------+-------+-----------+
| Roll_no | Name | Lastname |
+------------+-------+-----------+
| 37032 | johny | bravo |
| 37034 | sam | smith |
+------------+-------+-----------+
and second one stu_attendace
+------------+-------+-----------+
| Roll_no | Name | month |
+------------+-------+-----------+
| -1 | total | 26 |
| 37032 | johny | 19 |
| 37034 | sam | 25 |
+------------+-------+-----------+
Total days are 26 , so johny's attendance is 73.03% and Sam's attendance is 95.03% .
So how can I show their Attendance with percentage by calculating at run-time and showing those values in new column 'per_attendace' which is actually not in database. like shown in below
+----------+--------+-----------+---------------+
| roll_no | name | last_name | per_attendace |
+----------+--------+-----------+---------------+
| 37032 | johny | bravo | xx % |
| 37034 | sam | smith | xx % |
+----------+--------+-----------+---------------+
You can easily do what you want, in the SELECT clause you can select a literal value, or a column name, or any valid expression.
Just JOIN the two tables, and then calculate the percent like this:
SELECT
i.roll_no,
i.name,
i.Lastname,
((a.month / 26) * 100) AS percent
FROM stuinfo AS i
INNER JOIN stu_attendance AS a ON i.Roll_no = a.Roll_no;
Note that:
There is no need to duplicate the column name in both the two tables, just remove it from the other table, and keep your tables normalized.
Don't store the total value like this as a value in the column.
Update:
If you want to select the value of the total from the database, you can do this:
SELECT
i.roll_no,
i.name,
i.Lastname,
((a.month / (SELECT month
FROM stu_attendace
WHERE name = 'total')) * 100) AS percent
FROM stuinfo AS i
INNER JOIN stu_attendance AS a ON i.Roll_no = a.Roll_no;
SQL Fiddle Demo
You can also set that total value as a variable instead of the correlated subquery, like this:
SET #total = 0;
SELECT month
FROM stu_attendace
WHERE name = 'total'
INTO #total;
Then:
SELECT
i.roll_no,
i.name,
i.Lastname,
ROUND(((a.month / #total) * 100), 2) AS percent
FROM stuinfo AS i
INNER JOIN stu_attendace AS a ON i.Roll_no = a.Roll_no;
I used the ROUND function to round the number to only two decimal values.
Updated SQL Fiddle Demo
Try this
select a.roll_no , a.name ,a.last_name,(b.month/26)*100 as per_attendace
from stuinfo as a join
stu_attendace as b on a.roll_no=b.roll_no
try
SELECT i.roll_no, i.name, i.Lastname, ((a.month / 26) * 100) AS percent
FROM stuinfo AS i
INNER JOIN stu_attendance AS a ON i.Roll_no = a.Roll_no;

nested query & transaction

Update #1: query gives me syntax error on Left Join line (running the query within the left join independently works perfectly though)
SELECT b1.company_id, ((sum(b1.credit)-sum(b1.debit)) as 'Balance'
FROM MyTable b1
JOIN CustomerInfoTable c on c.id = b1.company_id
#Filter for Clients of particular brand, package and active status
where c.brand_id = 2 and c.status = 2 and c.package_id = 3
LEFT JOIN
(
SELECT b2.company_id, sum(b2.debit) as 'Current_Usage'
FROM MyTable b2
WHERE year(b2.timestamp) = '2012' and month(b2.timestamp) = '06'
GROUP BY b2.company_id
)
b3 on b3.company_id = b1.company_id
group by b1.company_id;
Original Post:
I keep track of debits and credits in the same table. The table has the following schema:
| company_id | timestamp | credit | debit |
| 10 | MAY-25 | 100 | 000 |
| 11 | MAY-25 | 000 | 054 |
| 10 | MAY-28 | 000 | 040 |
| 12 | JUN-01 | 100 | 000 |
| 10 | JUN-25 | 150 | 000 |
| 10 | JUN-25 | 000 | 025 |
As my result, I want to to see:
| Grouped by: company_id | Balance* | Current_Usage (in June) |
| 10 | 185 | 25 |
| 12 | 100 | 0 |
| 11 | -54 | 0 |
Balance: Calculated by (sum(credit) - sum(debits))* - timestamp does not matter
Current_Usage: Calculated by sum(debits) - but only for debits in JUN.
The problem: If I filter by JUN timestamp right away, it does not calculate the balance of all time but only the balance of any transactions in June.
How can I calculate the current usage by month but the balance on all transactions in the table. I have everything working, except that it filters only the JUN results into the current usage calculation in my code:
SELECT b.company_id, ((sum(b.credit)-sum(b.debit))/1024/1024/1024/1024) as 'BW_remaining', sum(b.debit/1024/1024/1024/1024/28*30) as 'Usage_per_month'
FROM mytable b
#How to filter this only for the current_usage calculation?
WHERE month(a.timestamp) = 'JUN' and a.credit = 0
#Group by company in order to sum all entries for balance
group by b.company_id
order by b.balance desc;
what you will need here is a join with sub query which will filter based on month.
SELECT T1.company_id,
((sum(T1.credit)-sum(T1.debit))/1024/1024/1024/1024) as 'BW_remaining',
MAX(T3.DEBIT_PER_MONTH)
FROM MYTABLE T1
LEFT JOIN
(
SELECT T2.company_id, SUM(T2.debit) T3.DEBIT_PER_MONTH
FROM MYTABLE T2
WHERE month(T2.timestamp) = 'JUN'
GROUP BY T2.company_id
)
T3 ON T1.company_id-T3.company_id
GROUP BY T1.company_id
I havn't tested the query. The point here i am trying to make is how you can join your existing query to get usage per month.
alright, thanks to #Kshitij I got it working. In case somebody else is running into the same issue, this is how I solved it:
SELECT b1.company_id, ((sum(b1.credit)-sum(b1.debit)) as 'Balance',
(
SELECT sum(b2.debit)
FROM MYTABLE b2
WHERE b2.company_id = b1.company_id and year(b2.timestamp) = '2012' and month(b2.timestamp) = '06'
GROUP BY b2.company_id
) AS 'Usage_June'
FROM MYTABLE b1
#Group by company in order to add sum of all zones the company is using
group by b1.company_id
order by Usage_June desc;