I used the mysql query below.
Select MONTHNAME(t.date) as month, YEAR(t.date) as iyear, t.income,
t.subincome, t.ssubincome, sum(rupees) as amount,
COALESCE((SELECT
SUM(x.rupees)
FROM house_details x WHERE x.id < t.id), '-') AS progressive_total,
(SELECT SUM(x.rupees)
FROM house_details x WHERE x.id <= t.id
group by MONTH(t.date)) AS cumulative_sum
FROM house_details t
WHERE YEAR(t.date) = YEAR(CURRENT_DATE()) AND t.subincome = "Garbage tax"
GROUP BY MONTH(t.date) order by t.id
and got the value below:
month iyear income subincome amount progressive_total cumulative_sum
January 2017 Taxes Garbage tax 385 - 125
February 2017 Taxes Garbage tax 890 125 1015
March 2017 Taxes Garbage tax 200 1015 1215
April 2017 Taxes Garbage tax 250 1215 1465
May 2017 Taxes Garbage tax 270 1465 1735
The cumulative_sum in first row shows wrongly as 125.It should show as 385.I want to display the correct cumulative_sum group by month.
SQL Fiddle: http://sqlfiddle.com/#!9/c7b979/1/0
First thing you need to update is remove comparisons on id because id is not sequential as date January record can come after march or nay month record so this may result as invalid calculations. Second thing for cumulative sum and progressive total you need to do this when your main query is executed with GROUP BY like
SELECT *,
COALESCE((SELECT SUM(x.rupees) FROM house_details x WHERE MONTH(x.date) < t1.month AND x.subincome = "Garbage tax" ), 0) AS progressive_total,
(SELECT SUM(x.rupees) FROM house_details x WHERE MONTH(x.date) <= t1.month AND x.subincome = "Garbage tax" ) AS cumulative_sum
FROM (
SELECT MONTHNAME(t.date) AS `monthname`,
MONTH(t.date) `month`,
YEAR(t.date) AS iyear,
t.income,
t.subincome,
t.ssubincome,
SUM(rupees) AS amount
FROM house_details t
WHERE YEAR(t.date) = YEAR(CURRENT_DATE()) AND t.subincome = "Garbage tax"
GROUP BY MONTH(t.date)
ORDER BY t.date
) t1
Demo
Related
So i have this code:
SELECT a.total_sales AS July, b.total_sales AS August, c.total_sales AS September
FROM
(SELECT EXTRACT(month FROM delivered_at) AS month, ROUND(SUM (sale_price),2) AS total_sales
FROM `bigquery-public-data.thelook_ecommerce.order_items`
WHERE status = 'Complete' AND delivered_at BETWEEN "2022-01-01" AND "2022-10-01"
GROUP BY month
ORDER BY month) a,
(SELECT EXTRACT(month FROM delivered_at) AS month, ROUND(SUM (sale_price),2) AS total_sales
FROM `bigquery-public-data.thelook_ecommerce.order_items`
WHERE status = 'Complete' AND delivered_at BETWEEN "2022-01-01" AND "2022-10-01"
GROUP BY month
ORDER BY month) b,
(SELECT EXTRACT(month FROM delivered_at) AS month, ROUND(SUM (sale_price),2) AS total_sales
FROM `bigquery-public-data.thelook_ecommerce.order_items`
WHERE status = 'Complete' AND delivered_at BETWEEN "2022-01-01" AND "2022-10-01"
GROUP BY month
ORDER BY month) c
WHERE a.month = 7 AND b.month = 8 AND c.month = 9
I got the result that i wanted, which is this:
Row July August September
1 148622.29 169310.62 209339.57
Is there any simpler ways to do this?
We can reduce 3 subquerys into 1 subquery
SELECT
SUM(IF(t.month=7,t.total_sales,0)) AS July,
SUM(IF(t.month=8,t.total_sales,0)) AS August,
SUM(IF(t.month=9,t.total_sales,0)) AS September
FROM
(
SELECT EXTRACT(month FROM delivered_at) AS month, ROUND(SUM (sale_price),2) AS total_sales
FROM `bigquery-public-data.thelook_ecommerce.order_items`
WHERE status = 'Complete' AND delivered_at BETWEEN "2022-01-01" AND "2022-10-01"
AND month in(7,8,9)
GROUP BY month
) t
I have written query which should fetch ADJUSTMENT_AMOUNT and PAY_AMOUNT from the table ABC for different month, I have used subqueries to get this done, each ADJUSTMENT_AMOUNT and PAY_AMOUNT contains number, which is addition of multiple rows for the month by that each month I can see how much adjustment amount is available, same with PAY_AMOUNT.
But with the query that I have written, it is failing when ADJUSTMENT_AMOUNT is calculated using june,july,august,september month but PAY_AMOUNT is calculated using only september month, here in this condition PAY_AMOUNT is duplicated. I just want to avoid the duplicate values and make it blank, so basically only one row should be available for PAY_AMOUNT and rest 3 rows should be blank.
ADJ_MONTH ADJUSTMENT_AMOUNT CURRENCY PAY_MONTH PAY_AMOUNT
September 445 USD September 177.14
June 200 USD September 177.14
July 67 USD September 177.14
August 23 USD September 177.14
My query:
SELECT *
FROM
(SELECT TO_CHAR(CRE_DT, 'Month') AS ADJ_MONTH ,
SUM(ADJ_AMT) AS ADJUSTMENT_AMOUNT,
CURRENCY_CD
FROM ci_Adj
WHERE sa_id IN
(SELECT sa_id FROM ci_Sa WHERE acct_id=:F1
)
AND EXTRACT( YEAR FROM cre_dt) = EXTRACT(YEAR FROM sysdate)
GROUP BY TO_CHAR(CRE_DT, 'Month'),
CURRENCY_CD
ORDER BY TO_CHAR(CRE_DT, 'Month') DESC
),
(SELECT TO_CHAR(pae.cre_dttm, 'Month') AS PAY_MONTH ,
SUM(pa.PAY_AMT) AS PAY_AMOUNT
FROM ci_pay_event pae,
ci_pay pa
WHERE pa.acct_id =:F1
AND pa.pay_status_flg ='50'
AND pae.pay_event_id =pa.pay_event_id
AND EXTRACT( YEAR FROM pae.cre_dttm) = EXTRACT(YEAR FROM sysdate)
GROUP BY TO_CHAR(pae.cre_dttm, 'Month')
ORDER BY TO_CHAR(pae.cre_dttm, 'Month') DESC
)
Adding to points mentioned by (kfinity) and assuming the sub-queries are working fine. The below query will give all the data from the first sub-query and if it has any co-related data in the second query you get it, else the columns from query b will be null.
SELECT a.ADJ_MONTH, a.ADJUSTMENT_AMOUNT, a.CURRENCY_CD, b.PAY_MONTH, b.pay_amount
FROM
(SELECT TO_CHAR(CRE_DT, 'Month') AS ADJ_MONTH ,
SUM(ADJ_AMT) AS ADJUSTMENT_AMOUNT,
CURRENCY_CD
FROM ci_Adj
WHERE
sa_id IN (SELECT sa_id FROM ci_Sa WHERE acct_id=:F1)
AND EXTRACT( YEAR FROM cre_dt) = EXTRACT(YEAR FROM sysdate)
GROUP BY TO_CHAR(CRE_DT, 'Month'), CURRENCY_CD
ORDER BY TO_CHAR(CRE_DT, 'Month') DESC
)a,
(SELECT TO_CHAR(pae.cre_dttm, 'Month') AS PAY_MONTH ,
SUM(pa.PAY_AMT) AS PAY_AMOUNT
FROM ci_pay_event pae,
ci_pay pa
WHERE pa.acct_id =:F1
AND pa.pay_status_flg ='50'
AND pae.pay_event_id =pa.pay_event_id
AND EXTRACT( YEAR FROM pae.cre_dttm) = EXTRACT(YEAR FROM sysdate)
GROUP BY TO_CHAR(pae.cre_dttm, 'Month')
ORDER BY TO_CHAR(pae.cre_dttm, 'Month') DESC
)b
where a.ADJ_MONTH=b.pay_amount (+);
I am supposed to write a query that shows the total profit in descending order for each month in 2010.
So far, I have a query that shows the total profits in descending order for 2010 and I have a query that can extract each month, but I can't seem to connect the two.
select (SalesPrice - AcquisitionPrice) profit, datesold
from Transaction
where DateSold >= '10-jan-01' and DateSold <= '10-dec-31';
and:
select to_char(datesold, 'mon')
from transaction
group by to_char(datesold, 'mon')
Oracle Query:
SELECT TRUNC( datesold, 'MM' ) AS "Month",
SUM( SalesPrice - AcquisitionPrice ) AS profit
FROM Transaction
WHERE EXTRACT( YEAR FROM datesold ) = 2010
GROUP BY TRUNC( datesold, 'MM' )
ORDER BY TRUNC( datesold, 'MM' ) DESC
MySQL and Oracle Query:
SELECT EXTRACT( MONTH FROM datesold ) AS mnth,
SUM( SalesPrice - AcquisitionPrice ) AS profit
FROM Transaction
WHERE EXTRACT( YEAR FROM datesold ) = 2010
GROUP BY EXTRACT( MONTH FROM datesold )
ORDER BY EXTRACT( MONTH FROM datesold ) DESC
I have two tables reg_dealer and claim_data .
SELECT YEAR(claim_dt) AS `Year`,
MONTHNAME(claim_dt) AS `Month`,
COUNT(distinct `uniquecode`) AS `No of Reg`
FROM claim_data
WHERE YEAR(claim_dt) = '2016'
GROUP BY `Year`, `Month`
ORDER BY MONTH(claim_dt) DESC
SELECT YEAR(reg_date) AS `Year`,
MONTHNAME(reg_date) AS `Month`,
COUNT(*) AS `No of Reg`
FROM reg_dealer
WHERE YEAR(reg_date) = '2016'
GROUP BY `Year`, `Month`
ORDER BY MONTH(reg_date) DESC
i am getting month wise result from both table in same format.
Year----------Month----------No_of_Reg
2016 March 150
2016 February 125
2016 Janurary 75
and i want registration and claim in a one go
Year----------Month----------No_of_Reg---------No_of_claims
2016 March 150 350
2016 February 125 250
2016 Janurary 75 150
also want to get the unique claimants
please help
Join the queries.
SELECT t1.Year, t1.Month, `No of Reg`, `No of Claims`
FROM (SELECT YEAR(claim_dt) AS `Year`, MONTHNAME(claim_dt) AS `Month`, MONTH(claim_dt) AS MonthNum, COUNT(distinct `uniquecode`) AS `No of Claims`
FROM claim_data
WHERE YEAR(claim_dt) = '2016' GROUP BY `Year`, `Month`) AS t1
JOIN (SELECT YEAR(reg_date) AS `Year`, MONTH(claim_dt) AS MonthNum, COUNT(*) AS `No of Reg`
FROM reg_dealer
WHERE YEAR(reg_date) = '2016'
GROUP BY `Year`, `MonthNum`) AS t2
ON t1.Year = t2.Year AND t1.MonthNum = t2.MonthNum
ORDER BY t1.Year, t1.MonthNum DESC
MySql query is like this
SELECT MONTHNAME(access_date) as date,
DATE_FORMAT( access_date, '%d/%m/%Y' ) as month_date,
COUNT( log_id ) as total_count
FROM user_activity_log
WHERE dam_id = (
SELECT dam_id
FROM dam_content_details
WHERE content_type= 'userLogin'
)
AND CAST(access_date as DATE) BETWEEN '2012-09-01'
AND '2014-01-01'
GROUP BY MONTH( access_date )
ORDER BY access_date ASC
The problem i faced is the data of November & December in 2012 year is adding with November & December of 2013 year & showing in a one row. But i want to be separate rows for this.
The second one is its only showing the first 12 months not up to 2014 January.
My sample output is like this
date month_date total_count
--------- ------------ -----------
September 15/09/2012 7
October 05/10/2012 34
November 05/11/2012 21
December 07/12/2012 49
January 01/01/2013 45
February 02/02/2013 107
March 01/03/2013 158
April 01/04/2013 100
May 01/05/2013 393
June 01/06/2013 272
Try this:
SELECT Monthname(access_date) AS DATE,
Date_format(access_date, '%d/%m/%Y') AS month_date,
Count(log_id) AS total_count
FROM user_activity_log
WHERE dam_id = (SELECT dam_id
FROM dam_content_details
WHERE content_type = 'userLogin')
AND Cast(access_date AS DATE) BETWEEN '2012-09-01' AND '2014-01-01'
GROUP BY Year(access_date),
Month(access_date)
ORDER BY access_date ASC
OR
Query WITH JOIN
SELECT Monthname(ual.access_date) AS DATE,
Date_format(ual.access_date, '%d/%m/%Y') AS month_date,
Count(DISTINCT ual.log_id) AS total_count
FROM user_activity_log ual
INNER JOIN dam_content_details dcd
ON ual.dam_id = dcd.dam_id
AND dcd.content_type = 'userLogin'
WHERE ual.access_date BETWEEN '2012-09-01' AND '2014-01-01'
GROUP BY Year(ual.access_date),
Month(ual.access_date)
ORDER BY ual.access_date ASC