I have a table with 2 columns (exp_Years, exp_months).
Saving years and months in 2 different columns.
Now I need a SQL Query to get min and max experience
If minExp=2years and maxExp=4years
how do I get records with out getting 4.1 and 4.2 exp?
example data:--
table: Resume(name, email, phone, exp_years, exp_months)
I need list of records with exp_years>2 and exp_years<5... but how to check exp_months here..
if I use exp_years>2 and exp_years< 5 condition am getting records with 5years 1 month, 5year 2 months are also getting.
I need only more than 2 years and less than or equals 5 years 0 months.. but not 5years 1month and moreee....
actually I need hibernate HQL Query....
Thanks in advance.
This is a strange way of storing the data. So you are supposed to store two years and three months as years = 2 and months = 3.
But you could just as well store years = 1 and months = 15 or years = 0 and months = 27, which would be the same time span. This said, it would be better to have just one column, i.e months only.
Then to get records with a time span of over 2 years and until 5 years exactly you'd select
where months between 25 and 60
If you must live with the table design you are showing, then you can always calculate total months = years * 12 + months. That would be
where years * 12 + months between 25 and 60
you can make this by add months to years after divide months on 12 then you can use ROUND to round to int value:
select ROUND(max(exp_Years+exp_months/12)), ROUND(min(exp_Years+exp_months/12))
from your_table
Related
I want to take records from database which "creationDate" is no older than 2 years and 2 months back from now(UTC).
How can I filter by date keeping in mind that the value in "creationDate" is integer?
Thanks
I am getting the average age of an account. to do so I have employed the following code.
select
m."storeId", s."locationName",
COUNT(m."membershipEnded" is not null) as "Cancelations",
AVG(AGE(m."membershipEnded"::date, m."memberSince"::date)) as "Membership Duration in days"
FROM "members" m
INNER JOIN "stores" s on s."storeId" = m."storeId"
where (m."membershipEnded" is not null
and m."membershipEnded"::date > m."memberSince"::date)
and "memberSince" is not null
and f."countryCode" in ('US','CA')
and s."storeStatus" ='Active'
group by 1,2
order by "storeId";
The results that I get include invalid days, so one example is: "1 year 35 days 22:17:08.546743" or another one with "2 years 9 mons 41 days 13:42:51.453257".
I've searched everywhere but have found the reason for this miscalculation of the days.
Any and all help will be appreciated.
I will also note that I have just subtracted the two to get the days difference, but I prefer the results with the years, months, and days broken out like it is with age.
Postgresql version is 10.9
***** EDIT - ANSWER
the updated code with the answer from below:
justify_interval() worked like a charm
select
m."storeId", s."locationName",
COUNT(m."membershipEnded" is not null) as "Cancelations",
justify_interval(AVG(AGE(m."membershipEnded"::date, m."memberSince"::date))) as "Membership Duration in days"
FROM "members" m
INNER JOIN "stores" s on s."storeId" = m."storeId"
where (m."membershipEnded" is not null
and m."membershipEnded"::date > m."memberSince"::date)
and "memberSince" is not null
and s."countryCode" in ('US','CA')
and s."storeStatus" ='Active'
group by 1,2
order by "storeId";
Operations on intervals can lead to these kind of results as operations are done on the years, then on the months, then days etc.
To overcome it, use justify_interval to adjust the exceeding months/days/hours etc
select justify_interval('1 year 35 days 22:17:08.546743'),
justify_interval('2 years 9 mons 41 days 13:42:51.453257');
justify_interval | justify_interval
-------------------------------------+-----------------------------------------
1 year 1 mon 5 days 22:17:08.546743 | 2 years 10 mons 11 days 13:42:51.453257
I apologize, I am new at SQL. I am using BigQuery. I have a field called "last_engaged_date", this field is a datetime value (2021-12-12 00:00:00 UTC). I am trying to perform a count on the number of records that were "engaged" 12 months ago, 18 months ago, and 24 months ago based on this field. At first, to make it simple for myself, I was just trying to get a count of the number of records per year, something like:
Select count(id), year(last_engaged_date) as last_engaged_year
from xyz
group by last_engaged_year
order by last_engaged_year asc
I know that there are a lot of things wrong with this query but primarily, BQ says that "Year" is not a valid function? Either way, What I really need is something like:
Date() - last_engaged_date = int(# of months)
count if <= 12 months as "12_months_count" (# of records where now - last engaged date is less than or equal to 12 months)
count if <= 18 months as "18_months_count"
count if <= 24 months as "24_months_count"
So that I have a count of how many records for each last_engaged_date period there are.
I hope this makes sense. Thank you so much for any ideas
[How to] Return the number of months between now and datetime value [in BigQuery] SQL
The simples way is just to use DATE_DIFF function as in below example
date_diff(current_date(), date(last_engaged_date), month)
This question already has answers here:
SQL - Running Total - Year To Date, Previous Year To Date, and Last Rolling 12 Months
(1 answer)
SQL Server - Cumulative Sum over Last 12 Months, but starting from the Last Month (SQL Server 18)
(2 answers)
Closed 2 years ago.
I have a below table with me in which I have 2 years sales data with me and with multiple heads. I want to do running total of previous 12 months for all the heads. So for example if I am in Apr - 2019, my running total should give me sum of sales from May-2018 to Apr-2019 (12 months). And it should change dynamically as my month gets changed. The output should look something like below.
It would be very helpful for me if anyone can guide me on this. Please feel free to ask any questions regarding the requirement.
This question already has an answer here:
SQL Server Management Studio make default date 5 days from now
(1 answer)
Closed 4 years ago.
I need to create a view everyday with all records reaching the "Prazo" date ('Prazo' stands for due date in Portuguese), so everyday I need to make a select showing all the records that are 5 days from reaching the due date. How can I do that?
You can do:
Select *
from Table as t
Where DATEDIFF(DD, GETDATE(), PrazoDate) = 5
It just says how many records from today are 5 days from reaching their due date.