I have a table like this
Day Operation Times Condition
Monday addition 3 done
Monday subtraction 2 done
Monday subtraction 4 uncomplete
Tuesday multiplication 3 done
Tuesday addition 1 done
Tuesday addition 8 uncomplete
Wednesday subtraction 1 uncomplete
And I want to transform the Condition column in two distinct columns something like
Day Operation Done Uncomplete Total
Monday addition 3 0 3
Monday subtraction 2 4 6
Tuesday multiplication 3 0 3
Tuesday addition 1 8 9
Wednesday subtraction 0 1 1
Fusioning the Done and Uncomplete Operations in a single row
Is there a way to do it in SQL?
So far I have tried something like this Creating multiple columns from one column using case. I think than I may need to group it first by day, and then by Operation.
Considering the fact that you do this on Microsoft Access please refer to the following tutorial: https://www.youtube.com/watch?v=ZgerpTHzQes
One method is conditional aggregation:
select day, operation,
sum(case when condition = 'done' then times else 0 end) as dones,
sum(case when condition = 'uncomplete' then times else 0 end) as uncompletes,
sum(times) as total
from t
group by day, operation;
You need to tag your question with the database you are using. The equivalent in MS Access is:
select day, operation,
sum(iif(condition = "done", times, 0)) as dones,
sum(iif(condition = "uncomplete", times, 0)) as uncompletes,
sum(times) as total
from t
group by day, operation;
Related
how to calculate the 2 days or 3 days total amounts in MySQL.
a sum of 3 days amounts and sum of 2 days amounts
how to write a query in MySql
I don't know how your relation looks like but if I get it right you want to SUM one column over the past 2 or 3 days which will be something like:
SELECT FORMAT(SUM(yourColumnName)
FROM tableName
WHERE entry.date >= CURDATE() - INTERVAL 2 days
For 3 days you have to set INTERVAL 3 days
This is a slightly adjusted version of the question here: Calculating Weekly Returns from Daily Time Series of Prices which was answered by #Scott Craner:
I want to calculate weekly returns of a mutual fund from a time series of daily prices. My data looks like this:
A B C D E
DATE WEEK W.DAY MF.PRICE WEEKLY RETURN
02/01/12 1 1 2,7587 -0,0108
03/01/12 1 2 2,7667
04/01/12 1 3 2,7892
05/01/12 1 4 2,7666
06/01/12 1 5 2,7391
09/01/12 2 1 2,7288 0,0067
10/01/12 2 2 2,6707
11/01/12 2 3 2,7044
12/01/12 2 4 2,7183
13/01/12 2 5 2,7619
16/01/12 3 1 2,7470 0,0511
17/01/12 3 2 2,7878
18/01/12 3 3 2,8156
19/01/12 3 4 2,8310
20/01/12 3 5 2,8760
23/01/12 4 1 2,8875
The date is (dd/mm/yy) format and "," is decimal separator. This would be done by using this formula: (Price for first weekday of next week - Price for first weekday of current week)/(Price for first weekday of current week). For example the return for the first week is (2,7288 - 2,7587)/2,7587 = -0,0108 and for the second is (2,7470 - 2,7288)/2,7288 = 0,0067.
The problem is that the list goes on for a year, and some weeks have less than five working days due to holidays or other reasons. Some weeks start with weekday 2, some end with weekday 3. So I can't simply copy and paste the formula above. I added the extra two columns for week number and week day using WEEKNUM and WEEKDAY functions, thought it might help. I want to automate this with a formula and hope to get a table like this:
WEEK RETURN
1 -0,0108
2 0,0067
3 0,0511
.
.
.
I'm looking for a way to tell excel to "find the prices that correspond to the min weekdays of two consecutive weeks and apply the formula "(Price for first weekday of next week - Price for first weekday of current week)/(Price for first weekday of current week)".
I would appreciate any help! (I have 5 separate worksheets for consecutive years, each with daily prices of 20 mutual funds)
It seems to me that you can generate your column E with this formula in E2 :
=IF(B2=B1, "", (VLOOKUP(1+B2, B3:D9, 3, FALSE) - D2)/D2)
It's a VLookup limited on the next 7 rows from each row that declares a new week.
Copying into all cells will give the result indicated in your first tableau. To transform this result into to the list (Week, Return) is a matter of a filter that hides blanks from E.
Notice that a problem could occur if the WeekNum restarts from one when a new year is reached, but since you say that each of your sheets is for one (calendar) year, it shouldn't happen.
I've executed the following case in a query:
WHEN phases is null and INSERTIONDATE is null and Priority = 1 and Complexity = 'minor' THEN trunc(CreationDate)+2
But I want to add +2 working days. How can I achieve that?
Example:
Lets say that we have an ID with insertion date NULL and priority 1 minor and creation date on Friday 15/01/2016.
The output should be:
115 prio1 minor acknowledge_date 19/01/2016
This is a total shot in the dark, but you mentioned you wanted to add two work days, and the example you gave (January 16 + 2 work days = January 19) tells me you mean you want to skip weekends. If this is the case, I think something like this might work:
select
case
when phases is null and
insertiondate is null and
priority = 1 and
complexity = 'minor' then
case to_char(creationdate, 'D')
when '5' then to_char(creationdate + 4, 'DD/MM/YYYY')
when '6' then to_char(creationdate + 4, 'DD/MM/YYYY')
when '7' then to_char(creationdate + 3, 'DD/MM/YYYY')
else to_char(creationdate + 2, 'DD/MM/YYYY')
end
end
from test
In essence, I'm doing a poor man's, brute-force business day calculation of "2 work days" by saying on Thursday and Friday, 2 business days = 4 calendar days, on Saturday, 2 business days = 3 calendar days, and all other days 2 days = 2 days.
It wouldn't shock me if this misses the mark, but maybe it's a starting point for clarification.
For the following create days, it returns the following results:
Creation date Result
1/15/2016 9:28:31 AM 19/01/2016
1/16/2016 9:41:49 AM 19/01/2016
1/17/2016 9:41:51 AM 19/01/2016
1/18/2016 9:41:52 AM 20/01/2016
1/19/2016 9:41:54 AM 21/01/2016
1/21/2016 9:42:00 AM 25/01/2016
Table:
DayOfWeek Enrollments
Monday 35
Monday 12
Saturday 25
Tuesday 15
Monday 9
Tuesday 15
Basically I'm trying to sum the total enrolments for each day.
so the Output will look like:
DayOfWeek Enrollments
Monday 56
Saturday 25
Tuesday 30
I've spent around 4 hours trying to work this out trying many many different ways but no luck.
The problem I'm having is i can count how many enrollments for each day but can't have it aligned with the correct day when i run the query e.g. I want The total to be on the same line as the day it was calculated from. (I hope that is clear enough)
Group by DayOfWeek, and ask for the sum of Enrollments within each group. The SQL will look like this.
SELECT DayOfWeek, Sum(Enrollments) AS SumOfEnrollments
FROM YourTable
GROUP BY DayOfWeek;
If you're using the Access query designer to create this, select your fields, then click the symbol for "Totals" query (Greek character sigma). In the "Total:" row of the design grid, select Group By and Sum for the appropriate fields.
I am using Oracle's to_char() function to convert a date to a week number (1-53):
select pat_id,
pat_enc_csn_id,
contact_date,
to_char(contact_date,'ww') week,
...
the 'ww' switch gives me these values for dates in January of this year:
Date Week
1-Jan-10 1
2-Jan-10 1
3-Jan-10 1
4-Jan-10 1
5-Jan-10 1
6-Jan-10 1
7-Jan-10 1
8-Jan-10 2
9-Jan-10 2
10-Jan-10 2
11-Jan-10 2
12-Jan-10 2
a quick look at the calendar indicates that these values should be:
Date Week
1-Jan-10 1
2-Jan-10 1
3-Jan-10 2
4-Jan-10 2
5-Jan-10 2
6-Jan-10 2
7-Jan-10 2
8-Jan-10 2
9-Jan-10 2
10-Jan-10 3
11-Jan-10 3
12-Jan-10 3
if I use the 'iw' switch instead of 'ww', the outcome is less desirable:
Date Week
1-Jan-10 53
2-Jan-10 53
3-Jan-10 53
4-Jan-10 1
5-Jan-10 1
6-Jan-10 1
7-Jan-10 1
8-Jan-10 1
9-Jan-10 1
10-Jan-10 1
11-Jan-10 2
12-Jan-10 2
Is there another Oracle function that will calculate weeks as I would expect or do I need to write my own?
EDIT
I'm trying to match the logic used by Crystal Reports. Each full week starts on a Sunday; the first week of the year starts on whichever day is represented by January 1st (e.g. in 2010, January 1st is a Friday).
When using IW, Oracle follows the ISO 8601 standard regarding week numbers (see http://en.wikipedia.org/wiki/ISO_8601). That is the same standard than the one we generally use in Europe here.
Your problem is also mentioned on the Oracle forum: http://forums.oracle.com/forums/thread.jspa?threadID=947291 and http://forums.oracle.com/forums/message.jspa?messageID=3318715#3318715. Maybe you can find a solution there.
I know this is old, but still a common question.
This should give you the correct results in the smallest amount of effort:
select pat_id,
pat_enc_csn_id,
contact_date,
to_char(contact_date + 1,'IW') week,
...
Since it looks like you are using your own special definition of the week number you'll need to write your own function.
It might be helpful that NLS_TERRITORY affects the day with which a week starts as used by the D Format Model
see also:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements004.htm#SQLRF00210
and
http://www.adp-gmbh.ch/ora/sql/to_char.html
Based on this question, How do I calculate the week number given a date?, I wrote the following Oracle logic:
CASE
--if [date field]'s day-of-week (e.g. Monday) is earlier than 1/1/YYYY's day-of-week
WHEN to_char(to_date('01/01/' || to_char([date field],'YYYY'),'mm/dd/yyyy'), 'D') - to_char([date field], 'D') > 1 THEN
--adjust the week
trunc(to_char([date field], 'DDD') / 7) + 1 + 1 --'+ 1 + 1' used for clarity
ELSE trunc(to_char([date field], 'DDD') / 7) + 1
END calendar_week