Calculating aggregated field - sql

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.

Related

Exclude dates within a range in SQL/Big Query

I am trying to exclude any data that has more than 10 days between Min_Sort_Date and Max_Sort_Date.
This is the initial query I have to start with:
select
srt_bin_id,
Min(srt_cred_tmst) as Min_Sort_Date,
Max(srt_cred_tmst)as Max_Sort_Date,
Count(trkg_id) as count_of_packages,
Max(srt_loc_id) as Store
from bda-prd.dp_logistics.bqt_lgs_rets_rpt
where srt_cred_tmst > "2022-08-13"
Group by 1
Any tips on where to go from here?
If my memory is good, you can use DATE_DIFF function with BigQuery to do something like:
select
DATE_DIFF(Max(srt_cred_tmst), Min(srt_cred_tmst), DAY) AS DAYS_GAP from
Database.Table
Like that you will be able to calculate the gap, and exclude rows with a where condition when DAYS_GAP >= 10

Timestamp to date in SQL

Here is what I did:
Select count(check_id)
From Checks
Where timestamp::date > '2012-07-31'
Group by 1
Is it right to do it like I did or is there a better way? Should/could I have used the DateDIFF function in my WHERE clause? Something like: DATEDIFF(day, timestamp, '2012/07/31') > 0
Also, I need to figure out how I'd calculate the total rate of acceptance for this
time period? Can anyone provide their expertise with this?
Is it right to do it like I did or is there a better way?
Using a cast like that is a perfectly valid way to convert a timestamp to a date (I don't understand the reference to the non-existing datediff though - why would adding anything to a timestamp change it)
However, the cast has one drawback: if there is an index on the column "timestamp" it won't be used.
But as you just want a range after a certain date, there is no reason to cast the column to begin with.
The following will achieve the same thing as your query, but can make use of an index on the column "timestamp" in case there is one and using it is considered beneficial by the optimizer.
Select count(distinct check_id)
From Checks
Where "timestamp" > date '2012-07-31' + 1
Note the + 1 which selects the day after, otherwise the query would include rows that are on that date but after midnight.
I removed the unnecessary group by from your query.
If you want to get a count per day, then you will need to include the day in the SELECT list. In that case casting is a good way to do it:
Select "timestamp"::date, count(distinct check_id)
From Checks
Where "timestamp" > date '2012-07-31' + 1
group by "timestamp"::date

Why are different result between use date_part and exactly date parameter query data in peroid date?

I'm try to count distinct value in some columns in a table.
i have a logic and i try to write in 2 way
But i get diffent results from this two query.
Can any one help to clarify me? I dont know what wrong is code or i think.
SQL
select count(distinct membership_id) from members_membership m
where date_part(year,m.membership_expires)>=2019
and date_part(month,m.membership_expires)>=7
and date_part(day,m.membership_expires)>=1
and date_part(year,m.membership_creationdate)<=2019
and date_part(month,m.membership_creationdate)<=7
and date_part(day,m.membership_creationdate)<=1
;
select count(distinct membership_id) from members_membership m
where m.membership_expires>='2019-07-01'
and m.membership_creationdate<='2019-07-01'
;
I actually think that this is the query you intend to run:
SELECT
COUNT(DISTINCT membership_id)
FROM members_membership m
WHERE
m.membership_expires >= '2019-07-01' AND
m.membership_creationdate < '2019-07-01';
It doesn't make sense for a membership to expire at the same moment it gets created, so if it expires on midnight of 1st-July 2019, then it should have been created strictly before that point in time.
That being said, the problem with the first query is that, e.g., the restriction on the month being on or before July would apply to every year, not just 2019. It is difficult to write a date inequality using the year, month, and day terms separately. For this reason, the second version you used is preferable. It is also sargable, meaning that an index on membership_expires or membership_creationdate can be used.
There is an issue with the first query:
select count(distinct membership_id) from members_membership m
where date_part(year,m.membership_expires)>=2019
and date_part(month,m.membership_expires)>=7
and date_part(day,m.membership_expires)>=1
and date_part(year,m.membership_creationdate)<=2019
and date_part(month,m.membership_creationdate)<=7
and date_part(day,m.membership_creationdate)<=1; -- do you think that any day is less than 1??
-- this condition will be satisfy by only 01-Jul-2019, But I think you need all the dates before 01-Jul-2019
and date_part(day,m.membership_creationdate)<=1 is culprit of the issue.
even membership_creationdate = 15-jan-1901 will not satisfy above condition.
You need to always use date functions on date columns to avoid such type of issue. (Your second query is perfectly fine)
Cheers!!
The reason could be due to a time component.
The proper comparison for the first query is:
select count(distinct membership_id)
from members_membership m
where m.membership_expires >= '2019-07-01' and
m.membership_creationdate < '2019-07-02'
--------------------------------^ not <= ---^ next day
This logic should work regardless of whether or not the "date" has a time component.

Is there a way to turn these multiple SQL queries into one?

I have a query that looks like this:
SELECT COUNT(*)
FROM table
WHERE date_field >= '2018-04-08'
AND date_field <= '2018-04-14'
I need to do this 26 times, for the current week and for 25 previous weeks, with each result separated by a carriage return. Is this possible with a single SQL query or do I need to put it in a loop, as I'm now doing?
Note that this is in FileMaker. I don't think that's relevant, but now you know, just in case.
Look into using "group by". Assuming that you are looking at calendar weeks, grouping by the week of the date will give you the counts per week and an extra condition can limit the overall range.
FileMaker has a WeekOfYearFiscal() function. 2nd parameter is the day of week start date.
SELECT WeekOfYearFiscal(dateField;2), COUNT(*)
FROM table
WHERE date_field >= '2018-01-01'
AND date_field <= '2018-04-14'
group by WeekOfYearFiscal(dateField;2)
See this documentation - http://www.filemaker.com/help/12/fmp/html/func_ref1.31.28.html
Give this a go. If the GROUP BY doesn't work on the function, you can nest the inner part and give an alias.

Aggregate function -Avg is not working in my sql query

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.