Retrieve repeated month data in separate rows - mysql

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

Related

SQL: SELECT AS multiple value with the same FROM with different WHERE

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

Avoid duplicate in query and make all the duplicated row blank

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 (+);

Display correct cumulative_sum value using Mysql

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

Retrieve data for specific months

MySQL Query is like this
SELECT MONTHNAME(access_date) as date,
DATE_FORMAT( access_date, '%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= '$content_type'
)
AND access_date >= last_day(NOW() - INTERVAL ($month) MONTH)
GROUP BY MONTH( access_date )
ORDER BY access_date ASC
i will pass the numbers like 1,2,3.... then its giving value for that month.
The problem i faced is it retrieving the data per 30 days,60 days like that. I want if i will write $month = '1; then it should return the current month data & previous month data starting from day 1.
My sample output - $month = 2
date month_date total_count
--------- ------------ -----------
December 12/2013 4
January 01/2014 1
I want for december it should calculate from 12/01/2013. 1st December 2013. Any idea how to solve it ?
You just have to remove = from >=.
Try this:
SELECT MONTHNAME(access_date) as date,
DATE_FORMAT( access_date, '%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= '$content_type') AND
access_date > LAST_DAY(NOW() - INTERVAL (($month)+1) MONTH)
GROUP BY MONTH( access_date )
ORDER BY access_date ASC

MYSQL Query group by custom Month date?

I wrote a query that returns monthly sales.
SELECT
count(O.orderid) as Number_of_Orders,
concat (MonthName(FROM_UNIXTIME(O.`date`)),' - ',year(FROM_UNIXTIME(O.date))) as Ordered_Month,
sum(O.total) as TotalAmount,
Month(FROM_UNIXTIME(O.`date`)) as Month_of_Year,
year(FROM_UNIXTIME(O.date)) as Sale_Year
FROM orders O
group by Month_of_Year, Sale_Year
order by Sale_Year DESC,Month_of_Year DESC
I would like to make it group for a custom date like
instead of 1st to 1st, it should group for 10th -10th of every month.
Not sure how to group it that way!
because you are dealing with a time "shift", you'll have to do that math in your equation to "fake it out". Something like
SELECT
count(O.orderid) as Number_of_Orders,
concat(
MonthName( Date_Sub( FROM_UNIXTIME(O.`date`), INTERVAL 10 DAY )),
' - ',
Year( Date_Sub( FROM_UNIXTIME(O.date), INTERVAL 10 DAY) )
) as Ordered_Month,
sum(O.total) as TotalAmount,
Month( Date_Sub( FROM_UNIXTIME(O.`date`), INTERVAL 10 DAY )) as Month_of_Year,
Year( Date_Sub( FROM_UNIXTIME(O.date), INTERVAL 10 DAY )) as Sale_Year
FROM
orders O
group by
Month_of_Year,
Sale_Year
order by
Sale_Year DESC,
Month_of_Year DESC
So, in essence, you are taking the dates ex: March 11-31 + April 1-10 and subtracting "10 days" from them... so for the query, they will look like March 1-31, and April 11-30 will appear like April 1-20 + May, etc for rest of each year...
Not tested.
group by Month_of_Year, ceil(day(o.`date`)/10), Sale_Year
This is a better idea in order to avoid having 4 groups but just 3
select
month(my_date) as your_month,
year(my_date) as your_year,
case
when day(my_date) <= 10 then 1
when day(my_date) between 11 and 20 then 2
else 3 end as decade,
count(*) as total
from table
group by
your_month,your_year,decade
Adapt it to your needs.