Fetching next lower date record in table 1 referring date in table2 - date

account
date
name
1221
01-01-2020
vinay
1221
05-01-2020
vinay
account.
date.
1221.
8-01-2020
Result
Account
date
name
1221
05-01-202
vinay
For my bad, I couldn’t take query from my Citrix network.

Related

Handle date and time for different timezones

I'm working on a feature of an app where users can create groups, and each group can have multiple posts. The groups have a start and end date. I'm using Node.js for the backend and PostgreSQL to model the data. The tables look like this (simplified):
CREATE TABLE IF NOT EXISTS public."group"
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
start_date timestamp with time zone NOT NULL,
end_date timestamp with time zone NOT NULL,
owner_id uuid
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
)
CREATE TABLE IF NOT EXISTS public."post"
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
group_id uuid,
created_at timestamp with time zone NOT NULL
)
A user can only post to a specific group once a day and the days in which they post should be recorded (users will be able to see in a "progress calendar" the days in which other people in the group and themselves created a post). Users from different countries may enter the same group.
How should I approach the following problems:
1. Make sure the user doesn't post more than once a day.
Here I could just create a query which checks if the user already
posted on day X. The problem is not knowing the user's
timezone, as the server's timezone is generally used. Should I just
store users' location in the database when they create an account
and use the timezone from there? What if they move? I also thought
about sending a timezone along with the request, but that doesn't
seem like a good idea, as a malicious user could send anything
there.
2. Sync the progress calendars for users with different time zones (e.g. user X from Romania, at 1 AM 22 August, should not see an x-mark in user's Y calendar for the date of 21 August, denoting that user Y hasn't posted on that date, as user Y still has time to post (user Y's date/time is 21 August 6 PM).
3. Not as important as the other 2 - What's a good practice for choosing the start / end date timezone for the group, so that users are not surprised when the group starts / ends. I would go with UTC, but I'm not sure people having UTC-9 and UTC+9 would have a pleasant experience with this.
Just to give an example, timestamp with timezone is represented as 2021-08-17 20:01:00.427+03 in postgres.

How to make table triggier based on date?

Is there a way to make a table trigger that sends an email once a date attribute get 30 days from the date listed? e.g. 07/31/2020 is listed in the table and when it gets to 07/01/2020 in real life, an email is sent out to notify someone. it would be a 30 day in advance notification.
Thanks
You can make an SQL Job, which will executed on daily basis and send db email once it get date difference (datediff(D, getdate(),'2020-07-31) equals to 30 OR change in the table to trigger

Active redshift user count for specific time period

I need Active redshift user count for specific time period (say 20th Apr to 25th May).
I know PG_user table, but know luck with what i am looking for.
All on all i need a report with active user count for specific time period (20th Apr to 25th May).
Thanks a lot in advance.
Do you need to know how many user where logged in that time period?
to get the list day by day
select starttime, process, user_name
from stl_sessions where starttime>='2017-04-20' starttime <'2017-05-26'
to count users in total during that period:
select count(*)
from stl_sessions where starttime>='2017-04-20' starttime <'2017-05-26'

How to set the SQL Query for a Report?

Crystal Reports 2011.
Database is MS Access 2003
I have the following tables:
Calendar
has Date entries for the current and next Year, for every Day of the Year, plus some Status Fields marking certain days as "Special" (Joining this table so I have a record for Days with no activity.
Staff table
StaffNo
Name
.
.
.
DayResults
Date
StaffNo
Status
.
.
.
The DayResults table has one entry per Day and Staff.
Entries are only made when the staff gets an entry by the Program for Status or other events. Staff that does not log in the system has no entry for this Day.
So, in case of John not showing up on July 2nd, i have no entry for him for this Day. But I need an entry for my report!
I need to create a Report that fetches Data from the DayResults Table and make calculations on the Parameters here, in order to calculate a Daily as ell as Period Bonus.
The rules for this Bonus require that a Day without activity (i.e. No Show) results in a negative bonus amount.
Therefore I need to have a select statement which creates an entry FOR EACH DAY FOR EACH STAFF.
This should look like this:
Date StaffNo Name Status
2016/07/01 1 Jim 1
2016/07/01 2 John 2
2016/07/02 1 Jim 2
2016/07/02 2 John NULL
(John did not show up on 2016/07/02 ...)
SELECT Calendar.Date, Staff.StaffNo, Staff.NickName, DayResults.Status
FROM Staff LEFT JOIN (Calendar RIGHT JOIN DayResults ON Calendar.Date = DayResults.Date) ON Staff.StaffNo = DayResults.StaffNo;
Unfortunately, no entry here for John on July 2nd?
Any idea how to proceed?
Manfred

Average Monthly Balance of Accounts

I have table containing daily balances of all accounts of different branches with following fields.
Branch_Code, Account_Num, Date, Day_End_Balance
The table contains data from 01-01-2015 to 31-12-2015 on daily basis for each accounts. I want to get average monthly balance from January to December for each account of Branch_Code "123".
What should be the SQL Query for teradata.
Regards
KAM
Select AVG(balance) from table where branch_code="123" group by Account_Num
I guess.. this should work.. :)