SQL sum and show row with non-existent sum values - mysql

i have 2 tables : dt_user and dt_invoice.
**dt_members :**
id firstname
3 Salim
5 Sara
8 Julie
**dt_invoice**
user_id amount_ht period month year
3 4950 04 2018 04 2018
3 7200 10 2018 10 2018
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
3 8800 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
3 7550 03 2019 03 2019
I want to create a query joining the two table, but i want to show each user_id for PERIOD that there is in table dt_invoice.
**Expected results :**
user_id amount_ht period month year
3 4950 04 2018 04 2018
5 0 04 2018 04 2018 //non-existent record in dt_invoice
8 0 04 2018 04 2018 //non-existent record in dt_invoice
3 7200 10 2018 10 2018
5 0 10 2018 10 2018 //non-existent record in dt_invoice
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
5 0 11 2018 11 2018 //etc ...
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
5 0 12 2018 12 2018
8 0 12 2018 12 2018
3 8800 01 2019 01 2019
5 0 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
5 0 02 2019 02 2018
8 0 02 2019 02 2018
3 7550 03 2019 03 2019
5 0 03 2019 03 2018
8 0 03 2019 03 2018
Thanks in advance for your help, i'm totally stuck ..
SQL datas available here : https://rextester.com/live/LBSEY76360
also in sqlfiddle : http://sqlfiddle.com/#!9/728af3/1

Use a cross join to generate the rows and left join to bring in the values:
select m.user_id, p.period, p.month, p.year,
coalesce(t.amount_ht, 0) as amount_ht
from dt_members m cross join
(select distinct period, month, year from dt_invoice) p left join
dt_invoice t
on t.user_id = m.id and t.period = p.period;

Maybe this would help.
SELECT user_id, amount_ht, period, month, year
FROM dt_invoice
LEFT JOIN dt_members ON user_id = id

Related

Get a Count of month year Record for Certain Label and Replace a Value in Another Column

I have a table called "ticketManager" in my mssql. There were some expenses missing for "ABC" and I got monthly expenses. I want to divide it equally base on the count of Signed_Date.
Event_ID Name ticket Revenue Expences expect Signed_Date
G-00001 ABC 671 6720 0 50 01 June 2021
G-00002 CSA 5 56 18 100 05 June 2021
G-00003 CSA 5 78 38 100 03 June 2021
G-00004 VSX 23 34 23 NaN 03 June 2021
G-00005 ABC 4 89 0 40 02 June 2021
G-00006 ABC 60 73 0 60 15 April 2021
G-00007 CSA 60 345 110 60 12 June 2021
G-00008 ABC 89 890 NaN NaN 02 June 2021
G-00009 VSX 0 0 0 50 30 April 2021
G-00010 CSA 6 45 16 60 22 June 2021
G-00011 VSX 3 39 23 30 10 June 2021
G-00012 ABC 2 34 0 20 03 June 2021
G-00013 VSX 4 89 48 40 12 June 2021
G-00014 VSX 32 127 35 10 24 April 2021
G-00015 ABC 3 84 0 120 21 April 2021
G-00016 ABC 1 100 0 140 7 June 2021
G-00017 CSA 23 525 90 02 April 2021
for example, in June I have 5 records for ABC and I have expenses as 750.00. So I want to place 150 (750/5) for each record same as for April I have expenses as 110 and have 2 records. So want to place 55 for each record in ABC.
So the table looks like below.
Event_ID Name ticket Revenue Expences expect Signed_Date
G-00001 ABC 671 6720 150 50 01 June 2021
G-00002 CSA 5 56 18 100 05 June 2021
G-00003 CSA 5 78 38 100 03 June 2021
G-00004 VSX 23 34 23 NaN 03 June 2021
G-00005 ABC 4 89 150 40 02 June 2021
G-00006 ABC 60 73 55 60 15 April 2021
G-00007 CSA 60 345 110 60 12 June 2021
G-00008 ABC 89 890 150 NaN 02 June 2021
G-00009 VSX 0 0 0 50 30 April 2021
G-00010 CSA 6 45 16 60 22 June 2021
G-00011 VSX 3 39 23 30 10 June 2021
G-00012 ABC 2 34 150 20 03 June 2021
G-00013 VSX 4 89 48 40 12 June 2021
G-00014 VSX 32 127 35 10 24 April 2021
G-00015 ABC 3 84 55 120 21 April 2021
G-00016 ABC 1 100 150 140 7 June 2021
G-00017 CSA 23 525 90 02 April 2021
I have like million reords like that and have 5 years woth of records. What would be the efficience way to do that?
Thanks in advance.
You can use a window function:
SELECT t.*, avg(Expences) over (partition by extract(year_month from Signed_Date))
FROM ticketManager t;

MySQL Get data by previous quarter

I am trying to find the last entry for the previous years quarter.
All I can access is year i.e 2021 and quarter i.e 1
Here is the data in my database:
id
name
start
end
16
April 2021
2021-04-01
2021-04-30
15
March 2021
2021-03-01
2021-03-31
14
February 2021
2021-02-01
2021-02-28
57
November 2020
2020-11-01
2020-11-30
55
October 2020
2020-10-01
2020-10-31
29
September 2020
2020-09-01
2020-09-30
27
July 2020
2020-07-01
2020-07-31
24
April 2020
2020-04-01
2020-04-30
23
March 2020
2020-03-01
2020-03-31
22
February 2020
2020-02-01
2020-02-29
21
January 2020
2020-01-01
2020-01-31
Using the MySQL quarter function I can get it to print out the quarter as an integer in another column:
SET #given_year = 2021;
SET #given_quarter = 1;
SELECT
id, name, start, end, QUARTER(end) as "Q"
FROM
submissions
id
name
start
end
Q
16
April 2021
2021-04-01
2021-04-30
2
15
March 2021
2021-03-01
2021-03-31
1
14
February 2021
2021-02-01
2021-02-28
1
57
November 2020
2020-11-01
2020-11-30
4
55
October 2020
2020-10-01
2020-10-31
4
29
September 2020
2020-09-01
2020-09-30
3
27
July 2020
2020-07-01
2020-07-31
3
24
April 2020
2020-04-01
2020-04-30
2
23
March 2020
2020-03-01
2020-03-31
1
22
February 2020
2020-02-01
2020-02-29
1
21
January 2020
2020-01-01
2020-01-31
1
I tried using WHERE and LIKE but it is returning 0 rows:
SELECT * FROM (
SELECT
id, name, start, end, QUARTER(end) as "Q"
FROM
submissions as s
) AS vs
WHERE
vs.end
LIKE
#given_year
AND
vs.Q < #given_quarter
I also need to account for the possibility that there may be no rows this year and I need to find the previous year.
For example with these two rows, if I was passed the year 2021 and quarter 1 I would need to return November of the previous year and a different quarter.
id
name
start
end
Q
14
February
2021
2021-02-01
2021-02-28
57
November
2020
2020-11-01
2020-11-30
If I understand correctly, you want all the rows from the quarter in the data before a given quarter. You can filter and use dense_rank():
select s.*
from (select s.*,
dense_rank() over (order by year(start) desc, quarter(start) desc) as seqnum
from submissions s
where year(start) < #given_year or
(year(start) = #given_year and quarter(start) < #given_quarter)
) s
where seqnum = 1;
The above returns all rows from the previous quarter (which is what I thought you wanted). If you want only one row:
select s.*
from submissions s
where year(start) < #given_year or
(year(start) = #given_year and quarter(start) < #given_quarter)
order by start desc
limit 1;

Data across multiple years using Sequelize.js

I am trying to find the month-wise data for multiple years in Sequelize.js
For eg.Stardate:2016-01-01 and EndDate:2017-02-29.
table1
date pack_price
2016-01-01 30
2016-02-19 30
2016-03-14 20
2016-10-01 50
2017-01-28 100
2017-02-25 29
Expected Answer:
date pack_price
Jan 2016 30
Feb 2016 30
Mar 2016 20
Oct 2016 50
Jan 2017 100
Feb 2017 29
Actual Answer
Date Pack_price
Jan 2016 130
Feb 2016 59
Mar 2016 20
Oct 2016 50
Node js code using Sequelize.js
Utils.table1.findAll({
where:{
tran_date: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
attributes: [[Sequelize.fn('sum', Sequelize.col('pack_price')),'total'],[Sequelize.fn('month', Sequelize.col('tran_date')),'Month']],
group:[[Sequelize.fn('month', Sequelize.col('tran_date'))]],
raw: true,
}).then(total=>{
Utils.sendResponse(1, total, "success", q)
})
However, this works well for one year.Should work for multiple years.Do you have any idea how I could make this happen?.Thanks in advance.

adding two column values in mysql

I have two mysql tables
suppose table one name 'marks'
no A B C D
1 10 05 01 04
2 08 07 10 05
3 09 05 07 10
4 07 05 04 10
5 04 07 06 09
6 05 09 07 07
7 09 05 10 06
8 09 06 06 08
9 08 06 10 07
10 08 07 04 06
suppose table two name 'results'
in second table I want to put total marks and average marks based on above table.(import data from 'marks' table,process it and save it in 'results' table)
So once it filled it must be like this.
I want add column A,B,C,D in 'marks' table and put total value in column 'Total' in table 'results' and average by dividing 'Total' column by 4.
no Total Average
1 20 5.00
2 30 7.50
3 31 7.75
4 26 6.50
5 26 6.50
6 28 7.00
7 30 7.50
8 29 7.25
9 31 7.75
10 25 6.25
So how can I fill the 'result' table using mysql query?
Is it possible to do in mysql?
Thank you
Try something like:
INSERT INTO result (no, total, average)
SELECT no, A+B+C+D, (A+B+C+D)/4
FROM marks

Sorting table based on Year and Year+Months

I have a table with a column Year and ID
YEAR ID
1988 29
1989 89
1990 22
1992 9
1994 8
1998 23
1922 20
August 1990 12
September 2009 14
August 1991 11
November 2009 33
October 1990 30
January 1990 55
March 2001 24
Is there way I can sort the table in such a way that my final result is in Order.. I am looking for the result like
YEAR ID
1922 20
1988 29
1989 89
1990 22
1992 9
1994 8
1998 23
January 1990 55
August 1990 12
October 1990 30
August 1991 11
March 2001 24
September 2009 14
November 2009 33
Thank you in advance
Just replace myYear with your table name...
select year,id
from
(select year,id,
case when STR_TO_DATE(year,'%Y') is not null then STR_TO_DATE(year,'%Y') else STR_TO_DATE(year,'%M %Y') end as d,
case when STR_TO_DATE(year,'%Y') is not null then 0 else 1 end as ob
from myYear
) y
order by ob asc,d asc;