Aggregate function -Avg is not working in my sql query - sql

In my query I need to display date and average age:
SELECT (SYSDATE-rownum) AS DATE,
avg((SYSDATE - rownum)- create_time) as average_Age
FROM items
group by (SYSDATE-rownum)
But my output for average age is not correct. It's simply calculating/displaying the output of (SYSDATE - rownum)- create_time but not calculating the average of them though I use: avg((SYSDATE - rownum)- create_time).
Can someone tell me why the aggregate function AVG is not working in my query and what might be the possible solution

In the select clause you are using both an non-aggregate expression as wel as an aggregate expression. By dropping the (SYSDATE-rownum) AS DATE statemant you would generate an outcome over the whole data set. In that way the avg is calculated over the whole data set ... and not just per single record retrieve.
Then you might drop the group by too. In the end you just keep the avg statement
SELECT avg((SYSDATE - rownum)- create_time) as average_Age
FROM items

First you need to think on rows or group on which you need avg. this column will come in group by clause. as a simple thing if there is 5 rows with age of 20, 10, 20, 30 then avg will be (80/4=20) i.e. 20. so I think you need to fist calculate age by (sysdate - create_time).
eg.select months_between(sysdate,create_date)/12 cal3 from your_table
and then there will be outer query for avg on group.

Related

Hive query to get count from previous day

Table A
Year(String), Month(String), and Day(String) are partition columns.
I want to get the count for the previous day.
Note - here no Date formate like 2022-06-01 in any columns
I tried the below query.
Select count(*) FROM Table A where Day='03' and Month='06' and Year='2022' GROUP BY city;
But I don't want to hard-coded value.
I think I got the solution -
select count(*) from Table A where cast(concat_ws('-',year,month,day) as date)=date_sub(current_date, 1);
this is giving me the correct results.

Returning single records per month

I have a use case function that needs to returns a single row only for every end of month.
I tried using select distinct and it is showing multiple records for the same end of month
SELECT DISTINCT CASE
WHEN eff_interest_balance < 0.01 THEN trial_balance_date
WHEN date_paid < trial_balance_date THEN date_paid
END as A
, period
FROM dbo.Intpayments[enter image description here][1]
WHERE loan_number = 60023
ORDER BY period ASC
Each row should return single date for each month
Distinct is returning unique rows, not grouping them. You are looking to aggregate rows. This means using some combination of aggregate functions and group by.
What your current query is missing is some sort of logic for aggregating the rows that are in the same period. Do you want to compare the sum of these values? The min, the max?
In any case, the basic idea of aggregating and grouping would look like this - I don't think this summing is what you want, but the query shows the basic idea of aggregating and grouping:
SELECT
period
, SUM(eff_interest_balance) AS SumOfBalance
FROM dbo.Intpayments
WHERE loan_number = 60023
GROUP BY period

Calculating aggregated field

I'm back with more potgresql question :)
I have a query that shows the amount of time between when a plan was created and when a plan was updated.
select
...
EXTRACT(DAY FROM MAX(plans.updated_at) - MIN(plans.created_at)) AS DateDifference
...
Now I would like to exclude the results when the number of days is 0
So I tried:
WHERE EXTRACT(DAY FROM MAX(plans.updated_at) - MIN(plans.created_at)) > 0
But I get Aggregated functions are not allowed in where.
What would be the best way to accomplish this?
Thanks in advance
If your query does what you want, then you would use a having clause:
HAVING EXTRACT(DAY FROM MAX(plans.updated_at) - MIN(plans.created_at)) > 0
Conditions on aggregated values need to be handled after the aggregation. Hence, the HAVING clause goes after the GROUP BY.

Aggregate on relative daterange in Oracle SQL

I have a table with several columns I would like to aggregate on, I have already aggregated on absolute days, but now I want to aggregate relatively. In my table I have a timestamp, and I would like to group by a date range so that the datestamp's day is 0, the day after 1 and so forth.
SELECT count(*) num_elements, sum(some_value) like_to_sum, reldate
FROM the_large_table
GROUP BY *** what goes here *** reldate.
Can I aggregate on the return from a function, or is there a better way to approach this problem.
If I can aggregate on the result from a function, are there any functions for this already in SQL, or how could this be solved?
SELECT
COUNT(*) AS num_elements,
SUM(some_value) AS like_to_sum,
TRUNC(SYSDATE) - TRUNC(your_date) AS rel_date
FROM the_large_table
GROUP BY TRUNC(SYSDATE) - TRUNC(your_date);

SQL AVG() function returning incorrect values

I want to use the AVG function in sql to return a working average for some values (ie based on the last week not an overall average). I have two values I am calculating, weight and restingHR (heart rate). I have the following sql statements for each:
SELECT AVG( weight ) AS average
FROM stats
WHERE userid='$userid'
ORDER BY date DESC LIMIT 7
SELECT AVG( restingHR ) AS average
FROM stats
WHERE userid='$userid'
ORDER BY date DESC LIMIT 7
The value I get for weight is 82.56 but it should be 83.35
This is not a massive error and I'm rounding it when I use it so its not too big a deal.
However for restingHR I get 45.96 when it should be 57.57 which is a massive difference.
I don't understand why this is going so wrong. Any help is much appreciated.
Thanks
Use a subquery to separate selecting the rows from computing the average:
SELECT AVG(weight) average
FROM (SELECT weight
FROM stats
WHERE userid = '$userid'
ORDER BY date DESC
LIMIT 7) subq
It seems you want to filter your data with ORDER BY date DESC LIMIT 7, but you have to consider, that the ORDER BY clause takes effect after everything else is done. So your AVG() function considers all values of restingHR from your $userId, not just the 7 latest.
To overcome this...okay, Barmar just posted a query.