Friday, March 31, 2023
No Result
View All Result
Get the latest A.I News on A.I. Pulses
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
No Result
View All Result
Get the latest A.I News on A.I. Pulses
No Result
View All Result

Dates and Subqueries in SQL. Working with dates in SQL | by Michael Grogan | Jan, 2023

January 30, 2023
141 9
Home Data science
Share on FacebookShare on Twitter


Working with dates in SQL

Supply: Photograph by webandi from Pixabay

It’s usually the case that when working with a SQL database, one usually has to work with tables that include a date column displaying the date for every related document.

Nevertheless, the flexibility of SQL to work with dates and yield priceless insights from such knowledge varieties is commonly not nicely understood.

Climate Knowledge Instance

Allow us to contemplate the next instance. Suppose there exists a climate database with recorded dates and related climate data in a desk. Here’s a snippet of the info:

Supply: Desk (and knowledge) created by writer utilizing PostgreSQL. Desk displayed in pgAdmin4.

Additionally, allow us to suppose {that a} month variable has been outlined within the desk and the related values have been extracted from the desk as follows:

replace weatherdata set month=extract(month from date);

Now, for the needs of guaranteeing that we’ve adequate temperature information for every month and wouldn’t have too lengthy a lag between information — allow us to assume that we want to calculate the common length between every consecutive document within the desk, and group them by month.

This process will probably be achieved by:

Calculating the distinction between every consecutive date utilizing the LAG() functionUsing a subquery to calculate the common length between every document that has been calculated in step 1, after which grouping them by month

Calculate length between dates

By utilizing the LAG perform, we are able to calculate the length between every consecutive date. Nevertheless, we might additionally prefer to show the date and month columns within the new desk as nicely — we might want to use the month column on the subject of subsequently grouping the common length by month.

To perform this, we should:

Calculate the length between dates by subtracting the distinction between every consecutive date utilizing the LAG functionInner be part of the related desk to itself by means of the usage of an INNER JOIN perform

That is accomplished as follows:

choose t1.date, t1.date – lag(t1.date) over (order by t1.date) as date_difference, t1.month from weatherdata as t1 inside be part of weatherdata as t2 on t1.date=t2.date;

Right here is the generated desk from the above question:

Supply: Desk (and knowledge) created by writer utilizing PostgreSQL. Desk displayed in pgAdmin4.

We are able to see that for the climate knowledge recorded final month — there’s lower than a day of length for many entries — which means that climate patterns are being recorded with regularity and we’re doubtless acquiring a consultant pattern for that month!

Utilizing subquery with a GROUP BY perform

Having calculated the above desk, we now want to calculate the common length between recorded dates by month.

So as to do that utilizing the info we’ve simply generated above — we should now use a subquery. That’s to say, we will probably be incorporating the above question right into a broader mixture question that may use the GROUP BY perform.

To group the length by month, the next question is run:

choose month, avg(date_difference) from (choose t1.date as date, t1.date – lag(t1.date) over (order by t1.date) as date_difference, t1.month as month from weatherdata as t1 inside be part of weatherdata as t2 on t1.date=t2.date) as subquery group by month order by month;

Right here is the generated knowledge:

Supply: Desk (and knowledge) created by writer utilizing PostgreSQL. Desk displayed in pgAdmin4.

From the above, we are able to see that throughout months 1–12 (January to December) — we’ve calculated the common length between every recorded date for every month.

Conclusion

On this article, you’ve seen:

How you can extract month values from datesHow to make use of the LAG perform to calculate variations in length between consecutive datesUse of subqueries to permit to be used of mixture capabilities comparable to GROUP BY

Many thanks for studying, and any questions or suggestions are enormously appreciated!

Disclaimer: This text is written on an “as is” foundation and with out guarantee. It was written with the intention of offering an outline of information science ideas, and shouldn’t be interpreted as skilled recommendation. The findings and interpretations on this article are these of the writer and usually are not endorsed by or affiliated with any third-party talked about on this article. The writer has no relationship with any third events talked about on this article.



Source link

Tags: DatesGroganJanMichaelSQLSubqueriesWorking
Next Post

CRPS : Scoring Operate for Bayesian ML Fashions | by Itamar Faran

Carry out Outlier Detection In Python In Straightforward Steps For Machine Studying, #1

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent News

Interpretowalność modeli klasy AI/ML na platformie SAS Viya

March 31, 2023

Can a Robotic’s Look Affect Its Effectiveness as a Office Wellbeing Coach?

March 31, 2023

Robotic Speak Episode 43 – Maitreyee Wairagkar

March 31, 2023

What Is Abstraction In Pc Science?

March 31, 2023

How Has Synthetic Intelligence Helped App Growth?

March 31, 2023

Leverage GPT to research your customized paperwork

March 31, 2023

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
A.I. Pulses

Get The Latest A.I. News on A.I.Pulses.com.
Machine learning, Computer Vision, A.I. Startups, Robotics News and more.

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
No Result
View All Result

Recent News

  • Interpretowalność modeli klasy AI/ML na platformie SAS Viya
  • Can a Robotic’s Look Affect Its Effectiveness as a Office Wellbeing Coach?
  • Robotic Speak Episode 43 – Maitreyee Wairagkar
  • Home
  • DMCA
  • Disclaimer
  • Cookie Privacy Policy
  • Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In