MySQL SUM() Query of decimal value calculates wrong - mysql

I'm making a query to calculate the summation of total and amount columns but with the calculation of raw-materials and growth profit, I get the raw-materials summation from the items table that has the price in it and calculate the growth profit by subtracting calculated raw-materials from the amount,
The problem comes when I run the query it gives me wrong values. like sum of amount
sum(`payment`.`amount`) as total amount
should be like 1425 but the result is 107100, I don't know why it does this in the image below I showed the results, that number 107100 it way more than it should be please I’m new someone help me
This is my query
select `payment`.`date` as `Date`,
sum(`payment`.`total`) as `Total`,
sum(`payment`.`amount`) as `Paid amount`,
sum(items.price) as `R.M`,
sum(`payment`.`amount`) - sum(items.price) AS `G.P`,
`currennt_exp`.`expence` as `Current Expenses`,
sum(`payment`.`amount`)-sum(items.price) - `currennt_exp`.`expence` AS `Revenue`
from `payment`,`paymen_information`,`bill_information`,`bills`,`items`,`currennt_exp`,`appointments`
where `payment`.`id_payment` = `paymen_information`.`id_payment` and `paymen_information`.`id_bill` = `bills`.`id_bill` and bill_information.bills_id = `bills`.`id_bill` and bill_information.item_id = `items`.`id_item` and
date_format(`payment`.`date`,'%Y-%m-%d') = date_format(`currennt_exp`.`_date`,'%Y-%m-%d') and
date_format(`payment`.`date`, '%Y-%m-%d') = CURDATE()
and this is my table
total amount
135.0000 135.0000
165.0000 165.0000
375.0000 375.0000
300.0000 300.0000
450.0000 450.0000
and this is the result and its very wrong

Related

4.99 showing up as 499 - Converting BIGINT into Decimals

The problem I am having is that my output is not being displayed correctly. For example, what should display 4.99 shows as 499. The amounts in my Stripe database are stored as BIGINT. I've tried using CAST but the decimal is input at the end of my values with trailing 00s and that is not what I want.
What I've tried:
SELECT COALESCE(card_address_country, card_country) AS "Country",
SUM(CAST(amount AS DECIMAL(19,2))) AS "Total Revenue"
FROM charges
WHERE amount_refunded = 0 and paid = true
GROUP BY COALESCE(card_address_country, card_country)
ORDER BY SUM(amount) DESC
$293,839,799 should be $2,938,397.99 and $499 = $4.99.
I've tried dividing the amounts by 100 but it results in a round whole number and the sum is no longer calculated correctly.
SELECT COALESCE(card_address_country, card_country) AS "Country",
SUM(amount/100) AS "Total Revenue"
FROM charges
WHERE amount_refunded = 0 and paid = true
GROUP BY COALESCE(card_address_country, card_country)
ORDER BY SUM(amount) DESC
Is what I am trying to achieve possible? Or am I just using the wrong functions? I was thinking it could be the way the database has been setup as well.

SQL Server Report Builder - Expression

My condition is that industry who has paying highest time amount and total receivable amount of that industry
My expected result is ABC industry is highest number of count and total receive amount for that is 3584
I am trying find SSRS expression for above query but I couldn't. please help me
It will probably be better to do this in your dataset query, something like this.
DECLARE #t TABLE(IndustryType Varchar(3), Amount int)
INSERT INTO #t VALUES
('ABC', 500),('XYZ', 304),('KLM', 683),('ABC', 529),('ERJ', 703),
('XYZ', 902),('ABC', 852),('KLM', 950),('ABC', 703),('ABC', 1000)
SELECT TOP 1 *
FROM(
SELECT IndustryType, COUNT(*) as cnt, SUM(Amount) as TotalAmount
FROM #t
GROUP BY IndustryType
) x
ORDER BY x.cnt DESC
This gives the following results
If you cannot do this for whatever reason, edit your question and show what the final output should look like.

What is difference for below there two sql queries

select
substr(insert_date, 1, 14),
device, count(1)
from
abc.xyztable
where
insert_date >= DATE_SUB(NOW(), INTERVAL 10 DAY)
group by
device, substr(insert_date, 1, 14) ;
and then I am trying to get average of the same rows count which I got above.
SELECT
date, device, AVG(count)
FROM
(SELECT
substr(insert_date, 1, 14) AS date,
device,
COUNT(1) AS count
FROM
abc.xyztable
WHERE
insert_date >= DATE_SUB(NOW(), INTERVAL 10 DAY)
GROUP BY
device, substr(insert_date, 1, 14)) a
GROUP BY
device, date;
AS I found both queries return the same results, I tried for last 10 days data.
My purpose is to get the average rows count for last 10 days which I get from the above 1st query.
I'm not entirely sure what you're asking, the "difference" between the two queries is that the first one is valid but the second does not appear to be, as per HoneyBadger's comment. They also seem to be trying to achieve two different goals.
However, I think what you are trying to do is produce a query based on the data from the first query, which returns the date, device, and an average of the count column. If so, I believe the following query would calculate this:
WITH
dataset AS (
select substr(insert_date,1,14) AS theDate, device, count(*) AS
theCount
from abc.xyztable
where insert_date >=DATE_SUB(NOW(), INTERVAL 10 DAY)
group by device,substr(insert_date,1,14)
)
SELECT theDate, device, (SELECT ROUND(AVG(CAST(theCount
AS FLOAT)), 2) FROM
dataset) AS Average
FROM dataset
GROUP BY theDate, device
I have referenced the accepted answers of this question to calculate the average: How to calculate average of a column and then include it in a select query in oracle?
And this question to tidy up the query: Formatting Clear and readable SQL queries
Without having a sample of your data, or any proper context, I can't see how this would be especially useful, so if it was not what you were looking for, please edit your question and clarify exactly what you need.
EDIT: Based on what extra information you have provided, I've made a tweak to my solution to increase the precision of the average column. It now calculates the average to two decimal places. You have stated that this returns the same result as your original query, but the two queries are not formulating the same thing. If the count column is consistently the same number with little variation, the AVG function will round this, which in turn could produce results which look the same, especially if you only compare a small sample, so I have amended my answer to demonstrate this. Again, we'd all be able to help you much easier if you would provide more information, such as a sample of your data.
If you want an average you need to change the last GROUP BY
to get an average per device
GROUP BY device;
to get an average per date
GROUP BY date;
or remove it completely to get an average for all rows in the sub-query
Update
Below is a full example for getting the average per device
SELECT device, avg(count)
FROM (SELECT substr(insert_date,1,14) as date, device, count(1) as count
FROM abc.xyztable
WHERE insert_date >=DATE_SUB(NOW(), INTERVAL 10 DAY)
GROUP BY device,substr(insert_date,1,14)) a
GROUP BY device;

Running a cumulative Union in mysql

I am trying to get a list, a total, and a cumulative total in the same column. So far I have the list and the total for that date range, but moving forward I would like to get a cumulative total. Here is the code i'm using, please note temp_2 and temp_3 yield the same results;
Select School,Principal, NumberOfObs
From temp_2
Union
Select '','TOTAL',Sum(NumberOfObs)
From temp_3;
Any thoughts?

how to sum different values of time type in sql table

my query :
SELECT (
`total_hours`
)
FROM work_details
WHERE `employee_id` = '28'
AND DATE
BETWEEN '2012-02-01'
AND '2012-02-01'
LIMIT 0 , 30
// Which takes total hours worked for a given employee in given date from database table work_details
result:
total_hours
02:27:29
00:13:56
03:03:49
00:00:03
00:30:20
01:04:13
//result shows the times an employee worked in a given date,I need to get the total of these values to find total working hour in a given date.
how to get sum of these values in a field???
if i use sum(total_hours) result would be
sum( total_hours )
67870
and that is not correct.I require it in time type itself.
And i have one more help needed, this query worked because i gave same date for the BETWEEN clause.
DATE
BETWEEN '2012-02-01'
AND '2012-02-01'
But i need to calculate total working hours of days in a given range , say
DATE
BETWEEN '2012-02-01'
AND '2012-02-29'
Since a given date has multiple total hours entry , i need get total hours for each distinct day.Help??
try this (taken from here):
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`total_hours`))) ... -- rest of your query