Today I want to get a help in creating scores per user in my database. I have this query:
SELECT
r1.id,
r1.nickname,
r1.fecha,
r1.bestia1,
r1.bestia2,
r1.bestia3,
r1.bestia4
r1.bestia5
FROM
reporte AS r1
INNER JOIN
( SELECT
nickname, MAX(fecha) AS max_date
FROM
reporte
GROUP BY
nickname ) AS latests_reports
ON latests_reports.nickname = r1.nickname
AND latests_reports.max_date = r1.fecha
ORDER BY
r1.fecha DESC
that's from a friend from this site who helped me in get "the last record per user in each day", based on this I am looking how to count the results in a ranking daily, weekly or monthly, in order to use statistics charts or google datastudio, I've tried the next:
select id, nickname, sum(bestia1), sum(bestia2), etc...
But its not giving the complete result which I want. That's why I am looking for help. Additionally I know datastudio filters where I can show many charts but still I can count completely.
for example, one player in the last 30 days reported 265 monsters killed, but when I use in datastudio my query it counts only the latest value (it can be 12). so I want to count correctly in order to use with charts
SQL records filtered with my query:
One general approach for get the total monsters killed by each user on the latest X days and make a score calculation like the one you propose on the commentaries can be like this:
SET #daysOnHistory = X; -- Where X should be an integer positive number (like 10).
SELECT
nickname,
SUM(bestia1) AS total_bestia1_killed,
SUM(bestia2) AS total_bestia2_killed,
SUM(bestia3) AS total_bestia3_killed,
SUM(bestia4) AS total_bestia4_killed,
SUM(bestia5) AS total_bestia5_killed,
SUM(bestia1 + bestia2 + bestia3 + bestia4 + bestia5) AS total_monsters_killed,
SUM(bestia1 + 2 * bestia2 + 3 * bestia3 + 4 * bestia4 + 5 * bestia5) AS total_score
FROM
reporte
WHERE
fecha >= DATE_ADD(DATE(NOW()), INTERVAL -#daysOnHistory DAY)
GROUP BY
nickname
ORDER BY
total_score DESC
Now, if you want the same calculation but only taking into account the days of the current week (assuming a week starts on Monday), you need to replace the previous WHERE clause by next one:
WHERE
fecha >= DATE_ADD(DATE(NOW()), INTERVAL -WEEKDAY(NOW()) DAY)
Even more, if you want all the same, but only taking into account the days of the current month, you need to replace the WHERE clause by:
WHERE
MONTH(fecha) = MONTH(NOW())
For evaluate the statistics on the days of the current year, you need to replace the WHERE clause by:
WHERE
YEAR(fecha) = YEAR(NOW())
And finally, for evaluation on a specific range of days you can use, for example:
WHERE
DATE(fecha) BETWEEN CAST("2018-10-15" AS DATE) AND CAST('2018-11-10' AS DATE)
I hope this guide will help you and clarify your outlook.
This will give you number of monster killed in the last 30 days per user :
SELECT
nickname,
sum(bestia1) as bestia1,
sum(bestia2) as bestia2,
sum(bestia3) as bestia3,
sum(bestia4) as bestia4,
sum(bestia5) as bestia5
FROM
reporte
WHERE fecha >= DATE_ADD(curdate(), interval -30 day)
GROUP BY nickName
ORDER BY
Related
In short: MySQL - I need to bring company that have been inactive for a while (or 365 days for the fiddle example).
How I check this? each company have at least a contact, who is related to an event, and each event have (many) subevents, in this last table I have the last date of activity, the days that considers that one company is on inactivity is decided for the user, I don't have problem to do this calculation
sql.Append("where DATEDIFF(CURDATE(),DATE(lastdate)) > " +days.ToString()+ "
The problem is, that this check ALL the subevents, so this not only check the last date, but every date... and this means, bad output.
I was thinking on subqueries to get or the max date on the subevent of a contact, or the max date of the subevent of a event.
Then with a friend we get close with sort of this, but the query is infinite.
select * from subevent se
where DATEDIFF(CURDATE(),DATE(
(select se2.dates from subevent se2
where se2.dates in
(select max(se3.dates)
from subevent se3
where se.idev = se3.idev)
group by se2.dates)));
I'm stuck and I would appreciate the help...
Tried group by, subquery and MAX (obviously max is necessary, but don't how where to apply...)
https://www.db-fiddle.com/f/wgSQGn7Z26tHnwm6nAaNSA/8
(On the Fiddle link, should only bring the companyname2 and companyname4)
You can use aggregation to get the last subevent date for each company. Then filter using a having clause:
select c.idcomp
from contact c join
events e
on e.idcont = c.idcont join
subevent se
on se.idev = e.idev
group by c.idcomp
having max(se.date) < current_date - interval 365 day;
Here is a db-fiddle.
I have a table with every login by all users.
I want to run a query that will pull the number of times each user logs in but limit it to 4 if the user logged in more than 4 times on a day.
And then do a sum to get the total number of logins.
Further to this I want to pull back the time frame for the total number of logins. So I specify the total number of logins as 100 then the query must pull back the earliest date, going back from today and counting the number of logins (limited at 4 if above 4) per user.
My query so far to get the list of totals limited to 4 per user:
SELECT (case when (count(l.user_id) > 4) then 4 else count(l.user_id) end) as cappedcount
FROM `logins` l
where l.store_id = 908
and l.login_dt > '2018-04-17 00:00:00' and l.login_dt < '2018-04-18 23:59:59'
group by l.user_id order by cappedcount desc
I'm specifying the date range at the moment but don't want to do that in the final query.
If I understand correctly, you only want to look at the last four logins per user and day and ignore their earlier logins. From this set you want the last 100 logins.
So the first task is to get the four last logins per user and day, which would usually be solved with window functions, but MySQL doesn't feature them. So count in a subquery instead (which may take long):
select *
from logins
where
(
select count(*)
from logins later
where later.user_id = logins.user_id
and date(later.login_dt) = date(logins.login_dt)
and later.login_dt > logins.login_dt
) < 4
order by login_dt desc
limit 100;
I suggest to provide the following index for this query:
create index idx_logins on logins (user_id, login_dt);
What is the version of MySQL you user? Because as far as I know with clause is only supported in recent versions of MySQL.
I believe the answer to your first request is something like :
select sum(cntx) from (
select user_id, date(login_time), least(count(*), 4) cntx
from logins
where login_time between '2018-04-10 00:00:00' and '2018-04-17 00:00:00'
group by user_id, date(login_time)
) x
as you can view it in sqlfiddle.com.
For your second question, I have following answer, I believe it's not the best solution, but it works on MySQL 5.6. In next MySQL version (MySQL 8) you can use with clause which provides better solution for this question. I use views in the solution to skip duplicate queries:
create view xlogins as
select user_id, date(login_time) xdt, least(count(*), 4) xcnt
from logins
group by user_id, date(login_time);
create view xxlogins as
select distinct xdt, (select sum(x2.xcnt)
from xlogins x2
where x2.xdt >= x1.xdt) sumx
from xlogins x1;
select min(x1.xdt)
from xxlogins x1
join xxlogins x2 on x1.xdt < x2.xd
where x1.sumx >= 100
and x2.sumx <= 100
Find the solution in this sqlfiddle.com, I've just changed the 100 to 10.
I'm trying to figure out how to calculate the overtime on timecard bills that have been or need to be paid. The problem is that bills cover more than one weeks worth of hours and the query grabs more than one employee's history at a time. Any suggestions on how to do this, perhaps with a case statement of some kind?
For example, suppose in my list of employees one works 39 hours one week and 45 the next. The bill would show 84 hours worked, and would also need to show 5 hours of overtime (not four!). This needs to be done in the context of the below query, which handles multiple bills and multiple employees.
Note the query below shows how this would work if the billing period was only one week.
select
username,
CASE
WHEN paidOn IS NULL THEN 'Unpaid'
ELSE paidOn
END as paid,
round(sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600,2) AS hours
, CASE
WHEN round(sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600,2) > 40
THEN round((sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600 - 40) * payrate + 40 * payrate,2)
ELSE
round(sum(TIME_TO_SEC(TIMEDIFF(timeOut, timeIn)))/3600 * payrate, 2)
END as pay
from
timecard
LEFT JOIN
employees
ON
employees.userID = timecard.userID
WHERE
paid != 'd'
GROUP BY
paidOn, timecard.userID
ORDER BY
paid DESC
LIMIT 30;
The way I understand it is that this works fine if the billing period is only 1 week, but not when you extend it to multiple weeks. Then I would simply use the query you have above as a subquery and then aggregate your weeks together. Here's a short example of what I mean:
SELECT
EmployeeId,
SUM(RegularPayHours) * RegularPayRate,
SUM(OverTimeHours) * OverTimeRate
FROM
(SELECT
EmployeeId,
DATEPART(week,TimeCardDate) AS [WorkWeek],
CASE WHEN SUM(HoursWorked) > 40 THEN 40 ELSE SUM(HoursWorked) END AS [RegularPayHours],
CASE WHEN SUM(HoursWorked) > 40 THEN SUM(HoursWorked) - 40 ELSE 0 END AS [OvertimeHours]
FROM
TimeCard
WHERE
TimeCardDate BETWEEN StartDate AND EndDate
GROUP BY
EmployeeId,
DATEPART(week,TimeCardDate)
) a
WHERE
WorkWeek IN (1,2)
GROUP BY
EmployeeId
This will give you the first two work weeks combined as one result but with the overtime calculations done at the week level. You can essentially create any custom pay period that is X weeks long with this method.
You may need to custom define what a work-week is by setting the DATEFIRST value.
My app stores payments done by clients. At the end of every month, the total paid that month is calculated and given to owner: I have a payments table that has the following fields (the most important).
id, datepaid, endingdate (actual month's closing date), ammount, type, code, ...
Now, having a year of payments, I've been asked to create a graph of payments evolution (totals) of latest 12 months.
By reading, etc. this is the query I've got, but I don't know how to get the latest 12 months totals (and to get 0 in case no payment was done that month)....
SELECT id, endingdate, datepaid, SUM(ammount) AS total
FROM `sis_payments`
WHERE endingdate >= DATE_SUB( CURDATE( ) , INTERVAL 1 YEAR )
GROUP BY endingdate
I know it may be badly designed, but it's what I was given... any clues? Thanks
You could try GROUP BY YEAR(endingdate), MONTH(endingdate), or something equivalent using DATE_FORMAT. Take a look at the Mysql documentation for DATE_FORMAT.
Also, don't include the date fields (endingdate, datepaid) in the SELECT clause. Instead, use YEAR(endingdate), MONTH(endingdate), just like in GROUP BY.
Getting 0 when there were no payments that month is a little more complicated in SQL. You could handle that in PHP, after running the query.
UPDATE
Example using DATE_FORMAT:
SELECT
DATE_FORMAT(mrendido,'%y-%m') as xyearmonth,
SUM(FORMAT(ammountpaid,2)) AS ammtotal
FROM sis_pyments
WHERE mrendido >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY xyearmonth
ORDER BY xyearmonth ASC
I have some problems when coding SQL group by week.
I have a MySQL table named order.
In this entity, there are several attributes, called 'order_id', 'order_date', 'amount', etc.
I want to make a table to show the statistics of past 7 days order sales amount.
I think first I should get the today value.
Since I use Java Server Page, the code like this:
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int Month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String today = year + "-" + Month + "-" + day;
then, I need to use group by statement to calculate the SUM of past 7 day total sales amount.
like this:
ResultSet rs=statement.executeQuery("select order_date, SUM(amount) " +
"from `testing`.`order` GROUP BY order_date");
I have problem here. In my SQL, all order_date will be displayed.
How can I modify this SQL so that only display past seven days order sale amount?
Besides that, I discover a problem in my original SQL.
That is, if there is no sales on that day, no results would be displayed.
OF course, I know the ResultSet does not allow return null values in my SQL.
I just want to know if I need the past 7 order sales even the amount is 0 dollars,
Can I have other methods to show the 0?
Please kindly give me advices if you have idea.
Thank you.
Usually it occurs to create with a script or with a stored procedure a calendar table with all dates.
However if you prefer you can create a table with few dates (in your case dates of last week) with a single query.
This is an example:
create table orders(
id int not null auto_increment primary key,
dorder date,
amount int
) engine = myisam;
insert into orders (dorder,amount)
values (curdate(),100),
(curdate(),200),
('2011-02-24',50),
('2011-02-24',150),
('2011-02-22',10),
('2011-02-22',20),
('2011-02-22',30),
('2011-02-22',5),
('2011-02-19',10);
select t.cdate,sum(coalesce(o.amount,0)) as total
from (
select curdate() -
interval tmp.digit * 1 day as `cdate`
from (
select 0 as digit union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 ) as tmp) as t
left join orders as o
on t.cdate = o.dorder and o.dorder >= curdate() - interval 7 day
group by t.cdate
order by t.cdate desc
Hope that it helps. Regards.
To answer your question "How can I modify this SQL so that only display past seven days order sale amount?"
Modify the SQL statement by adding a where clause to it:
Where order_date >= #date_7days_ago
The value for this #date_7days_ago date variable can be set before your statement:
Select #date_7days_ago = dateadd(dd,-7,getdate())
Adding that where clause to your query will return only those records which order date is in the last seven days.
Hope this helps.
You can try using this:
ResultSet rs = statement.executeQuery(
"SELECT IFNULL(SUM(amount),0)
FROM table `testing`.`order`
WHERE order_date >= DATE_SUB('" + today + "', INTERVAL 7 DAY)"
);
This will get you the number of orders made in the last 7 days, and 0 if there were none.