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 8935239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:56:57+00:00 2026-06-15T09:56:57+00:00

Given the following table: CREATE TABLE channel1m ( ts TIMESTAMP WITHOUT TIME ZONE NOT

  • 0

Given the following table:

CREATE TABLE channel1m (
  ts TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  itemId BIGINT,
  value BIGINT
)

in which a row may be inserted each minute, per itemId, as follows:

ts                    itemId         value
2012-12-03 15:29:00   100            1
2012-12-03 15:30:00   100            2
2012-12-03 15:30:00   101            0
2012-12-03 15:32:00   100            1
2012-12-03 15:32:00   101            1

I can’t find a way (without creating additional tables) to write a query that fills the time gaps (for example, 15:29:00 for itemId 101, and 15:31:00 for both items) by returning NULL in value.

The expected resultset would be:

ts                    itemId         value
2012-12-03 15:29:00   100            1
2012-12-03 15:29:00   101            NULL
2012-12-03 15:30:00   100            2
2012-12-03 15:30:00   101            0
2012-12-03 15:31:00   100            NULL
2012-12-03 15:31:00   101            NULL
2012-12-03 15:32:00   100            1
2012-12-03 15:32:00   101            1

I’ve found solutions having a separate time table with the full serie of timestamps, but I would much prefer to solve this in the query alone. Is this possible?

  • 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-06-15T09:56:58+00:00Added an answer on June 15, 2026 at 9:56 am
    DROP SCHEMA tmp CASCADE;
    CREATE SCHEMA tmp ;
    SET search_path = tmp;
    
    DROP TABLE IF EXISTS channel1m CASCADE;
    CREATE TABLE channel1m (
      zts TIMESTAMP WITHOUT TIME ZONE NOT NULL,
      zitemid BIGINT,
      zvalue BIGINT
    );
    
    -- in which a row may be inserted each minute, per zitemid, as follows:
    
    INSERT INTO channel1m(zts, zitemid, zvalue) VALUES
     ('2012-12-03 15:29:00',   100,            1)
    ,('2012-12-03 15:30:00',   100,            2)
    ,('2012-12-03 15:30:00',   101,            0)
    ,('2012-12-03 15:32:00',   100,            1)
    ,('2012-12-03 15:32:00',   101,            1)
            ;
    
            -- CTE to the rescue!!!
    WITH cal AS (
            WITH mm AS (
                    SELECT MIN(xx.zts) AS minmin, MAX(xx.zts) AS maxmax
                     FROM channel1m xx)
            SELECT generate_series(mm.minmin , mm.maxmax , '1 min'::interval) AS stamp
            FROM mm
            )
    , ite AS (
            SELECT DISTINCT zitemid AS zitemid
            FROM channel1m
            )
    SELECT cal.stamp
            , ite.zitemid
            , tab.zvalue
    FROM cal
    JOIN ite ON 1=1 -- Note: this is a cartesian product of the {time,id} -domains
    LEFT JOIN channel1m tab ON tab.zts = cal.stamp AND tab.zitemid = ite.zitemid
    ORDER BY stamp ASC
            ;
    

    Output:

    NOTICE:  drop cascades to table tmp.channel1m
    DROP SCHEMA
    CREATE SCHEMA
    SET
    NOTICE:  table "channel1m" does not exist, skipping
    DROP TABLE
    CREATE TABLE
    INSERT 0 5
            stamp        | zitemid | zvalue 
    ---------------------+---------+--------
     2012-12-03 15:29:00 |     101 |       
     2012-12-03 15:29:00 |     100 |      1
     2012-12-03 15:30:00 |     100 |      2
     2012-12-03 15:30:00 |     101 |      0
     2012-12-03 15:31:00 |     100 |       
     2012-12-03 15:31:00 |     101 |       
     2012-12-03 15:32:00 |     100 |      1
     2012-12-03 15:32:00 |     101 |      1
    (8 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following tables: CREATE TABLE #USGS_24K_TOPOMAP_BOUNDARIES( [OBJECTID] [int] NOT NULL, [AREA] [numeric](38, 8)
Given the following table structure: CREATE TABLE foo ( ID INT NOT NULL AUTO_INCREMENT,
Given an Oracle table created using the following: CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME
Given the following tables: CREATE TABLE Employees ( first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50)
Given the following tables: CREATE TABLE tree ( id serial NOT NULL, name character
Given the following tables: CREATE TABLE IF NOT EXISTS `rank` ( `rank_id` bigint(20) NOT
Given the following table: create table TreeNode ( ID int not null primary key,
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
Given the following table structure: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11)

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.