Could i calc for each row column Profit ?
SELECT
SUM(CASE WHEN m.billable = 0 THEN r.rate ELSE 0 END) AS Revenue, -- 33 + 34 + 456 + 52...etc = 5500
SUM(CASE WHEN m.billable = 1 AND m.optimized = 0 THEN r.rate ELSE 0 END) AS Costs,-- 33 + 4...etc = 339
5500 - 339 AS Profit -- I need to get this difference
FROM messages AS m
JOIN rates AS r ON (r.id = m.rate_id )
GROUP BY
m.client_account_id,
m.mcc,
m.mnc
I want to get
| Revenue | Costs | Profit
5500 500 5000
Move your query into a subquery, then subtract the results.
SELECT Revenue, Costs, Revenue - Costs AS Profit
FROM (
SELECT
SUM(CASE WHEN m.billable = 0 THEN r.rate ELSE 0 END) AS Revenue,
SUM(CASE WHEN m.billable = 1 AND m.optimized = 0 THEN r.rate ELSE 0 END) AS Costs
FROM YourTable) AS x
Joining your two tables, and showing the results grouped by account, it would be:
SELECT client_account_id, mmc, mnc, Revenue, Costs, Revenue - Costs AS Profit
FROM (
SELECT
m.client_account_id, m.mmc, m.mnc
SUM(CASE WHEN m.billable = 0 THEN r.rate ELSE 0 END) AS Revenue,
SUM(CASE WHEN m.billable = 1 AND m.optimized = 0 THEN r.rate ELSE 0 END) AS Costs
FROM messages AS m
JOIN rates AS r ON r.id = m.rate_id
GROUP BY m.client_account_id, m.mmc, m.mnc
) AS x
Simply put it in a sub query :
SELECT Revenue - Costs as Profit
FROM (
SELECT
SUM(CASE WHEN m.billable = 0 THEN r.rate ELSE 0 END) AS Revenue,
SUM(CASE WHEN m.billable = 1 AND m.optimized = 0 THEN r.rate ELSE 0 END) AS Costs
) as temp
Related
I have to run two queries in my code to get my tenants balance. However, these queries are too slow.
First query, I get all the tenants and it's unit name:
SELECT t.TenantID
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),
REPEAT('0', (10-CHAR_LENGTH(UnitName))),
Right(Replace(UnitName,'-',''),
CHAR_LENGTH(Replace(UnitName,'-',''))-2
) )
It returns 500 rows
Then I get the balances in 4 conditions. This query will be inside of first query loop:
Select
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Where TenantID= FirstQuery.TenantID
Am I doing the queries wrong? It's taking like 1 minute to run.
Do this in a single query with GROUP BY.
Try something like this:
SELECT t.TenantID, TotalDebit, HousingDebit, TotalCredit, HousingCredit
FROM Tenants t
JOIN Units u ON t.UnitID = u.UnitID
LEFT JOIN (
Select
TenantID,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Group By TenantID
) sums ON sums.TenantID = t.TenantID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))
The inner query may still run for a while but it will only run once.
Try a compound covering index on TenantTransactions containing these columns: (TenantID, TransactionTypeID, ChargeTypeID, TransactionAmount) to optimize the query with the SUMs in it.
Try a compound index on Tenants with the columns (PropertyID, Prospect) in it.
Here's another way to do it with a subquery. You know, the performance problem might not be database performance, but the back and forth between your database and application server. So that is where a single query will help.
SELECT t.TenantID,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalCredit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingCredit
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))
I have this query for calculating sums of values per every year based on a date.
It works, but it is heavy, takes a minute to 2 minutes running on about 10k records
Is there a way to optimize this, or write it in a more efficient way?
"select departments sum(case when year(employment_date) = '1990' then 1 else 0 end) as '1990',"
+ "sum(case when year(employment_date) = '2010' then 1 else 0 end) as '2010',"
+ "sum(case when year(employment_date) = '2011' then 1 else 0 end) as '2011',"
+ "sum(case when year(employment_date) = '2012' then 1 else 0 end) as '2012',"
+ "sum(case when year(employment_date) = '2013' then 1 else 0 end) as '2013',"
+ "sum(case when year(employment_date) = '2014' then 1 else 0 end) as '2014',"
+ "sum(case when year(employment_date) = '2015' then 1 else 0 end) as '2015',"
+ "sum(case when year(employment_date) = '2016' then 1 else 0 end) as '2016',"
+ " count(departments.dept_id) as Total "
+ "from employees inner join departments on employees.employee_id=departments.employee_id AND departments.dept_id = ?";
sample resuts
|departments | Total | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 |
|Data systems | 100 | 30 | 10 | 5 | 15 | 20 | 12 | 8 |
|Social ssmp | 70 | 10 | 10 | 15 | 15 | 4 | 6 | 10 |
In mysql, the one of best way to improve the query performance is indexing.The whole point of having an index is to speed up search queries by essentially cutting down the number of records/rows in a table that need to be examined.
CREATE INDEX Emp_index ON Employee (Employment_Date, Employee_Id);
CREATE INDEX Dept_index ON Departments(Departments , Dept_Id );
Please refer link for more info.
Just a quick suggestion.. As indexing costs you additional writes and storage space, so if your application requires more insert/update operation, you might want to use tables without indexes, but if it requires more data retrieval operations, you should go for indexed table.
give this a shot and see if it's any faster.
select sum(case when employment_year = '1990' then employee_count else 0 end) as '1990',
sum(case when employment_year = '2010' then employee_count else 0 end) as '2010',
sum(case when employment_year = '2011' then employee_count else 0 end) as '2011',
sum(case when employment_year = '2012' then employee_count else 0 end) as '2012',
sum(case when employment_year = '2013' then employee_count else 0 end) as '2013',
sum(case when employment_year = '2014' then employee_count else 0 end) as '2014',
sum(case when employment_year = '2015' then employee_count else 0 end) as '2015',
sum(case when employment_year = '2016' then employee_count else 0 end) as '2016',
sum(employee_count) as Total
from
(select * from
(select count(*) as employee_count,year(employment_date) as employment_year
from employees inner join departments on employees.employee_id=departments.employee_id AND departments.dept_id = 1
group by year(employment_date)
)T1
where employment_year = 1990
or employment_year between 2010 and 2016
)T2;
sqlfiddle
Just change departments.dept_id = 1 to whatever dep_id you're looking for.
I have a table with payment:
worker_id, amount, payed, date
Table workers:
id, name, lname
I need to write SQL that will give me name, lname and sum for jun, july, august, september.
Name | Lname | Sum_JUN | Sum_JULY | Sum_AUG | Sum_SEP
I'm trying with subqueries but can't do it. Can you help me?
I created SQL (example). I will replace dates in PHP.
select w.name, w.lname,
sum(case when p.payed_date between '2014-06-01' and '2014-06-31' then p.amount else 0 end) `sum_june`,
sum(case when p.payed_date between '2014-07-01' and '2014-07-31' then p.amount else 0 end) `sum_july`,
sum(case when p.payed_date between '2014-08-01' and '2014-08-31' then p.amount else 0 end) `sum_august`,
sum(case when p.payed_date between '2014-09-01' and '2014-09-31' then p.amount else 0 end) `sum_september`,
sum(case when p.payed_date between '2014-10-01' and '2014-10-31' then p.amount else 0 end) `sum_november`
from worker w
left join worker_sum p on(w.id = p.worker_id)
group by w.id
You can use conditional aggregation for your desired sum,But this will give you the sum for months from all years exist in your table
select w.*,
sum(case when month(p.date) = 6 then p.amount else 0 end) `sum_june`,
sum(case when month(p.date) = 7 then p.amount else 0 end) `sum_july`,
sum(case when month(p.date) = 8 then p.amount else 0 end) `sum_august`,
sum(case when month(p.date) = 9 then p.amount else 0 end) `sum_september`
from workers w
left join payment p on(w.id = p.worker_id)
group by w.id
I need to tabulate the data from three tables for a report. Please see this query
SELECT
DATE( sale.sale_time ) AS dat,
location.location_name as location,
sale.sale_id AS total_orders,
SUM( CASE sale.is_cancelled WHEN 0 THEN sale.sale_amount ELSE 0 END ) AS sales,
SUM( CASE sale.is_cancelled WHEN 0 THEN sale.sale_discount ELSE 0 END ) AS discounts,
SUM( CASE sale.is_cancelled WHEN 0 THEN sale.sale_tax ELSE 0 END ) AS taxes,
COUNT( DISTINCT sale.customer_id ) AS total_customers,
SUM( CASE WHEN DATE( person.added_on ) = DATE( sale.sale_time ) THEN 1 ELSE 0 END ) AS new_customers,
SUM( CASE sale.is_cancelled WHEN 1 THEN 1 ELSE 0 END ) AS cancelled_orders
FROM
sales AS sale
INNER JOIN locations AS location ON location.location_id = sale.location_id
INNER JOIN people AS person ON person.person_id = sale.customer_id
GROUP BY
dat,location
The results for new_customers is showing wrong, often more than total_customers. Where is it wrong, and how to correct this?
The results are like this:
dat location total_orders sales discounts taxes new_customers total_customers cancelled_orders
15-03-14 Location1 52 1355 0 129.04 4 2 0
16-03-14 Location1 56 280 0 30 2 1 0
16-03-14 Location2 59 2518 0 212.2 3 6 2
As you might have guessed from the query, sales table has the columns sale_id,sale_time,sale_cost,sale_discount,sale_tax,sale_amount, is_cancelled (tinyint with values 0 or 1), and customer_id
People table has the columns person_id, first_name, added_on
By comparing the date(salessale_time) to date(person.added_on), I want to list customers added on that date
I modified the query to the following to get new customers also in the result set.
SELECT DATE(sale.sale_time) AS dat, location.location_name as location, (sale.sale_id) AS total_orders,
SUM(CASE sale.is_cancelled WHEN 0 THEN sale.sale_amount ELSE 0 END) AS sales,
SUM(CASE sale.is_cancelled WHEN 0 THEN sale.sale_discount ELSE 0 END) AS discounts,
SUM(CASE sale.is_cancelled WHEN 0 THEN sale.sale_tax ELSE 0 END) AS taxes,
count(distinct(sale.customer_id)) AS total_customers,
(select count(person_id) from people where date(added_on) = date(sale.sale_time) and person_id = sale.customer_id) as new_customers,
SUM(CASE sale.is_cancelled WHEN 1 THEN 1 ELSE 0 END) AS cancelled_orders
FROM sales AS sale
INNER JOIN locations AS location ON location.location_id = sale.location_id
GROUP BY dat,location;
how can I achieve the desired result in mysql if my table looks like this.
result|year
1 |2011
2 |2011
1 |2011
0 |2011
1 |2012
2 |2012
1 = Won, 2 = lost, 0 = draw
Every year can have multiple values like this. Not sure how I can get the desired result like below.
year won lost draw totalPlayed
2011 2 1 1 3
2012 1 1 0 2
I have tried the following query but does not get the desired result
select year,
league_types.league_name,
sum(if(result = 1,1,0)) as won,
sum(if(result = 0,1,0)) as draw,
sum(if(result = 4,1,0)) as noResult,
sum(if(result = 2,1,0)) as lost,
sum(if(result = 3,1,0)) as tied,
sum(if(result > 0 and result < 4,1,0)) as played
from match_score_card
inner join fixtures on match_score_card.match_id = fixtures.match_id
inner join league_types on fixtures.league_id = league_types.league_id
where
team_id = 1 group by year order by year desc
Here is the SQL Fiddle that demonstrates the following query:
SELECT m.year,
SUM(CASE WHEN m.result = 1 THEN 1 ELSE 0 END) AS 'Won',
SUM(CASE WHEN m.result = 2 THEN 1 ELSE 0 END) AS 'Lost',
SUM(CASE WHEN m.result = 0 THEN 1 ELSE 0 END) AS 'Draw',
COUNT(*) AS 'TotalPlayed'
FROM MyTable AS m
GROUP BY m.year
I'm not familiar with that IF function in mySQL, but this standard SQL should work:
select year
, league_types.league_name
, sum(CASE WHEN result = 1 THEN 1 ELSE 0 END) as won
, sum(CASE WHEN result = 2 THEN 1 ELSE 0 END) as lost
, sum(CASE WHEN result = 3 THEN 1 ELSE 0 END) as draw
, sum(CASE WHEN result = 4 THEN 1 ELSE 0 END) as noResult
, sum(CASE WHEN result = 1
or result = 2 THEN 1 ELSE 0 END) as played
from match_score_card
inner join fixtures
on match_score_card.match_id = fixtures.match_id
inner join league_types
on fixtures.league_id = league_types.league_id
where team_id = 1
group by year, league_types.league_name
order by year desc, league_types.league_name
I'm guessing that you only want to count wins and losses as "played".