I have table having customer appointment date and id. Now i want to find out appointment date is business date or holiday like saturday and sunday .
I want to find out how many customers visits on business day and holiday using postgresql.
Table name is customers_details having two columns id integer, appoinment_date date.
You can use date functions with a conditional expression to define the groups:
select
(extract(dow from appoinment_date) in (0, 6)) is_holiday,
count(*) no_visits
from customers_details
group by 1
In the resultset, is_holiday is a boolean flag that indicates whether the appointment happened on a holiday (true) or on a working day (false).
Related
How to turn a date into last month's date?
SELECT extract(day from (Select (period_start) from accounts where id = 'id')) AS "Day of month";
The above give me the day of the month
Account Table
id (primary)
startPeriod
User records
timestamp
account_id (foreign key)
The user is registered at 02-12for subscription start
I would like to have a query to select the date range from the current month to last month
This current month is April, so I would like to select the record that is
select records from table where date > '03-12-2022'
get the subscription start period and get the date
set the query limited to last month with the subscription start date
how to produce this date 03-12-2022 which is driven by subscription start period and previous month?
many thanks
This approach should work:
Get the number of months between the subscription date and today e.g. age(timestamp1, timestamp2)
Get the number of months and subtract 1 to get last month e.g. duration = extract(month from age(timestamp1, timestamp2)) - 1
Add the months to the subscription date e.g. (subscription_date + (duration || ' month')::INTERVAL)
I am attempting to produce a table of historical unfulfilled units. Currently, the database captures fulfillment date and order date for a record.
CREATE TABLE `input_table`
(order_name STRING,
line_item_id STRING,
order_date DATE,
fulfillment_date DATE)
Sample Record:
order_name: ABC
line_item_id: 123456
order_date: 2017-04-19
fulfillment_date: 2017-04-25
I want to produce a table that shows the fulfillment status by day, starting with the order date and ending with the date prior to the fulfillment date of each line item, e.g. in the above sample record the output_table would be:
Ultimately, this would allow me to query the count of unfulfilled line items each day:
SELECT
date,
count(line_item_id) AS unfulfilled_line_items
FROM
`output_table`
GROUP BY 1
Indicating the fulfillment status is not strictly necessary, considering it would only include dates in which the status was unfulfilled.
While I could do something like this:
with days as (SELECT
*
FROM
UNNEST(GENERATE_DATE_ARRAY('2017-01-01', CURRENT_DATE(), INTERVAL 1 day)) AS day)
SELECT
*
FROM
`input_table`
JOIN days
ON 1=1
AND order_date <= day
AND fulfillment_date > day
..the operation is fairly expensive.
Is there a better way of going about this?
I want to produce a table that shows the fulfillment status by day, starting with the order date and ending with the date prior to the fulfillment date of each line item
Consider below
select date, order_name, line_item_id, 'unfulfilled' fulfillment_status
from `project.dataset.table`,
unnest(generate_date_array(order_date, fulfillment_date - 1)) date
if applied to sample entry in your question - output is
I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);
I need assistance writing a query that determines the quarter of a given date based on 445 FY calendar. The FY ends on first Friday of February every year.
For example in attached image the table has the order id and order created date. Based on the order created date I want to determine which 445 FY quarter it falls in.
Use a case statement with a between condition to divide up your custom quarters. You will need to enter the start and end dates of each quarter:
Select
Case
when date between 'startdateQ1' and 'enddateQ1' then 'Quarter1'
when date between 'startdateQ2' and 'enddateQ2' then 'Quarter2'
when date between 'startdateQ3' and 'enddateQ3' then 'Quarter3'
when date between 'startdateQ4' and 'enddateQ4' then 'Quarter4'
when date is null then 'Null'
else 'Unknown'
end
As Current_Quarter
From DB
I need to show distinct users per week. I have a date-visit column, and a user id, it is a big table with 1 billion rows.
I can change the date column from the CSVs to year,month, day columns. but how do I deduce the week from that in the query.
I can calculate the week from the CSV, but this is a big process step.
I also need to show how many distinct users visit day after day, looking for workaround as there is no date type.
any ideas?
To get the week of year number:
SELECT STRFTIME_UTC_USEC(TIMESTAMP('2015-5-19'), '%W')
20
If you have your date as a timestamp (i.e microseconds since the epoch) you can use the UTC_USEC_TO_DAY/UTC_USEC_TO_WEEK functions. Alternately, if you have an iso-formatted date string (e.g. "2012/03/13 19:00:06 -0700") you can call PARSE_UTC_USEC to turn the string into a timestamp and then use that to get the week or day.
To see an example, try:
SELECT LEFT((format_utc_usec(day)),10) as day, cnt
FROM (
SELECT day, count(*) as cnt
FROM (
SELECT UTC_USEC_TO_DAY(PARSE_UTC_USEC(created_at)) as day
FROM [publicdata:samples.github_timeline])
GROUP BY day
ORDER BY cnt DESC)
To show week, just change UTC_USEC_TO_DAY(...) to UTC_USEC_TO_WEEK(..., 0) (the 0 at the end is to indicate the week starts on Sunday). See the documentation for the above functions at https://developers.google.com/bigquery/docs/query-reference for more information.