This is a follow-up to a similar post...
I have the following dataset that looks like this. I'd like to select only those who have a "1" for the ESITwoToFive variable and then compute the AVG total_ED_LOS for each unique date. I'd also like to do the same for the ESIFourFive variable.
ID CheckinDate ESITwoToFive ESIFourFive Total_ED_Los
1 Feb 7 1 0 23
2 Feb 7 0 1 23
3 Feb 7 1 0 28
4 Feb 8 1 0 43
5 Feb 8 1 0 83
6 Feb 8 0 1 29
7 Feb 8 0 1 32
8 Feb 9 1 0 93
9 Feb 9 1 0 77
10 Feb 9 0 1 33
I was kindly given the following code to use to compute for the ESITwoToFive variable, which works.
select t.checkin_date, avg(t.total_ed_los) as [Avg LOS]
from [Fast Track Quality Research ESI Levels] t
where t.esitwotofive = 1
group by t.checkin_date
Desired output:
Checkindate Avg LOS for ESITwoToFive Avg LOS for ESIFourFive
Feb 7 24 23
Feb 8 54 30
Feb 9 86 56
The conditional is likely to be the cleanest solution, but just to offer an alternative without conditionals:
select
t.checkin_date,
sum(t.total_ed_los * t.esitwotofive) / sum(t.esitwotofive),
sum(t.total_ed_los * t.esifourfive) / sum(t.esifourfive),
from
[Fast Track Quality Research ESI Levels] t
group by
t.checkin_date
Try using IIf() conditional expression:
SELECT t.checkin_date, Avg(IIf(ESITwoToFive=1,t.total_ed_los,Null)) AS [AvgLOStwo],
Avg(IIf(ESIFourFive=1,t.total_ed_los,Null)) as [AvgLOSfour]
FROM [Fast Track Quality Research ESI Levels] t
GROUP BY t.checkin_date
Related
I have the following dataset that looks like this. I'd like to select only those who have a "1" for the ESITwoToFive variable and then compute the average total_ED_LOS for each unique dates. For example, the average for Feb 7th would only have the values from ID's 1 and 3.
ID CheckinDate ESITwoToFive Total_ED_Los
1 Feb 7 1 23
2 Feb 7 0 23
3 Feb 7 1 28
4 Feb 8 1 43
5 Feb 8 1 83
6 Feb 8 0 29
7 Feb 9 1 93
8 Feb 9 1 77
9 Feb 9 0 33
I'm using the following syntax but my output contains the same averages for every date.
SELECT [Fast Track Quality Research ESI Levels].checkin_date,
DAvg("total_ed_los","Fast Track Quality Research ESI Levels","ESITwoToFive =
'1'") AS Expr1
FROM [Fast Track Quality Research ESI Levels]
GROUP BY [Fast Track Quality Research ESI Levels].checkin_date;
Output that i'm getting...
Checkindate Avg LOS
Feb 7 24
Feb 8 24
Feb 9 24
Desired output....
Checkindate Avg LOS
Feb 7 24
Feb 8 54
Feb 9 86
I would suggest using the Avg function rather than the DAvg domain aggregate function, e.g.:
select t.checkin_date, avg(t.total_ed_los) as [Avg LOS]
from [Fast Track Quality Research ESI Levels] t
where t.esitwotofive = 1
group by t.checkin_date
The DAvg function in your example is calculating the average over all records for which ESITwoToFive = '1' before the data is aggregated by date, therefore you receive the same result for every record output by the query.
I want to select fields dynamically based on financial year (i.e., for the year of Apr'18 to Mar'19).
I have data in a table exact as given below :
ROWNO YEAR MONTH
1 2016 1
2 2016 2
3 2016 6
4 2017 7
5 2017 5
6 2018 4
7 2018 5
8 2018 6
9 2018 7
10 2018 8
11 2018 9
12 2018 10
13 2018 11
14 2018 12
15 2019 1
16 2019 2
17 2019 3
18 2019 3
19 2017 4
20 2017 1
21 2017 2
22 2018 3
I want to get the result as shown below for the financial year 2018-19 are
ROWNO YEAR MONTH
6 2018 4
7 2018 5
8 2018 6
9 2018 7
10 2018 8
11 2018 9
12 2018 10
13 2018 11
14 2018 12
15 2019 1
16 2019 2
17 2019 3
I used Query
SELECT * FROM rup_calendar WHERE YEAR BETWEEN YEAR(CURDATE()) AND YEAR(CURDATE())+1;
but not able to get exact result.
Please give me the query to get the result as given above.
Given the data you have your approach is not far away
select rowno,year,month
from t
where year * 100 + month between
case when month(now()) < 4 then (year(now()) - 1) * 100 + 3
else year(now()) * 100 + 4
end
and
case when month(now()) < 4 then (year(now())) * 100 + 3
else (year(now()) + 1) * 100 + 4
end
Notice I have calulated a yearmonth field to ease comparison. Note I haven't fully tested this and you should do so by substituting an # variable for now() to test.
I'm trying to display zero on missing month, but didn't succeed.
Table:
clicks | impressions | ctr | position | month | year
111 2709 4 20 3 2015
101 2695 3 20 6 2015
76 2714 2 21 7 2015
.
.
.
64 1212 4 25 11 2015
81 1905 4 24 12 2015
Required output:
clicks | impressions | ctr | position | month | year
0 0 0 0 1 2015
0 0 0 0 2 2015
111 2709 4 20 3 2015
0 0 0 0 4 2015
0 0 0 0 5 2015
101 2695 3 20 6 2015
.
.
.
64 1212 4 25 11 2015
81 1905 4 24 12 2015
Like #jarlh suggested ,you can do it like this: creating a 'table' that contains all month availabe(you will have to populate it your self, add which month and years you want) and then left join to the original table and when value not exists, put 0.
select coalese(s.clicks,0) as clicks,
coalese(s.impressions ,0) as impressions,
coalese(s.ctr ,0) as ctr ,
coalese(s.position ,0) as position ,
t.month,
t.year
from(
SELECT 1 as month_num,2015 as year_num
union SELECT 2,2015
union select 3,2015
union select 4,2015 ....) t
LEFT OUTER JOIN YourTable s
ON(t.month_num = s.month and t.year_num = s.year)
I have table like
id userid semid courseid coursename total
1 36 17 13 CA 23
2 36 17 5 CB 46
3 36 17 8 CC 20
4 36 19 16 CD 34
5 36 19 13 CA 31
6 36 19 3 CA# 29
7 36 19 7 CE 60
8 36 10 9 CK 32
9 36 10 15 CH 56
I need average of semid for a userid i.e., SUM(courseid) /count (moduleid), It was showing 9 as module count, but I have only 3 modules.
This is my query
SELECT userid, SUM(total)/count(semid) FROM custom WHERE userid=36
just use the AVG( ) function
SELECT userid, semid, AVG(total)
FROM custom
WHERE userid = 36
GROUP BY userid, semid
SQLFiddle Demo
SELECT userid, SUM(total)/count(distinct semid) FROM custom WHERE userid=36
Try this query
There is MYSQL aggregate function AVG() for finding Average . #John Totet Woo has posted the answer.
MyTable is:
tday char(3)
dow int(1)
tdate date
ttime time
T0 decimal(5,2)
T1 decimal(5,2)
T2 decimal(5,2)
T3 decimal(5,2)
Total decimal(8,2)
Given the above structure and following data:
DoW IM CDate CTime G0 G1 G2 G3
Thu 4 2010-12-02 05:29:15 1 3 0 11
Thu 4 2010-12-02 05:34:22 0 4 1 0
Thu 4 2010-12-02 05:39:28 6 0 7 0
Thu 4 2010-12-02 05:44:35 8 7 3 9
Thu 4 2010-12-02 05:49:41 0 7 0 8
Thu 4 2010-12-02 05:54:48 1 7 0 1
Thu 4 2010-12-02 05:59:54 3 0 5 3
Thu 4 2010-12-02 06:05:01 0 9 1 0
Thu 4 2010-12-02 06:10:07 2 9 10 0
Thu 4 2010-12-02 06:15:13 0 0 16 0
Thu 4 2010-12-02 06:20:20 2 0 8 0
Thu 4 2010-12-02 06:25:26 1 0 0 0
Thu 4 2010-12-02 06:30:33 1 5 19 21
Thu 4 2010-12-02 06:35:39 3 0 6 0
Thu 4 2010-12-02 06:40:46 3 4 7 2
Thu 4 2010-12-02 06:45:52 3 3 0 20
Thu 4 2010-12-02 06:50:58 5 0 7 10
Thu 4 2010-12-02 06:56:05 0 7 7 0
Thu 4 2010-12-02 07:01:11 7 0 10 3
Thu 4 2010-12-02 07:06:18 0 0 0 1
Thu 4 2010-12-02 07:11:24 0 0 0 0
Thu 4 2010-12-02 07:16:31 9 3 1 0
Thu 4 2010-12-02 07:21:37 0 0 3 10
Thu 4 2010-12-02 07:26:44 0 1 3 0
What I am trying to get is the sum of columns G0..G3 by the Hour per Day.
I have tried "Select CTime, sum(G0), sum(G1), sum(G2), sum(G3) from MyTable group by CTime order by CDate;" and various like statements without getting any success. The results I am trying to get is as follows:
DoW IM CDate CTime G0 G1 G2 G3
Thu 4 2010-12-02 05:00:00 19 28 16 32
Thu 4 2010-12-02 06:00:00 20 51 86 65
Thu 4 2010-12-02 07:00:00 16 4 17 14
Could you kindly assist. I have spent considerable time looking at already submitted solutions and other answers on the web but nothing comes close to what I think I need in the particular SQL statement required.
Thanks. Grahame
I think you're looking for something like
SELECT DoW, IM, CDate, HOUR(CTime) AS recorded_hour, SUM(G0) AS sum_0,
SUM(G1) AS sum)1, SUM(G2) AS sum_2, SUM(G3) AS sum_3
FROM MyTable
GROUP BY recorded_hour
If you want to have the hour for different dates to be distinct, then you just have to add the date column to the group by clause
What type of CTime and CDate is?
Could your past here normal table structure in pre&code
Try to run this:
Select CDate, HOUR(CTime), sum(G0), sum(G1), sum(G2), sum(G3) from MyTable group by CDate, HOUR(CTime) order by CDate, CTime
HOUR function