I am totally new to SQL and am trying to do some on-the-job training exercises which are currently kicking my rear end.
I am trying to write a query that will look at a table column containing minutes called CC_To_Dep and measure that column against some buckets: 0-30Mins, 30-60Mins, 1-2Hrs, Over 2 Hrs and then return the data to look something like this
Case Count Time
698,523 0-30 Mins
235,888 30-60 Mins
50,853 1-2 Hrs
2,855 Over 2 Hrs
This is what I am using and it labels each record with the time buckets above but it doesn't group them together. I tried a GROUP BY clause at the end but it but then the whole thing errored out.
SELECT
(CC_To_Dep) as CaseCount
,CASE
WHEN(CC_To_Dep)< 30 THEN '0-30Mins'
WHEN(CC_To_Dep)>= 30 AND (CC_To_Dep)<= 60 THEN '30-60Mins'
WHEN(CC_To_Dep)> 60 AND (CC_To_Dep)<= 120 THEN '1-2Hrs'
ELSE 'Over 2 Hrs'
END as Time
FROM Table1
WHERE YEAR(Arr_date)=2013
Any help is appreciated. I have looked around for answers but it is really hard for me to read code in other examples and then apply it to my situation. I'll get there one day but today is not that day.
SELECT
CASE
WHEN(CC_To_Dep)< 30 THEN '0-30Mins'
WHEN(CC_To_Dep)>= 30 AND (CC_To_Dep)<= 60 THEN '30-60Mins'
WHEN(CC_To_Dep)> 60 AND (CC_To_Dep)<= 120 THEN '1-2Hrs'
ELSE 'Over 2 Hrs'
END as Time
, count(*) as caseCount
FROM Table1
WHERE YEAR(Arr_date)=2013
group by
CASE
WHEN(CC_To_Dep)< 30 THEN '0-30Mins'
WHEN(CC_To_Dep)>= 30 AND (CC_To_Dep)<= 60 THEN '30-60Mins'
WHEN(CC_To_Dep)> 60 AND (CC_To_Dep)<= 120 THEN '1-2Hrs'
ELSE 'Over 2 Hrs'
END
Related
i have a load of data measured over a year, multiple times an hour and i want to sum those data up by an hour and then sum all columns. i'm doing it in ACCESSS and i just don't know how to make the script work in this idiotic software. I'm working on this already over few hours, and without proper debug it is like finding needle in haystack, and if some script works normally in sql it just won't in the Access, and every tutorial or document i found would every time lead me on how to calculate the time difference. So if anyone knows how to do this i would be very grateful.
The table looks like this
Time Column1 Column2
12.12.2014 5:20 0 0
12.12.2014 5:30 15 5
12.12.2014 5:45 50 12
12.12.2014 5:55 120 8
12.12.2014 6:25 300 25
12.12.2014 6:40 240 50
and i want it to look like this
Date Hour SUM(Column1) SUM(Column2) Summary
12.12.2014 6 185 25 210
12.12.2014 7 540 75 615
Tried doing it through DatePart, but it always fail.
You won't need a script but a query (air code) like this:
Select
DateValue([Time]) As [Date],
Hour(-Int(-TimeValue([Time]) * 24) / 24) As [Hour],
Sum([Column1]) As Total1,
Sum([Column2]) As Total2,
Sum([Column1]) + Sum([Column2]) As Summary
From
YourTable
Group By
DateValue([Time]),
Hour(-Int(-TimeValue([Time]) * 24) / 24)
I have a select statement that pulls the following information:
Site Date Scheduled Arrived yesterday
A 3/26 45 51
A 3/25 40 37
A 3/24 60 55
I need the results to look like this:
Site Date Scheduled Arrived yesterday
A 3/26 45 NULL
A 3/25 40 51
A 3/24 60 37
I am fairly new to sql and am not sure how to select the data to make the values in the arrived yesterday column shift to the previous day without affecting the other columns. I have tried things like dateadd, but Im not sure what to do.
Use lead():
select t.*,
lead(arrived_yesterday) over (partition by site order by date) as next_arrived_yesterday
from t;
I need random interval time between 0 and (10 days and 5 hours).
My code:
select random() * (interval '10 days 5 hours')
from generate_series(1, 50)
It works like should, except a few strange results, like:
0 years 0 mons 7 days 26 hours 10 mins 1.353353 secs
The problem is 26 hours, it shouldn't be more than 23. And I never get 10 days, what I'd like to.
Intervals in Postgres are quite flexible, so hour values of greater than 23 do not necessarily roll over to days. Use jusify_interval() to return them to the normal "days" and "hours"."
So:
select justify_interval(random() * interval '10 day 5 hour')
from generate_series(1, 200)
order by 1 desc;
will return values with appropriate values for days, hours, minutes, and seconds.
Now, why aren't you getting intervals with more than 10 days? This is simple randomness. If you increase the number of rows to 200 (as above), you'll see them (in all likelihood). If you run the code multiple times, sometimes you'll see none in that range; sometimes you'll see two.
Why? You are asking how often you get a value of 240+ in a range of 245. Those top 5 hours account for 0.02% of the range (about 1/50). In other words a sample of 50 is not big enough -- any given sample of 50 random values is likely to be missing 1 or more 5 hour ranges.
Plus, without justify_interval(), you are likely to miss those anyway because they may show up as 9 days with an hours component larger than 23.
Try this:
select justify_hours(random() * (interval '245 hours'))
FROM generate_series(1, 50)
See Postgres Documentation for an explanation of the justify_* functions.
One option would be to use an interval of one hour, and then multiply by the random number between 0 and 1 coming from the series:
select random() * 245 * interval '1 hour'
from generate_series(1, 50);
I can see that the other answers suggest using justify_interval. If you just want a series of intervals between 0 and 245 hours (245 hours corresponding to 10 days and 5 hours), then my answer should suffice.
I have a table where each record contains the time, represented by
an hour column (int) and a minute column (int).For example:
**records |hours| mins**
record1 | 15 | 30
record2 | 12 | 25
I want to write a statement where only the records ahead of the current time will be displayed. So far, I have:
SELECT...
WHERE hours >= hour(current_time)
AND
mins >= minute(current_time)
AND...
But this doesn't work because it needs both the hours and mins to be greater than the current hours and minutes. How do I write to that so that if the hours are the same, then the minutes are compared?
Do something like this:
WHERE hours >= hour(current_time)
OR (hours = hour(current_time) AND mins >= minute(currentime))
Something like
Where hours * 60 + mins > hours(currentTime) * 60 + minute(currentTime)
I have a time usage amount in an Oracle table stored as a number of seconds:
5
10
100
500
How can I convert the number of seconds to the number of minutes, rounded up to the next closest minute? For the seconds values above I want to get:
1
1
2
9
Assuming you are actually using Oracle, you can use the CEIL function to 'round up' the fractional number of minutes you get by dividing by 60 to the next integer.
ceil(your_column/60)
So with some sample values:
select seconds, ceil(seconds/60) as minutes
from your_table
order by seconds;
SECONDS MINUTES
---------- ----------
5 1
10 1
100 2
500 9
1800 30
You're trying to divide it by 60 and "trunc up". Unfortunately there isn't a built-in function that does exactly that, but trunc can still be used:
SELECT CASE TRUNC(myseconds/60) WHEN myseconds/60 THEN myseconds/60
ELSE TRUNC(myseconds/60) + 1
END
FROM my_table
EDIT:
Or in a more elegant form with a subquery:
SELECT CASE TRUNC(myminutes) WHEN myminutes THEN myminutes
ELSE TRUNC(myminutes) + 1
END
FROM (SELECT myseconds/60 AS myminutes
FROM myable)