SQL Query for multiple time frames - mysql

I am trying to pull a report from a data set , the conditions are as follow:
Customer A,B and C produced 100, 150 and 200 tickets respectively in a year.
A's period from 1/1/2022 till 3/30/2022
B's period from 1/10/2022 till 6/20/2022
C's period from 6/10/2022 till 9/5/2022
I want to pull how many cases each customer produced while they are in the incubation period. Such that the report will not include any cases outside the customers incubation period.
The start date and end date in available in a table.
Hopefully I was able to explain this, thanks for your help.

Related

Access Creating sum(iif to get the number of periods which apply

I'm trying to make a sum iif function which checks based on an employee's hire date and the ending pay period date whether they are part of the payroll period. So my idea was sum(iif([table1].[hiredate]<=[table2].[ppend],1,0))
While it works well for some, for some employees the number it gives is ridiculous. To give you an idea, there are 200 records for ppend, but it returns for some employees especially those who are well before the earliest ppend date, a number of 400 upward to 450. I'm also attempting to have it compare relative to the current date as well so I've used sum(iif(table1.hiredate<=table2.ppend<=date(),1,0)) but it largely leads to the same result. Could anybody help. Perhaps there is a factor I've neglected.
For table 1 the column data is in this order: employee ID, name, hire date and table 2 is payroll date, pp beg, pp end.

How to write sql query in laravel to fetch different exp category

I need to fetch the data in order to do back end testing
Task - I am having Manpower table with set of columns. In the report we need to display employeese who falls under different experience category.
ex -
0-6 months
6-12 months
12-36 months
More than 36 months...
Solution I have is get 'date of joining' and compare with the current date and caluculate months, But not getting how to group into different exp category. Please help me.

MySQL: Extract data for line graph to show counts over time period

I have a table which captures when certain events (say alien attacks) happened. Each time the aliens attack a new record is created in this table (some days can have multiple attacks and some days none)..
attack_id attack_date
--------- ---------
1 03/12/2015
2 03/12/2015
3 04/01/2015
4 04/21/2015
5 06/14/2015
I want to show in a line graph how many attacks occured per week. So the x-axis would be weeks in the year and the y-axis would be the number attacks in that week.
Thus the result set to feed my graph might look like
Week Number of attacks
---- -----------------
Can someone suggest a mysql query?
two things you need: week() function to get the week from the date, and count() to get how many attacks happened:
SELECT WEEK(alien_date) as attack_week, COUNT(*) as num_of_attacks
FROM yourTable
GROUP BY WEEK(alien_date)

Finding the sum of a set of calculated sums

I am developing a php/mysql database.
I have a table called ‘actions’ which (amongst others) contains fields hrs, mins, actiondate, invoiceid and staffid.
For any particular actiondate there could be any number of actions carried out by various staff who would enter their time as hrs and mins.
What I need to do is produce a table which for each date and for a specific member of staff and invoice, adds up all of the hrs and mins for each date as a decimal, rounds it up to the nearest quarter and displays that result. I also need to be able to add up all of those results and display that total.
For example, if on March 1st, person with staffid=23 had carried out 4 actions for invoiced 121 lasting, 1h2m, 23m, 10m and 20m the total for that day would be 62+23+10+20 = 115m = 115/60 = 1.92 which would be rounded up to 2.00.
I can get each day’s total (maybe not very elegantly) and display it against the date using the code below
SELECT actions.`actiondate`,
(FORMAT((((CEIL((((60*SUM(hrs))+SUM(mins))/60)*4))/4)),2)) AS dayfeeqtr
FROM actions
WHERE staff.staffid=’23’
AND invoiceid=‘121’
GROUP BY actions.`actiondate`
However, what I can’t work out, is how can I add up all of these rounded up results for that invoice and that member of staff.
Can anyone help please?
If I understand correctly, you can use a subquery:
SELECT sum(dayfeeqtr)
FROM (SELECT a.`actiondate`,
FORMAT((((CEIL((((60*SUM(hrs))+SUM(mins))/60)*4))/4)), 2) AS dayfeeqtr
FROM actions a
WHERE s.staffid = '23' AND invoiceid = '121'
GROUP BY a.`actiondate`
) a;
I do note that your query is not correct -- for instance, there is a reference to staff, which is not in a from clause. However, you say that this is working, so I assume the errors are a transcription problem.

Trouble creating a SQL query

I've been thinking about how to compose this SQL query for a while now, but after thinking about it for a few hours I thought I'd ask the SO community to see if they have any ideas.
Here is a mock up of the relevant portion of the tables:
contracts
id
date
ar (yes/no)
term
payments
contract_id
payment_date
The object of the query is to determine, per month, how many payments we expect, vs how many payments we received.
conditions for expecting a payment
Expected payments begin on contracts.term months after contracts.date, if contracts.ar is "yes". Payments continue to be expected until the month after the first missed payment.
There is one other complication to this: payments might be late, but they need to show up as if they were paid on the date expected.
The data is all there, but I've been having trouble wrapping my head around the SQL query. I am not an SQL guru - I merely have a decent amount of experience handling simpler queries. I'd like to avoid filtering the results in code, if possible - but without your help that may be what I have to do.
Expected Output
Month Expected Payments Received Payments
January 500 450
February 498 478
March 234 211
April 987 789
...
SQL Fiddle
I've created an SQL Fiddle: http://sqlfiddle.com/#!2/a2c3f/2
Without writing up the query, I can give you the general idea:
In the contracts table, cast date + term in months, and group the result set by months where ar = 'YES'. This gives you the expected payments.
With the second table, cast payment_date in months and group by months for the number of received payments.
You can then join these two sub-results on month to get both pieces of information in one result set.