I have a factory that contains 4 production lines, work in a year which is devised into periods of year. I also have a working days and holidays table that contains these columns:
factory_id, mainline_id, year_id, period_id, holiday check
Holiday check is a boolean column; it's true when the day is holiday, false when it's working day
I want to count all working days in a period of the year that belongs to specific mainline which is belong to specific factory
I tried
select count(holiday_check)
from myTable
where holiday_check = false
but it returns all working days in all periods and all years for all factories
Use the GROUP BY clause:
SELECT COUNT(*) FROM your_table
WHERE holiday_check = false
GROUP BY period_id, factory_id, mainline_id
Related
The scenario is: I have a workflow in which an object passes through multiple stages until it's closed (end of process). In this case, I have a table with the unique Id of this object, the day it arrived in a given stage, and the day it left this same stage. The calculation of "total days in stage X" must NOT consider weekends and holidays (which is an info that comes from a different calendar table).
Id
first time at stage
last time at stage
01
2021-09-02
2021-09-09
So in between those dates, we have a weekend and 2 holidays here in my country, which means the proper time in stage would be 3.
In paralel, I created a table with the dates of all weekends and holidays of the year (it's just one column "date"), with the intention of basically counting the days in this table that would be in between "first time at stage" and "last time at stage", in order to subtract it from the final datediff.
So I have:
SELECT DISTINCT
id,
first_time_stage,
last_time_stage,
DATE_DIFF(last_time_stage,first_time_stage,DAY) - COALESCE(
(SELECT COUNT(1)
FROM base_holidays_and_weekends c
LEFT JOIN base_stage_periods p ON c.date = p.first_time_at_steage AND c.date = p.last_time_stage
WHERE date BETWEEN first_time_stage AND last_time_stage), 0) AS time_spent_in_stage
Where am I getting it wrong? I assume the issue is having 2 different tables, or in the JOIN... :(
Thank you for any inputs!
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).
I am a beginner in Postgres and looking for some help on a problem I am having with a query. I am trying to count the number of business days between two dates (exclude sat & sun & holidays). This is kind of what I am trying to do:
Select column1, column2, (current_date - defined_date) as elapsed_days
from mytable
where elapsed_days excludes sat, sun, and holidays
create table calendar c as
(cal_date date primary key,
business_day boolean not null);
insert into calendar
(select
('01/01/1900'::date + (g||' days')::interval)::date,
case extract(dow from '01/01/1900'::date
+ (g||' days')::interval)
when 0 then false when 6 then false else true end
from generate_series(0,365*150) g)
Now you have a calendar table populated with weekends set to "business_day=false" and all other days set to true.
You'll have to populate your other holidays manually or write a program to do that.
Afterwards, to calculate difference between days do something like:
select count(*) from cal
where cal between "start_date_var" and "end_date_var"
and business_day=true;
NOTE: If it were me, I'd add a few other columns to your calendar table so that it can include which holiday it is, or other things like that. May even have another table for holidays. This is a good start though.
I am looking for a query which gives me the daily playing time. The start (first_date) and end date(last_update) are given as shown in the Table. The following query gives me the sum of playing time on given date. How can I extend it to get a table from first day to last day and plot the query data in it and show 0 on dates when no game is played.
SELECT startTime, SUM(duration) as sum
FROM myTable
WHERE startTime = endTime
GROUP BY startTime
To show date when no one play you will need create a table days with a date field day so you could do a left join. (100 years is only 36500 rows).
Using select Generate days from date range
This use store procedure in MSQL
I will assume if a play pass the midnight a new record begin. So I could simplify my code and remove the time from datetime field
SELECT d.day, SUM(duration) as sum
FROM
days d
left join myTable m
on CONVERT(date, m.starttime) = d.day
GROUP BY d.day
If I understand correctly, you could try:
SELECT SUM(duration) AS duration, date
FROM myTable
WHERE date <= 20140430
AND date => 20140401
GROUP BY date
This would get the total time played for each date between april 1 and april 30
As far as showing 0 for dates not in the table, I don't know.
Also, the table you posted doesn't show a duration column, but the query you posted does, so I went ahead and used it.
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.