Here is a problem. I need to find working days between two dates (without weekends). I currently found and was able to successfully use this approach:
SELECT 5 * (DATEDIFF(DateClosed, DateOpened) DIV 7) + MID('0123455501234445012333450122234501101234000123450',
7 * WEEKDAY(DateOpened) + WEEKDAY(DateClosed) + 1, 1) AS TotalResolutionTimeBusinessDays
FROM table1
This is a complex calculation based on a matrix:
| M T W T F S S
-|--------------
M| 0 1 2 3 4 5 5
T| 5 0 1 2 3 4 4
W| 4 5 0 1 2 3 3
T| 3 4 5 0 1 2 2
F| 2 3 4 5 0 1 1
S| 0 1 2 3 4 0 0
S| 0 1 2 3 4 5 0
In the matrix, the intersection of any given x and y value pair (WEEKDAY(#S) and WEEKDAY(#E) yields the difference in work days between the two values. The 49 values in the table are concatenated into the following string: 0123455501234445012333450122234501101234000123450.
This works fine for me but now I need to present difference between dates in another format. E.g.: Let's say we have two dates: StartDate = '2013-06-28 01:27:35' and EndDate = '2013-07-08 16:47:21'. If we use method described above we get 7 working days which is correct. But I need to count all the difference between dates(including hours and minutes) so it could look like
SELECT TIME_TO_SEC(TIMEDIFF('2013-07-08 16:47:21','2013-06-28 01:27:35')) / 3600 / 24
which without weekends should be value like 7.64 days.
Any suggestions how to write a calculation based on that format? Any help would be much appreciated.
Related
i have a column that has month number stored as integer. i would like to create a new column with the quater of each month. is there an simpler way of doing the same, i have to use this in multiple places. so just wanted to check if there is any simpler way of doing this.
currently i am using below to achieve it
case when tt1.resolved_month in (1,2,3) then 1
when tt1.resolved_month in (4,5,6) then 2
when tt1.resolved_month in (7,8,9) then 3
when tt1.resolved_month in (10,11,12) then 4
end as quater
i checked the Quarter function and it doesnt support number as input.
CEIL function will get the quarter.
SELECT CEIL(resolved_month / 3) AS quarter
Demonstration:
Month Month/3 Quarter
-------------------------
1 0.3333 1
2 0.6667 1
3 1 1
4 1.3333 2
5 1.6667 2
6 2 2
7 2.3333 3
8 2.6667 3
9 3 3
10 3.3333 4
11 3.6667 4
12 4 4
Working Fiddle
Instead of a case expression, you could divide the month by 3 and ceil the result:
SELECT CEIL(resolved_month / 3) AS quarter
FROM tt1
As #exudong suggested in the comments, it may be a good idea to encapsulate this logic in a function and re-use it wherever needed.
You could use the QUARTER() function:
SELECT
resolved_month,
QUARTER(ADDDATE('2021-01-01',INTERVAL resolved_month-1 MONTH)) as quarter2
FROM months;
Fiddle
The advantage (😉) is that everybody who read this will recognize that the quarter is being calculated.
There is mysql Ver 8.0.18 value_table as:
value count
1 3
11 1
12 2
22 5
31 1
34 3
35 1
40 3
46 7
What is query to get a total count for each dozen (1-10 - first dozen,11-20 - second , etc..)
as:
1 3
2 3
3 5
4 8
5 7
Query should be flexible, so when some records added to value_table , for example
51 2
62 3
so, it is not necessary to change a query by adding new range (51-60 - 6-th dozen, etc.)
I think you just want division and aggregation:
select min(value), sum(count)
from t
group by floor(value / 10);
To be honest, I'm not sure if the first column should be min(value) or floor(value / 10) + 1.
I've ran into a little difficulty when trying to filter top N results for a table.
Assume the following table:
ID, X, Y, Result0, Result1
-------------------------------
0 0 0 1 4
1 0 1 2 5
2 0 1 1 4
3 0 2 2 5
4 0 3 0 1
5 1 3 3 4
6 1 3 2 5
7 1 3 4 6
So, let's say I want to get the top 2 results for the highest Result0 value, using Result1 as a tie breaker if the Result0 values are equal, and having only distinct values for (X,Y),
if I'll run the following query:
$result = DB::table('table')
->orderBy('Result1', 'DSC')
->orderBy('Result0', 'DSC')
->take(300)
->get();
This code will return IDs 5,7, because they have the highest Result0 values, but the X,Y for these fields are identical, and I'd like to get only top result for distinct X,Y values.
I tried adding a
->groupBy('X','Y')
But it grouped the entries based on the database order of the entries (i.e the ID) rather than my sorting of that table.
Anyone has any idea how can I achieve my goal?
I've got an issue where I've been presented data in this format from SQL, and have directly imported that into SSRS 2008.
I do have access to the stored procedure for this report, however I don't want to change it as a few other reports rely on it.
Project HoursSpent Cost
1 5 45
1 8 10
1 7 25
1 5 25
2 1 15
2 3 10
2 5 15
2 6 10
3 6 10
3 4 5
3 4 10
3 2 5
I've been struggling all morning to understand how/when I should be implementing the SUM() function with this.
I have tried already to SUM() the rows, but it still outputs the above result.
Should I be adding any extra groups?
Ideally, I need to have the following output:
Project HoursSpent Cost
1 25 105
2 15 40
3 16 30
EDIT: Here is my current structure:
"LineName" is a group for each project
You have to add a row group on "Project" since you want to sum up the data per each project.
I have one employee table which contains:
emp id Sum
------ ---
1 7
2 6
I want a SQL query for getting the quotient and remainder when dividing the Sum with 8.
Use integer division and mod operators to get the quotient and remainder:
SELECT
emp_id,
sum,
sum / 8 AS Result,
sum div 8 AS Quotient,
sum mod 8 AS Remainder
FROM employee
emp_id sum Result Quotient Remainder
1 7 0.8750 0 7
2 6 0.7500 0 6
3 9 1.1250 1 1
4 10 1.2500 1 2
5 11 1.3750 1 3
6 12 1.5000 1 4
7 13 1.6250 1 5
8 14 1.7500 1 6
9 15 1.8750 1 7
10 16 2.0000 2 0
What will be the return type of your qoutient? If you don't care if its a floating point or an integer(whole number). You can try this.
SELECT
(sum / 8) AS qoutient,
(sum % 8) AS reminder
FROM employee
You can use the mysql function DIV to get the qoutient
(http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_div) :
SELECT 14 DIV 3
will return 4
It should do the trick
As for the remainder, others have replied
you can use the % operator to get the remainder. Here's an example.
SELECT Round(17 / 4) -- quotient without decimal
SELECT 17 % 4 -- remainder
For my PL/SQL function I usually use:
SELECT trunc(sum/8) --- Quotient
SELECT mod(sum/8) -- Remainder
use floor function, select floor(column_name/divisor) from dual