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.
Related
I am trying to find the last entry for the previous years quarter.
All I can access is year i.e 2021 and quarter i.e 1
Here is the data in my database:
id
name
start
end
16
April 2021
2021-04-01
2021-04-30
15
March 2021
2021-03-01
2021-03-31
14
February 2021
2021-02-01
2021-02-28
57
November 2020
2020-11-01
2020-11-30
55
October 2020
2020-10-01
2020-10-31
29
September 2020
2020-09-01
2020-09-30
27
July 2020
2020-07-01
2020-07-31
24
April 2020
2020-04-01
2020-04-30
23
March 2020
2020-03-01
2020-03-31
22
February 2020
2020-02-01
2020-02-29
21
January 2020
2020-01-01
2020-01-31
Using the MySQL quarter function I can get it to print out the quarter as an integer in another column:
SET #given_year = 2021;
SET #given_quarter = 1;
SELECT
id, name, start, end, QUARTER(end) as "Q"
FROM
submissions
id
name
start
end
Q
16
April 2021
2021-04-01
2021-04-30
2
15
March 2021
2021-03-01
2021-03-31
1
14
February 2021
2021-02-01
2021-02-28
1
57
November 2020
2020-11-01
2020-11-30
4
55
October 2020
2020-10-01
2020-10-31
4
29
September 2020
2020-09-01
2020-09-30
3
27
July 2020
2020-07-01
2020-07-31
3
24
April 2020
2020-04-01
2020-04-30
2
23
March 2020
2020-03-01
2020-03-31
1
22
February 2020
2020-02-01
2020-02-29
1
21
January 2020
2020-01-01
2020-01-31
1
I tried using WHERE and LIKE but it is returning 0 rows:
SELECT * FROM (
SELECT
id, name, start, end, QUARTER(end) as "Q"
FROM
submissions as s
) AS vs
WHERE
vs.end
LIKE
#given_year
AND
vs.Q < #given_quarter
I also need to account for the possibility that there may be no rows this year and I need to find the previous year.
For example with these two rows, if I was passed the year 2021 and quarter 1 I would need to return November of the previous year and a different quarter.
id
name
start
end
Q
14
February
2021
2021-02-01
2021-02-28
57
November
2020
2020-11-01
2020-11-30
If I understand correctly, you want all the rows from the quarter in the data before a given quarter. You can filter and use dense_rank():
select s.*
from (select s.*,
dense_rank() over (order by year(start) desc, quarter(start) desc) as seqnum
from submissions s
where year(start) < #given_year or
(year(start) = #given_year and quarter(start) < #given_quarter)
) s
where seqnum = 1;
The above returns all rows from the previous quarter (which is what I thought you wanted). If you want only one row:
select s.*
from submissions s
where year(start) < #given_year or
(year(start) = #given_year and quarter(start) < #given_quarter)
order by start desc
limit 1;
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
I want to make a report with MYSQL from this data
Table name: table
date name Quantity
13 Nov Rudy 40
13 Nov Mery 30
13 Nov Rudy 20
13 Nov John 10
14 Nov Rudy 20
14 Nov Rudy 30
I want the result like this
13 Nov Rudy 60
13 Nov Mery 30
13 Nov John 10
14 Nov Rudy 50
What query should I do?
thanks
I'm not sure if mysql has support for 2 columns in group by clause, so grouping by date + user should work. but if it has, you can skip this trick and replace this clause with (group by date name) as #RaymondNijland proposed.
select date, name, sum(quantity) as total
from your_table
group by concat(date, "_", name);
i have a report with parameter is the year i can select a multiple value
year
Data 2010 2011 2012
hp 14 25 30
Dell 17 18 20
what i need when i create the report i want to have another column with the last value concatenation with the last previous one like this :
year
Data 2010 2011 2012 2012/2011
hp 14 25 30 3025
Dell 17 18 20 2018
i tried this :
=Last(Field!Qte.value)+Last(Field!Qte.value)-1
but i had the wrong result i got this result :
year
Data 2010 2011 2012 2012/2011
hp 14 25 30 3029
Dell 17 18 20 2019
the problem is in this function
Last(Field!Qte.value)-1
it doesn't return the last previous value
I'm looking to get a list of the first and last business days of the month.
Its basically a list of business days:
2009-01-03
2009-01-04
2009-01-05
...
I just want to get a list of the first and last days, basically and max and min day(date) for each year-month combination.
Any suggestions?
Your question states that you already have a list of business days and that you need a way of finding the minimum and maximum for each year-month combination.
You can use ddply in package plyr to do this. I also make use of package lubridate because it has some convenience functions to extract the year and month from a date.
Create some data:
library(lubridate)
x <- sample(seq(as.Date("2011-01-01"), by="1 day", length.out=365), 100)
df <- data.frame(date=x, year=year(x), month=month(x))
Now extract the min and max for each month:
library(plyr)
ddply(df, .(year, month), summarize, first=min(date), last=max(date))
year month first last
1 2011 1 2011-01-03 2011-01-30
2 2011 2 2011-02-03 2011-02-19
3 2011 3 2011-03-06 2011-03-29
4 2011 4 2011-04-09 2011-04-30
5 2011 5 2011-05-01 2011-05-29
6 2011 6 2011-06-04 2011-06-28
7 2011 7 2011-07-02 2011-07-29
8 2011 8 2011-08-10 2011-08-30
9 2011 9 2011-09-01 2011-09-28
10 2011 10 2011-10-07 2011-10-31
11 2011 11 2011-11-01 2011-11-28
12 2011 12 2011-12-01 2011-12-30