Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpmg's avatar
Level 13

Help with sql query postgres!!

I am making a form that should tell me the people who did not go to work in a given date range ... Ejm from 2019-09-01 to 2019-09-30, the question I ask is how can I make a query to see who does not they went to work in that range since I have several ideas but I do not function ... If I use the function between everything, it gives me true since when getting that person was at least once on that date is the person working, but as I do to see if he didn't go to work on any of those days ???

Sorry for my English

0 likes
3 replies
Nakov's avatar

@jpmg can you please share some details about the database table structure that you are using? How do you store the working hours and so on, in order to get better help.

jpmg's avatar
Level 13

This is my Workes TABLE

CREATE TABLE public.persons_persons
(
    id integer NOT NULL DEFAULT nextval('persons_persons_id_seq'::regclass),
    created timestamp with time zone NOT NULL,
    modified timestamp with time zone NOT NULL,
    "number" integer NOT NULL,
    full_name character varying(400) NOT NULL,
    gender character varying(20) NOT NULL,
    phone character varying(100) NOT NULL,
    agreement_id integer NOT NULL,
    sector_id integer NOT NULL,
    admission_date date NOT NULL,
    cuil character varying(11),
    turn_off date,
    ignored_checktime boolean NOT NULL,
    company_id integer,
    blood_type character varying(7) NOT NULL,
    civil_state character varying(10) NOT NULL,
    confidential_file boolean NOT NULL,
    date_of_birth date,
    external boolean NOT NULL,
    cost_center_id integer NOT NULL,
    function_id integer NOT NULL,
    author_id integer,
    editor_id integer,
    agreement_category_id integer NOT NULL,
    CONSTRAINT persons_persons_pkey PRIMARY KEY (id),
     CONSTRAINT persons_persons_agreement_category_i_38a73002_fk_agreement FOREIGN KEY          
    (agreement_category_id)
      REFERENCES public.agreements_category (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
    CONSTRAINT persons_persons_agreement_id_84b14273_fk_agreement FOREIGN KEY (agreement_id)
        REFERENCES public.agreements_agreements (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
    CONSTRAINT persons_persons_author_id_671f6812_fk_users_user_id FOREIGN KEY (author_id)
        REFERENCES public.users_user (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
    CONSTRAINT persons_persons_company_id_2524264e_fk_companies_company_id FOREIGN KEY (            
    company_id)
    );

This is my chek time workes

CREATE TABLE public.liquidations_checktime
(
    id integer NOT NULL DEFAULT nextval('liquidation_checktime_id_seq'::regclass),
    date_hour timestamp with time zone NOT NULL,
    type character varying(20) NOT NULL,
    day date NOT NULL,
    "timestamp" timestamp with time zone NOT NULL,
    real_date_hour timestamp with time zone NOT NULL,
    indicted boolean NOT NULL,
    liquidation integer NOT NULL,
    status character varying(20) NOT NULL,
    origin_id integer NOT NULL,
    person_id integer NOT NULL,
    CONSTRAINT liquidation_checktime_pkey PRIMARY KEY (id),
    CONSTRAINT liquidation_checktim_origin_id_2a88117b_fk_liquidati FOREIGN KEY (origin_id)
        REFERENCES public.liquidations_dataorigin (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
    CONSTRAINT liquidation_checktime_person_id_8f08ddd8_fk_persons_persons_id FOREIGN KEY           

(person_id) REFERENCES public.persons_persons (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED )

The day attribute in the table liquidations_checktime is the one that controls the day the person signed their attendance

jpmg's avatar
jpmg
OP
Best Answer
Level 13

I finally did it myself..

SELECT
        COUNT(liquidations_checktime.*),to_char(generate_series::date, 'day') as nomd,
        generate_series::date
FROM
        liquidations_checktime
        RIGHT JOIN generate_series('2019-09-29', '2019-10-29', interval '1 day') ON generate_series = day and
        person_id=411  
    GROUP BY
    generate_series;

works perfectly!!

Please or to participate in this conversation.