Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6375631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:38:27+00:00 2026-05-25T01:38:27+00:00

Suppose I have a table like CREATE TABLE associacao ( id bigserial NOT NULL,

  • 0

Suppose I have a table like

CREATE TABLE associacao
(
  id bigserial NOT NULL,
  idusuario character varying(50),
  idunit character varying(50),
  dataassociacao timestamp with time zone,
  codigo bigint NOT NULL DEFAULT 0,
  CONSTRAINT associacao_pkey PRIMARY KEY (id)
)

with data like

id | idusuario | idunit | dataassociacao               | codigo
1  | "100000"  | "200"  | "2011-08-25 10:20:25.123-03" |   3
2  | "100000"  | "300"  | "2011-08-25 10:20:25.123-03" |   3
3  | "400000"  | "500"  | "2011-08-25 05:20:26.123-03" |   3
4  | "400000"  | "600"  | "2011-08-25 05:20:26.123-03" |   3
5  | "700000"  | "800"  | "2011-08-25 16:20:26.123-03" |   3
6  | "700000"  | "900"  | "2011-08-25 16:20:26.123-03" |   3
7  | "1000000" | "1100" | "2011-08-25 21:20:26.123-03" |   3
8  | "1200000" | "1300" | "2011-08-24 22:20:23.123-03" |   2
9  | "1200000" | "1300" | "2011-08-24 22:20:26.123-03" |   3

I want a SQL Statement that divides the day into 3 shifts (22:00:00.001 through 06:00:00.000, 06:00:00.001 through 14:00:00.000 and 14:00:00.001 through 22:00:00.000) and count how many distinct idusuario’s each part has.

so far I have reached the following code:

SELECT 
    CASE  
       WHEN DATE_PART('hour', dataassociacao) BETWEEN 6 AND 14 THEN 1 
       WHEN DATE_PART('hour', dataassociacao) BETWEEN 14 AND 22 THEN 2 
       WHEN DATE_PART('hour', dataassociacao) BETWEEN 22 AND 24 THEN 3 
       WHEN DATE_PART('hour', dataassociacao) BETWEEN 0 AND 6 THEN 3
    END AS data, COUNT(distinct idusuario)
FROM associacao
WHERE codigo = 3
GROUP BY data
ORDER BY data;

which gives me the following table (with the above example)

data | count(idusuario)
 1   |       1
 2   |       3
 3   |       1

my problems are:

  1. if I wanted a count by day, I wouldnt get the shifts counted correctly, like, 3rd shift (the night shift) would get counted as if they were working from 00:00:00.001 through 06:00:00.000 and then again from 22:00:00.001 through 00:00:00.000 in the same day, and not from 22:00:00.001 the day before through 06:00:00.000 the current day
  2. the date is only comparing hour, this way, every record from 22:00:00.000 through 22:59:59.999 are counting towards 2nd shift, not 3rd shift which is the correct.

Any toughts??

thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:38:27+00:00Added an answer on May 25, 2026 at 1:38 am

    I think the following is along the lines of what you want…

    First, you’re going to want a Calendar File, if you don’t have one already.

    Second, go ahead and define a Shift table. This would probably be important for tracking in any case, and is crucial here. Quick and dirty definition:

    CREATE TABLE Shift (Shift INTEGER NOT NULL, 
                        Start_Offset SMALLINT NOT NULL,
                        End_Offset SMALLINT NOT NULL,
                        CONSTRAINT Shift_PK PRIMARY KEY(Shift))
    

    Data to insert:

    INSERT INTO Shift VALUES(1, 6, 14), (2, 14, 22), (3, 22, 30)
    

    Then you should be able to run this query:

    SELECT a.Calendar_Date, b.Shift, COUNT(DISTINCT c.idusuario)
    FROM calendar as a
    CROSS JOIN Shift as b
    JOIN associacao as c
    ON c.dataassociacao >= a.Calendar_Date + ((INTERVAL '1 hours') * b.Start_Offset)
    AND c.dataassociacao < a.Calendar_Date + ((INTERVAL '1 hours') * b.End_Offset)
    GROUP BY a.Calendar_Date, b.Shift
    ORDER BY a.Calendar_Date, b.Shift
    

    This results in:

    Calendar_Date    Shift    Count
    =======================================
    2011-08-24       3        2
    2011-08-25       1        1
    2011-08-25       2        2 
    

    (Please note: I performed all build/test work on DB2, which doesn’t have the interval functions. From looking at the documentation and associated information online, the given query should be equivalent to what I wrote, but I cannot test on an instance of PostgreSQL).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a table like: create table user_news ( user_id int unsigned not
Suppose I have a table like: create table { id numeric(5,3), code varchar(10) }
Suppose we have 2 tables, a and b: CREATE TABLE a (id_a INT NOT
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
Suppose I have a table of users. Something like: ID integer, USER text, POSITION
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
Suppose that I have a table like: class Ticker(Entity): ticker = Field(String(7)) tsdata =
I have a postgres table like this: CREATE SEQUENCE seq; CREATE TABLE tbl (id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.