Mysql query for sum data - mysql

I want to make a report with MYSQL from this data
Table name: table
date name Quantity
13 Nov Rudy 40
13 Nov Mery 30
13 Nov Rudy 20
13 Nov John 10
14 Nov Rudy 20
14 Nov Rudy 30
I want the result like this
13 Nov Rudy 60
13 Nov Mery 30
13 Nov John 10
14 Nov Rudy 50
What query should I do?
thanks

I'm not sure if mysql has support for 2 columns in group by clause, so grouping by date + user should work. but if it has, you can skip this trick and replace this clause with (group by date name) as #RaymondNijland proposed.
select date, name, sum(quantity) as total
from your_table
group by concat(date, "_", name);

Related

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.

Group Davg Results by Date

I have the following dataset that looks like this. I'd like to select only those who have a "1" for the ESITwoToFive variable and then compute the average total_ED_LOS for each unique dates. For example, the average for Feb 7th would only have the values from ID's 1 and 3.
ID CheckinDate ESITwoToFive Total_ED_Los
1 Feb 7 1 23
2 Feb 7 0 23
3 Feb 7 1 28
4 Feb 8 1 43
5 Feb 8 1 83
6 Feb 8 0 29
7 Feb 9 1 93
8 Feb 9 1 77
9 Feb 9 0 33
I'm using the following syntax but my output contains the same averages for every date.
SELECT [Fast Track Quality Research ESI Levels].checkin_date,
DAvg("total_ed_los","Fast Track Quality Research ESI Levels","ESITwoToFive =
'1'") AS Expr1
FROM [Fast Track Quality Research ESI Levels]
GROUP BY [Fast Track Quality Research ESI Levels].checkin_date;
Output that i'm getting...
Checkindate Avg LOS
Feb 7 24
Feb 8 24
Feb 9 24
Desired output....
Checkindate Avg LOS
Feb 7 24
Feb 8 54
Feb 9 86
I would suggest using the Avg function rather than the DAvg domain aggregate function, e.g.:
select t.checkin_date, avg(t.total_ed_los) as [Avg LOS]
from [Fast Track Quality Research ESI Levels] t
where t.esitwotofive = 1
group by t.checkin_date
The DAvg function in your example is calculating the average over all records for which ESITwoToFive = '1' before the data is aggregated by date, therefore you receive the same result for every record output by the query.

Read the penultimate value ssrs

i have a report with parameter is the year i can select a multiple value
year
Data 2010 2011 2012
hp 14 25 30
Dell 17 18 20
what i need when i create the report i want to have another column with the last value concatenation with the last previous one like this :
year
Data 2010 2011 2012 2012/2011
hp 14 25 30 3025
Dell 17 18 20 2018
i tried this :
=Last(Field!Qte.value)+Last(Field!Qte.value)-1
but i had the wrong result i got this result :
year
Data 2010 2011 2012 2012/2011
hp 14 25 30 3029
Dell 17 18 20 2019
the problem is in this function
Last(Field!Qte.value)-1
it doesn't return the last previous value

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

MySql Daily Report

I am trying to get some help fixing my complex query. I am explaining below my situation, thanks.
I have the following two tables:
ACTIVITY TABLE:
ID USER_ID CARD_ID CLOCK
1 123 04675545 4/3/2013 1:07:06 PM
2 123 04675545 4/3/2013 2:08:06 PM
3 124 04675550 4/3/2013 2:07:06 PM
4 124 04675550 4/3/2013 4:07:06 PM
5 124 04675550 4/4/2013 10:07:06 AM
6 124 04675550 4/4/2013 2:00:00 PM
7 124 04675550 4/5/2013 4:07:06 PM
8 124 04675550 4/7/2013 8:00:00 AM
9 124 04675550 4/7/2013 5:00:00 PM
PRICE TABLE:
ID FROMTIME TOTIME PRICEPERHOUR
1 08:00:00 19:59:59 50.00
2 20:00:00 07:59:59 75.00
And the following query:
select a.user_id, date(a.clock), ABS(TIME_TO_SEC(TIMEDIFF(a.clock, b.clock))/3600)*c.PRICEPERHOUR as total from
(Select if((#rn:=#rn+1)%2=0,#rn,#rn-1) As rId, act.* from act
join (select #rn:=-1)a
order by user_Id, clock) a
inner join
(Select if((#rn1:=#rn1+1)%2=0,#rn1,#rn1-1) As rId, act.* from act
join
(select #rn1:=-1)b
order by user_Id, clock) b
ON a.rid=b.rid AND a.id <> b.id
inner join
price c
on
TIME_TO_SEC(a.clock) between TIME_TO_SEC(c.FROMTIME)
AND
TIME_TO_SEC(c.TOTIME)
group by a.user_id, date(a.clock)
And I am getting the following result:
USER_ID DATE(A.CLOCK) TOTAL
123 April, 03 2013 00:00:00+0000 50.8333
124 April, 03 2013 00:00:00+0000 100
124 April, 04 2013 00:00:00+0000 194.0833
124 April, 05 2013 00:00:00+0000 1,994.0833
124 April, 07 2013 00:00:00+0000 1,994.0833
However, I am trying to get this result instead:
USER_ID DATE(A.CLOCK) TOTAL
123 April, 03 2013 00:00:00+0000 50.8333
124 April, 03 2013 00:00:00+0000 100
124 April, 04 2013 00:00:00+0000 194.0833
124 April, 05 2013 00:00:00+0000 50
124 April, 07 2013 00:00:00+0000 450
This is part of a clock system. Each time the user check-in, one entry gets recorded on the database. A correct user behavior will be that it has always a pair record recorded. For example user_id 123 clocks at 1:07:06pm and clocks again at 2:08:06pm. However, in some situations, the user may do it just once (unpaired record on the database) and therefore it should only be charged that particular hour from the record. As an example, user 124 on day 4/5/2013.
I am trying all weekend to get this query working :(. Once I get the correct result, I will add a condition to get only one user_id also, (e.g. where user_id=124).
I think even if you manage to do this there are are some potential design pitfalls:
Can people clock in for more than 1 period per day? If so then 2 records for example
10am and 2pm could total 2hours or 4hours.
What happens if people clock in at 11pm and again at 2am?
From a quick glance I don't think your sql takes into account time periods that span across the 2 different pay rates? You should definitely include this scenario in your test data.
If I was going to implement this I would probably move the logic into code, and simplify the price table by only having one time column like:
TIME, PRICE
00:00, 75.00
08:00, 50.00
20:00, 75.00
Also if a user only has one card you may not need to have card_id and user_id in the activity table.