Fill zero for missing month - mysql

I'm trying to display zero on missing month, but didn't succeed.
Table:
clicks | impressions | ctr | position | month | year
111 2709 4 20 3 2015
101 2695 3 20 6 2015
76 2714 2 21 7 2015
.
.
.
64 1212 4 25 11 2015
81 1905 4 24 12 2015
Required output:
clicks | impressions | ctr | position | month | year
0 0 0 0 1 2015
0 0 0 0 2 2015
111 2709 4 20 3 2015
0 0 0 0 4 2015
0 0 0 0 5 2015
101 2695 3 20 6 2015
.
.
.
64 1212 4 25 11 2015
81 1905 4 24 12 2015

Like #jarlh suggested ,you can do it like this: creating a 'table' that contains all month availabe(you will have to populate it your self, add which month and years you want) and then left join to the original table and when value not exists, put 0.
select coalese(s.clicks,0) as clicks,
coalese(s.impressions ,0) as impressions,
coalese(s.ctr ,0) as ctr ,
coalese(s.position ,0) as position ,
t.month,
t.year
from(
SELECT 1 as month_num,2015 as year_num
union SELECT 2,2015
union select 3,2015
union select 4,2015 ....) t
LEFT OUTER JOIN YourTable s
ON(t.month_num = s.month and t.year_num = s.year)

Related

How can I join 2 tables by their dates in SQL?

I have 2 tables
cumulative_money_in:
Cumulative_amount | date
500 | Apr 12
570 | Apr 14
620 | Apr 16
cumulative_money_out:
Cumulative_amount | date
100 | Apr 13
170 | Apr 15
200 | Apr 17
I want my result in this example to be:
Remaining_balance | date
500 | Apr 12
400 | Apr 13
470 | Apr 14
400 | Apr 15
450 | Apr 16
420 | Apr 17
i.e. I want to have a timeline of how the remaining balance (money_in-money_out) changes
What I have so far is this:
SELECT date, (COALESCE(money_in.cumulative_amount,0) - COALESCE(money_out.cumulative_amount,0))
FROM money_in
FULL OUTER JOIN money_out
ON money_in.date <= money_out.date
Which clearly is not what I want to have
You can union all your tables, order by date. and then calculate remaining money for each interval with a left join:
with money_start as (
select *, 1 flag from money_in
union all
select *, -1 flag from money_out
),
money as (
select * from money_start order by date
)
select case when m1.flag = 1
then m1.amount - coalesce(m2.amount, 0)
else m2.amount - m1.amount end remaining,
m1.date
from money m1
left join money m2 on m2.date = date_sub(m1.date, interval 1 day)
order by m1.date;
Output:
remaining
date
500
2021-04-12
400
2021-04-13
470
2021-04-14
400
2021-04-15
450
2021-04-16
420
2021-04-17

Getting the list of all months and the number of products ordered for the month, and should have '0'for the month with no orders - mysql

I have a set of data that looks like this
Cust Id order date Ordered Product
1 Jan 2 1
1 Jan 5 2
1 March 14 1
1 September 9 1
1 December 12 2
2 Jan 5 1
2 Feb 13 2
3 March 12 2
3 April 5 3
3 June 10 2
and my output should look like this
Cust Id order Date Order product
1 Jan 31 3
1 feb 29 0
1 Mar31 1
1 Apr 30 0
1 May 31 0
1 June 30 0
1 July 31 0
1 Aug 31 0
1 Sept 30 1
1 oct 31 0
1 Nov 30 0
1 Dec 31 2
and I have got this far
1 January 31 3
1 March 31 1
1 September 30 1
1 December 31 2
and my code is
select customer_id,
date_format(last_day(order_date), '%M %d') as new_months,
sum(products_ordered) as total
from amazon_test
where customer_id =1
group by new_months, customer_id;
I currently stuck at the part where I need to have all the months and '0' as the output since no orders were made.
If you are running MySQL 8.0, one option is to use a recursive query to generate the months, and then bring the table with a left join:
with recursive months as (
select customer_id, date_format(min(order_date), '%Y-%m-01') order_date, max(order_date) max_order_date
from amazon_test
group by customer_id
union all
select customer_id, order_date + interval 1 month, max_order_date
from months
where order_date + interval 1 month < max_order_date
)
select
m.customer_id,
date_format(last_day(m.order_date), '%M %d') new_months,
coalesce(sum(t.products_ordered), 0) ordered_products
from months m
left join amazon_test t
on t.customer_id = m.customer_id
and t.order_date >= m.order_date
and t.order_date < m.order_date + interval 1 month
where m.customer_id = 1
group by m.customer_id, m.order_date
order by m.customer_id, m.order_date
I phrased the query so it actually operates on all customer_ids - if you remove the where clause in the outer query, you do get the results for all customers. If you really want the results for only one customer, you can optimize the query by pushing the where filter to the anchor of the recusive query.
Demo on DB Fiddle:
customer_id | new_months | ordered_products
----------: | :----------- | ---------------:
1 | January 31 | 3
1 | February 29 | 0
1 | March 31 | 1
1 | April 30 | 0
1 | May 31 | 0
1 | June 30 | 0
1 | July 31 | 0
1 | August 31 | 0
1 | September 30 | 1
1 | October 31 | 0
1 | November 30 | 0
1 | December 31 | 2

query to divide data

we have two columns id and monthid.
The output what I'm looking for is to divide year from month Id based on quarter . The output column should be from quarter. If id is active output should be 1 else 0 .If id comes in any of the 1st quarter (eg:only 1) the output is still 1 .
Like this:
id month
-----------------------------------
100 2012-03-01 00:00:00.0
100 2015-09-01 00:00:00.0
100 2016-10-01 00:00:00.0
100 2015-11-01 00:00:00.0
100 2014-01-01 00:00:00.0
100 2013-04-01 00:00:00.0
100 2014-12-01 00:00:00.0
100 2015-02-01 00:00:00.0
100 2014-06-01 00:00:00.0
100 2013-01-01 00:00:00.0
100 2014-05-01 00:00:00.0
100 2016-05-01 00:00:00.0
100 2013-07-01 00:00:00.0
result should be something like
ID YEAR QTR output (1 or 0)
--------------------------------------------------
100 2012 1 1
100 2012 2 0
100 2012 3 0
100 2012 4 0
100 2013 1 1
100 2013 2 1
100 2013 3 1
100 2013 4 0
Below is the one I tried but it doesn't return the expected results. Please help me achieve this.I want when the ouput is 0 as well.
select a.id,a.year,a.month,
CASE WHEN a.month BETWEEN 1 AND 4 THEN 1
ELSE 0 END as output
from
(select id,trim(substring(claim_month_id,1,4)) as year,(INT((MONTH(monthid)-1)/3)+1) as month from test) a
group by a.id,a.year,a.month
Any help would be appreciated.
#Ani; there is no hierarchical query in Hive to create four quarters (1,2,3,4) so I create a small table for it. Then I get all patient_id, year and month that exists in ims_patient_activity_diagnosis table. Finally, I did a right join on all possible patient id, year and quarters (1,2,3,4); If the id or year or quarter does not exists in the right join, then there is no activity for that id, year and quarter. I assign activity=0 for those rows.
I also inserted patient id=200 to test if there are more patient id in the table. Hope this helps. Thanks.
create table dbo.qtrs(month int);
insert into qtrs values (1),(2),(3),(4);
select DISTINCT NVL(ims.id, qtr.id) as patient_id,
qtr.year as year,
qtr.month as month,
CASE WHEN ims.id > 0 THEN 1 ELSE 0 END as activity
from sandbox_grwi.ims_patient_activity_diagnosis ims
right join (select distinct ims.id,YEAR(ims.month_dt) as year,qtrs.month from sandbox_grwi.ims_patient_activity_diagnosis ims join dbo.qtrs qtrs) qtr
on (ims.id=qtr.id and YEAR(ims.month_dt)=qtr.year and INT((MONTH(month_dt)-1)/3)+1=qtr.month)
sort by patient_id, year, month;
Sample Result:
p_id year month activity
100 2012 1 1
100 2012 2 0
100 2012 3 0
100 2012 4 0
100 2013 1 1
100 2013 2 1
100 2013 3 1
100 2013 4 0
100 2014 1 1
100 2014 2 1
100 2014 3 0
100 2014 4 1
100 2015 1 1
100 2015 2 0
100 2015 3 1
100 2015 4 1
100 2016 1 0
100 2016 2 1
100 2016 3 0
100 2016 4 1
200 2012 1 1
200 2012 2 0
200 2012 3 0
200 2012 4 0
200 2013 1 0
200 2013 2 1
200 2013 3 0
200 2013 4 0
additional sample data:
insert into sandbox_grwi.ims_patient_activity_diagnosis values
(200, '2012-03-01'),
(200, '2013-04-01');

how to to get a record from mysql 20 consecutive days?

I have a table XX. i need to get the records which are 20 days consecutive gap .below is my table look
ID ISmen Date
1 0 2013-05-2
2 0 2013-05-2
3 0 2014-04-2
4 1 2014-05-2
5 1 2014-05-2
6 0 2014-05-2
7 0 2014-05-2
8 0 2014-05-2
9 1 2014-05-25
10 1 2014-05-25
11 0 2014-05-26
12 1 2014-05-27
13 0 2014-05-28
From the above table i need to get the records which are ismen is 1 and the next record ismen is also 1 (i.e 4,5 and 9,10 but not 12).and one more thing 4,5 and 9,10 should have 20 days gap
i am getting the records which are 4,5 and 9,10 ..but i can't able to check date difference between the records .i know we can achieve in the loop but i am trying to get in MySQL is it possible or not.I try below query.thanks in advance for help
SELECT *
FROM XX t1,
XX t2
WHERE (t1.ID=t2.ID+1
OR t1.ID=t2.ID-1)
AND t1.Ismen=1
AND t2.Ismen=1
There is a 23 day gap between |4|5| to |9|10| but ignoring the sample data precision, this result:
| ISMEN | T1ID | T2ID | T1DATE | T2DATE |
|-------|------|------|----------------------------|----------------------------|
| 1 | 4 | 9 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
| 1 | 5 | 9 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
| 1 | 4 | 10 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
| 1 | 5 | 10 | May, 02 2014 00:00:00+0000 | May, 25 2014 00:00:00+0000 |
was produced by this query:
select
t1.ismen
, t1.id as t1id
, t2.id as t2id
, t1.`date` as t1date
, t2.`date` as t2date
from table1 as t1
inner join table1 as t2 on t1.ismen = t2.ismen
and t1.`date` + INTERVAL 23 DAY = t2.`date`
The wanted gap between records can be defined in the join conditions (change to 20 or whatever). But do note there is nothing to stop 4 relating to 9 and 10 or 5 to 9 & 10 so you get 4 records in total.
see: http://sqlfiddle.com/#!9/8d941/1
You could reduce that result by some means (e.g. using row_number() but I don't know if that is required.

access filter needed

Here is a sample data set
-----------------------------------------------------------------------------------------
id nameid name score diff include quantity grade
---------------------------------------------------------------------------------------
7 0002 MO 10 0 0 25 3
8 0002 MO 18 0 1 25 3
9 0002 MO 20 0 0 25 3
10 0002 MO 14 0 0 17 6
11 0002 MO 100 0 0 17 6
11 0002 MO 100 0 0 17 6
12 0003 MA 10 0 0 12 3
13 0003 MA 18 0 0 12 3
14 0003 MA 20 0 0 12 3
15 0003 MA 14 0 0 25 6
16 0003 MA 100 0 1 25 6
17 0003 MA 100 0 0 25 6
12 0004 MB 10 0 0 12 3
13 0004 MB 18 0 1 12 3
14 0004 MB 20 0 0 12 3
15 0004 MB 14 0 0 07 6
16 0004 MB 100 0 1 07 6
17 0004 MB 100 0 0 07 6
I have a query that returns the above table.
Note that in each group of six, there WILL be atleast one row that has value 1 in include column.
Look at ref: access query needed but not needed.
Also for each group of six, there are three rows that has grade = 3 and 3 rows that has grade = 6.
And corresspondingly, the grade 3 and grade 6 have the same quantity in that group.
What I want to do is filter out all the rows that have less then 15 quantity.
However, I still want to group them by 6.
I want to remove a "group" that has both quantity < 15 for both grade 3 and 6. From the above data set
I wwant the following result:
-----------------------------------------------------------------------------------------
id nameid name score diff include quantity grade
---------------------------------------------------------------------------------------
7 0002 MO 10 0 0 25 3
8 0002 MO 18 0 1 25 3
9 0002 MO 20 0 0 25 3
10 0002 MO 14 0 0 17 6
11 0002 MO 100 0 0 17 6
11 0002 MO 100 0 0 17 6
12 0003 MA 10 0 0 12 3
13 0003 MA 18 0 0 12 3
14 0003 MA 20 0 0 12 3
15 0003 MA 14 0 0 25 6
16 0003 MA 100 0 1 25 6
17 0003 MA 100 0 0 25 6
So basically if a group of six has include = 1 in any row, and either grade 3 or 6 quantity > 15 then I want the entire group.
"So basically if a group of six has include = 1 in any row, and either grade 3 or 6 quantity > 15 then I want the entire group."
My guess is this query will identify the candidate nameid groups:
SELECT DISTINCT nameid
FROM YourTable
WHERE
include = 1
AND quantity > 15
AND (grade = 3 OR grade = 6);
If I guessed correctly, you can save it as a separate query, or use it as a subquery, and INNER JOIN it to YourTable to limit the rows returned to only those where nameid meets your criteria. It might look close to this untested SELECT statement:
SELECT y.id, y.nameid, y.[name], y.score, y.diff, y.include, y.quantity, y.grade
FROM
YourTable AS y
INNER JOIN [
SELECT DISTINCT nameid
FROM YourTable
WHERE
include = 1
AND quantity > 15
AND (grade = 3 OR grade = 6)
]. AS q
ON y.nameid = q.nameid
ORDER BY y.nameid;
Edit: Add an index on nameid if you don't already have one.