For some reason my query does not take these records into consideration:
50 19/09/2019 15:00:00
60 19/09/2019 17:00:00
70 19/09/2019 18:00:00
What is causing that? Everything seems to work fine otherwise. It excludes the record which is beyond 7 days ago.
=QUERY(A:B,
"where B >= date '"&TEXT(TODAY()-7, "yyyy-MM-dd")&"'
and B <= datetime '"&TEXT(NOW(), "yyyy-MM-dd hh:mm:ss")&"'", 0)
Related
I need to calculate how much I have in my account after today. That means, for the current day
how much I left in my original Total_salary.
Below is my sample data set.
start_date end_date duration(months) Total_salary left_amount
2021-05-03 2022-05-03 12 1200 400
2019-01-01 2023-01-01 48 4800 2300
2018-01-01 2020-01-01 24 2400 0
2020-01-01 2023-01-01 36 3600 1200
2024-01-01 2027-01-01 36 3600 3600
I need get the upto current date how much I left, if end_date < current date.
Let take first row as an example, I agree with a client for working for 12 months with total
salary 1200, by each month I will receive 100 as my salary. So, I need to know today how much
I left from my original total_salary. (100*8 = 800, 1200-800 = 400)
I don't know how to get SUM up to current date.
I need to implement this in pyspark. Please anyone can help me to sort out this?
Thank you
import datetime
from pyspark.sql import functions as F
current_date = datetime.date.today()
(
df
.withColumn('left_months', F.greatest(F.lit(0), F.months_between('end_date', F.lit(current_date))))
.withColumn('left_amount', F.col('total_salary')/F.col('duration(months)') * F.col('left_months'))
.withColumn('left_amount', F.least('total_salary', 'left_amount'))
)
I'm trying to get last 6 months record from table using SQL. Here's the code:
select "datetime"
from "someTable"
where "datetime" >= '2021-06-09 16:00:00'::timestamp - interval '6 months' and "datetime" <= '2021-06-09 16:00:00'::timestamp
group by "datetime"
Result:
2020-12-09 16:00:00
...
2021-06-09 16:00:00
Expected Result:
2021-01-09 16:00:00
...
2021-06-09 16:00:00
Can anyone explain why using - interval '6 months' will get 7 months records? I'v tried not to use interval but between, the results are same. And also I don't want to use 5 months instead of 6 months to get expected result.
Thank you guys!
Use > instead of >= to exclude the lower bound:
where datetime > timestamp '2021-06-09 16:00:00' - interval '6 months'
Then you should get 6 rows as desired.
I have a whole bunch of tariffs, some work on weekends, some work on weekdays some on both. Sometimes I'll be querying on NOW() but sometimes I'll be querying on datetime column.
id | Weekday | Weekend | Price
1 | 1 | 0 | 0.04
2 | 0 | 1 | 0.02
date
2020-04-15 00:00:00
2012-04-16 00:00:00
The date is from another table and is not related to the Price / days of week.
I know I can get the weekend dates by
SELECT * FROM tariff where EXTRACT(ISODOW FROM date) IN (6,7)
however I can't think of how I'd get rows that are either weekend / weekdays or both given a date.
** edit **
Updated the tables to show the dates are seperate. What I'm trying to get is the tariff that corresponds to the date in that table, whether it's on a week day or a weekend (or both but I can extrapolate that).
The weekend 1 is the tariff that is used for weekends, weekdays 1, all days is both.
Cannot give you a query, supply anything to query. Nor can we be sure that the columns Weekday and Weekend mean as you didn't tell us. But if we take them as boolean indicator where 1 means desired may some thing like will work for you.
select ...
from ...
where ...
and ( (weekday = 1 and weekend =1)
or (weekday = 1 and extract(isodow from date) not in (6,7))
or (weekend = 1 and extract(isodow from date) in (6,7))
) ;
I have a table in PostgreSQL.
This table has a months column and a mydate column.
The months has a value of Jan the mydate has a value of 2017-01-01
I want to update that value to 2018-01-01 but I don't want to have to do it by hard coding in the 2018 date. I would like to use a date_part function but I am not sure if I am approaching this correctly.
Here is what I have so far it is not complete I am stuck on what I need to finish this query:
UPDATE tblshopstatus
Set mydate = mydate + date_part('year') -----I am stuck on this line----
WHERE months = 'Jan'
More examples:
In the months column I have all 12 months listed.
In the mydate column I have dates listed as 2017-01-01, 2017-02-01 etc... through the 12 months.
Is there a way to just increase the year to 2018 for all months.
set mydate = mydate + interval '1 year'
I have a CTE-based query in which I retrieve hourly intervals between two given timespans. My query works as following:
Getting start and end datetimes (let's say 07-13-2011 00:21:09 and 07-31-2011 21:11:21)
get the hourly total query values between the hourly intervals (in here it's from 00 to 21, a total of 21 hours but this is parametric and depends on the hours I give for the inputs) for each day. This query works well but there is a problem. It displays hourly amounts but for the start time, it gets all the queries between 00:00:00 and 00:59:59 for each day instead of 00:21:09 - 00:59:59 and same applies for the end time, it gets all the queries between 21:00:00 and 22:00:00 for each day instead of 21:00:00 and 21:11:21. -By the way, the other hour intervals e.g 03:00 - 04:00 etc are currently retrieved normally, no minute and seconds provided, just 1 hour flat intervals- How can I fix that? The query is below, thanks.
WITH cal AS (
SELECT generate_series('2011-02-02 00:00:00'::timestamp , '2012-04-01 05:00:00'::timestamp , '1 hour'::interval) AS stamp
)
, qqq AS (
SELECT date_trunc('hour', calltime) AS stamp
, count(*) AS zcount
FROM mytable
WHERE calltime >= '07-13-2011 00:21:09' AND calltime <='07-31-2011 21:11:21' AND date_part('hour', calltime) >= 0 AND date_part('hour', calltime) <= 21
GROUP BY date_trunc('hour', calltime)
)
SELECT cal.stamp
, COALESCE (qqq.zcount, 0) AS zcount
FROM cal
LEFT JOIN qqq ON cal.stamp = qqq.stamp
WHERE cal.stamp >= '07-13-2011 00:00:00' AND cal.stamp<='07-31-2011 21:11:21' AND date_part('hour', cal.stamp) >= 0 AND date_part('hour', cal.stamp) <= 21
ORDER BY stamp ASC;
EDIT:
What I mean with my problem is, despite giving 00:21:09 for my starting hour on first day, the days after that day calculate the total query count for the first hour interval as count of total queries between 00:00:00-01:00:00 instead of 00:21:09-01:00:00.(by the way this should apply to the first hour interval for every day, I can give 04:30:21 for the starting hour and the day will start to count total queries hourly starting from there etc.- Same applies to the ending hour 21:00:00-21:11:21, only the LAST day in the query results take this interval, other days before it take the query count between hour 21 and 22 by counting all queries between 21:00:00-22:00:00 instead of 21:00:00-21:11:21.
For example, if there are 200 queries between 00:00:00 and 01:00:00 on july 14 2011 (the next day after july 13, the start date) but there are 159 queries between 00:21:09 - 01:00:00, I should get 159 queries instead of 200. Also, if there are 300 queries between 21:00:00-22:00:00 on any random day, and 123 of them are between 21:00:00-21:11:21, I should get 123 queries as result instead of 300. (This applies to every single day, other hourly intervals should be counted as usual such as 01:00-02:00, 20:00-21:00 etc. This is parametric, hourly intervals and start-end times depend on user input-
Adding AND calltime::time >= '00:21:09' AND calltime::time <= '21:11:21' to the WHERE calltime >= '07-13-2011 00:21:09' AND calltime <='07-31-2011 21:11:21' block solved the issue.