AVG of the last 7 days not returning results - qlikview

I have an expression to give me the AVG of TIMETAKEN in the last 7 days but it isn't returning any results.
=AVG({<STARTDATE={"<$(=date(today()-7))"} >}TIMETAKEN)
I can get yesterdays AVG with this and it works:
=AVG({<STARTDATE={"$(=Date(Today()-1))"}>} TIMETAKEN)

Try this
count({<Date={"=Date>(today()-7)"}>}[Date])
Extra credit for
count({<Date={"=Date<(today())"}*{"=Date>(today()-7)"}>}[Date])

I used the following expression to achieve this:
=AVG({<STARTDATE=
{"$(=Date(Today()-7))","$(=Date(Today()-6))","$(=Date(Today()-5))",
"$(=Date(Today()-4))","$(=Date(Today()-3))","$(=Date(Today()-2))",
"$(=Date(Today()-1))"}>}TIMETAKEN)

Related

Year wise Average days SQL

Today i have below problem while perform an sql query. Please find below data.
I perform SQL query on my table and get the below resulted output. i perform Group by on ID, Name, Week, Year, Days now i want the Days column as average of All Days based on year column. means there is multiple value of year is exist so i need Avg of Days data in all rows of DAYS for particular row. expected result as per below.
Thanks in Advance!!!
Write in comment if you have any query.
You can use OVER:
SELECT
*,
AVG(Days) OVER (PARTITION BY LEFT(Year, 4)) AvgDays
FROM
Tbl
Note: Just grouped by year (2016)

Postgresql column must appear in the GROUP BY clause or be used in an aggregate function

I'm trying to write a script which will take hourly averages of time Series Data. I.e. I have 5 years worth of data at 5 minute intervals, but I need 5 years of hourly averages. Postgresql seems to be complaining about a missing column in the group by clause which is already in there? What schoolboy error am I making here?
SELECT
time_audit_primary.clockyear,
time_audit_primary.clockmonth,
time_audit_primary.clockday,
time_audit_primary.clockhour,
AVG (time_audit_primary.time_offset) AS timeoffset,
AVG (time_audit_primary.std_dev) AS std_dev
FROM tempin.time_audit_primary
GROUP BY (time_audit_primary.clockyear,
time_audit_primary.clockmonth,
time_audit_primary.clockday,
time_audit_primary.clockhour)
It's the parenthesis around the group by columns that is causing the error. Just remove them:
SELECT
time_audit_primary.clockyear,
time_audit_primary.clockmonth,
time_audit_primary.clockday,
time_audit_primary.clockhour,
AVG (time_audit_primary.time_offset) AS timeoffset,
AVG (time_audit_primary.std_dev) AS std_dev
FROM tempin.time_audit_primary
GROUP BY time_audit_primary.clockyear,
time_audit_primary.clockmonth,
time_audit_primary.clockday,
time_audit_primary.clockhour

Running a complex loop query in PostgreSQL

I have one problem in PostgreSQL.
This is my table (this table does not showing all data in image).
What is my requirement is:
Step 1 : find count of value (this is a column in table) Order by value for today date. So it will be like this and I did it.
Step 2 : find count of value for last 30 days starting from today. I am stuck here. Also one another thing is included in this step --
Example : today has 10 count for a value - kash, this will be 10x30,
yesterday had 4 count for the same value , so will be 4x29, so the total sum would be
(10x30) + (4x29) = 416.
This calculation is calculated for each and every value.
This loop execute for 30 times (as I said before last 30 days starting from today). Take today as thirtieth day.
Query will just need to return two columns with value and sum, ordered by the sum.
Add a WHERE clause to your existing query:
WHERE Timestamp > current_date - interval '30' day;
As far as ordering by the sum, add an ORDER BY clause.
ORDER BY COUNT(*) DESC.
I do not believe that you will need a loop (CURSOR) for this query.

SQL: How can I find the total and average value in one SQL statement?

I would like to calculate the total amount of expenditure and the calculate the average value from the number of rows like this:
SELECT AVG(SUM(expenditure)) from INCOME;
However, there is a error say "misuse of aggregate function sum()"
how can I achieve this?
You can't calculate the average of the total sum as there is only one total.
The average AVG() function already calculates the total as part of its logic.
This is what you want:
SELECT AVG(expenditure) as AverageExpenditure,
SUM(expenditure) as TotalExpenditure
from INCOME;
SELECT AVG(expenditure) AS avg_exp, SUM(expenditure) AS sum_exp FROM INCOME;

Query to find a weekly average

I have an SQLite database with the following fields for example:
date (yyyymmdd fomrat)
total (0.00 format)
There is typically 2 months of records in the database. Does anyone know a SQL query to find a weekly average?
I could easily just execute:
SELECT COUNT(1) as total_records, SUM(total) as total FROM stats_adsense
Then just divide total by 7 but unless there is exactly x days that are divisible by 7 in the db I don't think it will be very accurate, especially if there is less than 7 days of records.
To get a daily summary it's obviously just total / total_records.
Can anyone help me out with this?
You could try something like this:
SELECT strftime('%W', thedate) theweek, avg(total) theaverage
FROM table GROUP BY strftime('%W', thedate)
I'm not sure how the syntax would work in SQLite, but one way would be to parse out the date parts of each [date] field, and then specifying which WEEK and DAY boundaries in your WHERE clause and then GROUP by the week. This will give you a true average regardless of whether there are rows or not.
Something like this (using T-SQL):
SELECT DATEPART(w, theDate), Avg(theAmount) as Average
FROM Table
GROUP BY DATEPART(w, theDate)
This will return a row for every week. You could filter it in your WHERE clause to restrict it to a given date range.
Hope this helps.
Your weekly average is
daily * 7
Obviously this doesn't take in to account specific weeks, but you can get that by narrowing the result set in a date range.
You'll have to omit those records in the addition which don't belong to a full week. So, prior to summing up, you'll have to find the min and max of the dates, manipulate them such that they form "whole" weeks, and then run your original query with a WHERE that limits the date values according to the new range. Maybe you can even put all this into one query. I'll leave that up to you. ;-)
Those values which are "truncated" are not used then, obviously. If there's not enough values for a week at all, there's no result at all. But there's no solution to that, apparently.