SQL查询中的错误查找postgres中选定时间段内每个时间间隔的第一个和最后一个记录

时间:2017-09-22 09:26:31

标签: postgresql

Postgres 9.6。

我被建议a solution似乎有效,但是查询的所有优点,它随机错过了第一个或最后一个记录。为简单起见,有一个1秒间隔的例子。我很难找到bugfix。请帮忙!

SCHEMA:

CREATE TABLE public.prices
(
    uid uuid NOT NULL DEFAULT uuid_generate_v4(),
    instrument character varying COLLATE pg_catalog."default" NOT NULL,
    bid double precision NOT NULL,
    ask double precision NOT NULL,
    dt timestamp without time zone NOT NULL DEFAULT now(),
    CONSTRAINT prices_pkey PRIMARY KEY (uid)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

的索引:

CREATE INDEX idx_dt_instrument
    ON public.prices USING btree
    (dt, instrument COLLATE pg_catalog."default")
    TABLESPACE pg_default;

CREATE INDEX idx_dt_instrument_bid_ask
    ON public.prices USING btree
    (dt, instrument COLLATE pg_catalog."default", bid, ask)
    TABLESPACE pg_default;

CREATE INDEX idx_instrument
    ON public.prices USING btree
    (instrument COLLATE pg_catalog."default")
    TABLESPACE pg_default;

QUERY:

   WITH ranges as (
    SELECT dd as start_range, 
           dd + '1 seconds'::interval as end_range, 
           ROW_NUMBER() over () as grp
    FROM generate_series
            ( '2017-05-01 00:01:00'::timestamp 
            , '2017-05-01 00:01:01'::timestamp
            , '1 seconds'::interval) dd
), create_grp as (
    SELECT r.grp, r.start_range, r.end_range, p.*
    FROM prices p
    JOIN ranges r
      ON p.dt >= r.start_range
     AND p.dt < r.end_range
), minmax as ( 
   SELECT row_number() over (partition by grp
                             order by dt asc) as rn1,
          row_number() over (partition by grp
                             order by dt desc) as rn2,              
          create_grp.*
   FROM create_grp 
)
SELECT *,
       CASE WHEN rn1 = 1 and rn2 = 1 THEN 'first and last'
            WHEN rn1 = 1 THEN 'first'
            WHEN rn2 = 1 THEN 'last'
       END as row_position
FROM minmax
WHERE 
    1 IN (rn1, rn2) 
    AND instrument='EURGBP'
ORDER BY dt
;

原始数据集:

    "uid","instrument","bid","ask","dt"
"62d857cd-4830-4146-9516-dfff57abf9de","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.057"
"da4b37f8-673e-4708-95e9-2c316b46d703","GBPUSD","1.29334","1.29344","2017-05-01 00:01:00.057"
"fceb5de9-bf68-450b-91bf-b97d1af77587","GBPUSD","1.29334","1.29345","2017-05-01 00:01:00.058"
"35b0113b-209d-4195-b27d-f1539055b7de","GBPJPY","143.964005","143.977997","2017-05-01 00:01:00.059"
"df347488-d5e0-4e53-b02f-88e3d63246d2","GBPUSD","1.29334","1.29346","2017-05-01 00:01:00.059"
"83e5eb95-4e02-4c5d-917f-770eb232c54b","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.096"
"d4ed4f64-c292-4172-b7c9-91ee2e7e1b7b","GBPJPY","143.964005","143.979996","2017-05-01 00:01:00.132"
"d9c86d19-5f90-4afe-b29a-a3823366d700","GBPJPY","143.964005","143.981003","2017-05-01 00:01:00.143"
"830b0b6b-a520-462f-9a6d-9f0b18581ba7","GBPUSD","1.29334","1.29345","2017-05-01 00:01:00.172"
"9f4a0c3b-d902-4d17-8e3d-f4867e58ed9f","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.29"
"84c303ea-777a-40c4-9d26-b0767698bda7","GBPJPY","143.964005","143.981003","2017-05-01 00:01:00.294"
"1a79250d-4c3d-47ad-9fbd-6ad208afb1fc","GBPJPY","143.964005","143.988998","2017-05-01 00:01:00.297"
"d3467ea7-0406-46c1-b95a-26fd63b7a28f","GBPUSD","1.29334","1.29345","2017-05-01 00:01:00.298"
"5da4f08d-c4f8-41d3-bc55-4a0d706b872a","GBPJPY","143.964005","143.992004","2017-05-01 00:01:00.312"
"c163ff17-c7ff-4197-8858-8ebc87f1045a","GBPJPY","143.968994","143.992004","2017-05-01 00:01:00.324"
"089ee425-324b-4111-98a1-4cf8dba883c3","GBPJPY","143.968994","143.994003","2017-05-01 00:01:00.347"
"f78a9d85-485e-42ee-8d71-666238cebb02","GBPJPY","143.964005","143.994003","2017-05-01 00:01:00.357"
"c318fdb7-3503-4072-ba74-281780ac2a91","GBPUSD","1.29334","1.29344","2017-05-01 00:01:00.37"
"c5a9a7b0-ee83-4d44-ba48-5f8aa82aa641","GBPJPY","143.964005","143.996002","2017-05-01 00:01:00.382"
"6c0811d3-4129-4e52-93fc-fd9f949e6459","GBPJPY","143.964996","143.996002","2017-05-01 00:01:00.484"
"eb8691da-2480-4853-b49f-ba9ee9d86086","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.485"
"0ceea454-e079-4c25-b58b-635e7c2f2e5e","GBPUSD","1.29334","1.29342","2017-05-01 00:01:00.486"
"156aeaa6-4266-4414-bf5c-400a58929a67","GBPJPY","143.968002","143.983994","2017-05-01 00:01:00.487"
"2dc08833-8a11-4dde-907e-b4af9f795e75","GBPJPY","143.964996","143.992004","2017-05-01 00:01:00.505"
"9685ee23-3365-4db5-84da-041f1d26c6cb","EURGBP","0.8432","0.8433","2017-05-01 00:01:00.507"
"54beeda6-c693-4362-8f96-7c93fd36c4f5","GBPUSD","1.2933","1.29343","2017-05-01 00:01:00.508"
"1b09afe4-5970-43c9-9101-81be1be9c032","EURGBP","0.84321","0.8433","2017-05-01 00:01:00.519"
"40aea241-4ffe-4321-9314-b53ea5fd04d4","GBPJPY","143.964996","143.994995","2017-05-01 00:01:00.532"
"e43c2214-300b-45eb-91a7-eb4411addb06","GBPUSD","1.2933","1.2934","2017-05-01 00:01:00.54"
"fe2eaeb9-94d5-4d53-95ba-53be2864e10c","GBPJPY","143.964005","143.994995","2017-05-01 00:01:00.543"
"eacc0e09-4712-4d7c-9974-1ff4f8884a60","EURGBP","0.84322","0.84332","2017-05-01 00:01:00.553"
"7b9fda5e-dba0-4523-9f01-e7636de5ef78","GBPJPY","143.962006","143.992004","2017-05-01 00:01:00.553"
"37f5d872-1e21-4e92-bb7d-76c17e9e0278","EURGBP","0.84321","0.84332","2017-05-01 00:01:00.579"
"c7553b67-cf59-439e-ad39-ca1374d7fd81","GBPJPY","143.962006","143.983994","2017-05-01 00:01:00.587"
"e7addcce-55b0-4d7a-ae64-177964a6c698","GBPJPY","143.962006","143.985992","2017-05-01 00:01:00.669"
"57d8aa45-eeee-4809-91c5-9e0ff5495351","GBPUSD","1.29327","1.29341","2017-05-01 00:01:00.669"
"1c9b66f9-6558-44dd-94b7-892689ed6a8e","EURGBP","0.84321","0.84333","2017-05-01 00:01:00.67"
"f94c87b3-8dd8-4e97-8cf4-84af7e921c14","GBPJPY","143.957001","143.985992","2017-05-01 00:01:00.67"
"43fd091c-be15-476b-9e76-b9c71062ba92","GBPUSD","1.29325","1.29341","2017-05-01 00:01:00.671"
"90931fdf-5507-44cb-8b25-2e06a634caad","EURGBP","0.84323","0.84333","2017-05-01 00:01:00.672"
"a3e57590-e673-406d-9bd9-a8ac739fac37","GBPUSD","1.29325","1.29338","2017-05-01 00:01:00.679"
"4d2cbbec-06e3-450c-857d-97bb9a4b4271","EURGBP","0.84323","0.84333","2017-05-01 00:01:00.833"
"4a036cce-e34a-4ec2-9bb0-5760465cd6fc","GBPJPY","143.957001","143.985992","2017-05-01 00:01:00.833"
"3ddfddb4-7c5d-4dea-92ee-411bbba4f699","GBPUSD","1.29325","1.29339","2017-05-01 00:01:00.834"
"2a0334b9-f6cd-4bd9-8517-7bd7cfb83415","GBPJPY","143.957001","143.983994","2017-05-01 00:01:00.836"
"bf9293a1-d52e-4354-9583-c3c02178f985","EURGBP","0.84323","0.84333","2017-05-01 00:01:00.888"
"4ce95ac5-2de0-4123-8916-4dc4ab2659ea","GBPUSD","1.29325","1.29339","2017-05-01 00:01:00.891"
"377f5831-931b-4870-8b08-c6a7f3b6ad2e","GBPJPY","143.957001","143.983994","2017-05-01 00:01:01.014"
"96977442-9f60-4c02-87e9-c4ffa685e324","EURGBP","0.84323","0.84333","2017-05-01 00:01:01.017"
"509a3f68-7126-45ea-9f89-99d92c71697a","GBPUSD","1.29325","1.29339","2017-05-01 00:01:01.017"
"14e4d804-b419-43fb-9189-44a1fbe7a4e4","GBPUSD","1.29325","1.29338","2017-05-01 00:01:01.02"
"34dd5dac-d0e1-49e5-92cf-6d9d9de14e49","GBPUSD","1.29325","1.29339","2017-05-01 00:01:01.052"
"91cdab81-8a50-4db7-8292-a90f4c8c42cb","GBPJPY","143.957001","143.983994","2017-05-01 00:01:01.065"
"e8b9b67a-3708-4463-83d9-39b0286af14e","EURGBP","0.84323","0.84333","2017-05-01 00:01:01.193"
"abbb50d6-e764-48e6-9172-98dbfe04f961","EURGBP","0.84324","0.84333","2017-05-01 00:01:01.195"
"d4e07afc-c0b6-45b5-9004-e378116ac8e0","GBPUSD","1.29325","1.29339","2017-05-01 00:01:01.195"
"706004b0-dc76-4dfd-ba78-f8fcf455a86b","GBPJPY","143.957001","143.983994","2017-05-01 00:01:01.196"
"a1a5c7c9-8579-4477-ba55-f3bb6f9888c7","EURGBP","0.84324","0.84334","2017-05-01 00:01:01.231"
"5e29a0b9-e47c-4547-b9ab-30e80c50bb96","GBPUSD","1.29325","1.29339","2017-05-01 00:01:01.284"
"7fe3cea7-e2dc-4c82-b425-8cb4514a0742","GBPJPY","143.957001","143.983994","2017-05-01 00:01:01.297"
"91db0d7e-422a-4236-90f9-dbd1ca569c14","EURGBP","0.84324","0.84334","2017-05-01 00:01:01.38"
"dfeaf1d3-adea-4476-8717-3724ffc253f4","GBPUSD","1.29325","1.29339","2017-05-01 00:01:01.403"
"378a0924-510f-41b6-aa01-0508c22f1745","GBPUSD","1.29325","1.29338","2017-05-01 00:01:01.449"
"763237fc-85ba-4263-9ab6-4348b08c5cfc","EURGBP","0.84324","0.84334","2017-05-01 00:01:01.461"
"85a77007-c176-4ce0-9500-0d3919552fe5","EURGBP","0.84325","0.84334","2017-05-01 00:01:01.573"
"bdf1c190-d517-4b15-8b20-b01936e8e7ba","GBPJPY","143.957001","143.983994","2017-05-01 00:01:01.574"
"a80c4a57-8c3f-42f4-a6c1-54ab8f9fa13e","EURGBP","0.84327","0.84334","2017-05-01 00:01:01.576"
"7c811f46-3772-434e-b466-ee830c62c61e","GBPUSD","1.29325","1.29338","2017-05-01 00:01:01.576"
"ec7e93ad-af3e-4d64-9c7d-5881ed6444d8","GBPUSD","1.29325","1.29339","2017-05-01 00:01:01.593"
"65f794fb-278f-40b1-9974-142b0fc03125","EURGBP","0.84327","0.84338","2017-05-01 00:01:01.649"
"14e496ed-c732-444d-ada5-ef4e1f7434ba","GBPJPY","143.957001","143.983994","2017-05-01 00:01:01.649"
"3d28c092-3d42-4399-b774-c927c2f2283c","GBPUSD","1.29323","1.29339","2017-05-01 00:01:01.65"
"eb0bdae8-b67a-4262-ad87-0db109b53434","GBPJPY","143.955002","143.983994","2017-05-01 00:01:01.672"
"cde342f0-5bd3-469b-9f98-6053f9ea153e","EURGBP","0.84327","0.84338","2017-05-01 00:01:01.777"
"637bec3a-c65e-49ca-8381-ab5c7df29580","GBPUSD","1.29323","1.29339","2017-05-01 00:01:01.78"
"60af62a4-524b-4ad3-a4a8-1db39a8bccf5","EURGBP","0.84327","0.84339","2017-05-01 00:01:01.783"
"8ef15da4-7836-4186-b30b-9c4eb1e1aa3f","GBPJPY","143.955002","143.983994","2017-05-01 00:01:01.796"
"7ae9a50f-35b7-4a09-a392-1459abf12122","EURGBP","0.84327","0.8434","2017-05-01 00:01:01.832"
"00c63fdc-ddef-4dde-9f44-69d7482882e0","EURGBP","0.84328","0.8434","2017-05-01 00:01:01.846"
"e6791f56-e321-416b-bef5-6d45f89feee0","GBPUSD","1.29323","1.29338","2017-05-01 00:01:01.946"
"7d31abc9-5ea3-4d8b-a68e-5f3c8cc6addd","GBPJPY","143.955002","143.983994","2017-05-01 00:01:01.95"
"ca6ac100-8fd8-46e7-afc7-9966526a5c14","EURGBP","0.84329","0.8434","2017-05-01 00:01:01.96"
"5b3e48ca-b9e8-4d18-86eb-a326e33db188","GBPUSD","1.29323","1.29338","2017-05-01 00:01:01.982"
"5299b60d-654f-411a-8eab-34ed6bc61fb3","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.148"
"48dc504c-ba88-4665-a2e5-5f584529f19d","GBPUSD","1.29323","1.29338","2017-05-01 00:01:02.149"
"cafacce6-7ec1-4386-9323-6bad8d07ca50","GBPJPY","143.955002","143.983994","2017-05-01 00:01:02.15"
"959efc0d-6116-4c50-9a02-d3483c116732","GBPUSD","1.29324","1.29339","2017-05-01 00:01:02.151"
"097e7e31-8cb4-4f3e-9fb5-71569c5be2b7","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.187"
"18a62956-93fc-401a-9f89-8f94a3e3f463","GBPJPY","143.955002","143.983994","2017-05-01 00:01:02.258"
"fba787c1-db9b-4af7-80e1-8823209d30ba","GBPUSD","1.29324","1.29339","2017-05-01 00:01:02.259"
"c59d01fb-67fc-4b0d-b11a-13790ac512f5","GBPUSD","1.29325","1.29339","2017-05-01 00:01:02.356"
"13f9d5f0-f933-4633-9b79-5eaec4975997","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.357"
"e23dcfdc-1845-4c6c-b78a-c80562079853","GBPJPY","143.955002","143.983994","2017-05-01 00:01:02.371"
"afdf5a3a-ca4c-4b92-bbb2-f83391aaf603","GBPUSD","1.29325","1.29339","2017-05-01 00:01:02.399"
"67d58e76-34ae-4c06-b53b-dab360500f22","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.418"
"e9b1e743-2f3e-4d3e-8d01-14d7ec968efc","GBPUSD","1.29325","1.29339","2017-05-01 00:01:02.582"
"76bf9147-a9b7-4db8-bd09-4d2cd3635ace","GBPUSD","1.29325","1.29339","2017-05-01 00:01:02.796"
"4b8d5562-7bef-410d-9d83-ca5b3590aa0d","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.797"
"77849b8e-527f-47aa-967c-f4f161305537","GBPJPY","143.955002","143.983994","2017-05-01 00:01:02.832"
"3840acd1-c790-4302-b61d-35f76f9feb3f","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.878"
"b2786f7b-9f2c-40ae-af02-31bba7ad8ab0","GBPUSD","1.29325","1.29339","2017-05-01 00:01:02.987"
"0b99d3f6-1146-46ac-ba16-bc3b1f8fdf45","EURGBP","0.84329","0.8434","2017-05-01 00:01:02.995"
"54cecdbd-8cee-4f77-bcc5-9fc46bd3b0e9","GBPJPY","143.955002","143.983994","2017-05-01 00:01:03.067"
"23c82fb7-b264-421f-b7a7-60c5f6e04065","GBPUSD","1.29325","1.29339","2017-05-01 00:01:03.09"
"564fdf6d-3a5c-46e3-8b16-51a78c08a382","EURGBP","0.84328","0.8434","2017-05-01 00:01:03.091"
"df67f32a-3783-4c23-88cd-ef809dc0766b","GBPJPY","143.955002","143.983994","2017-05-01 00:01:03.186"
"ddb57e22-eb1f-4ba6-b442-c1bb60d3543f","EURGBP","0.84328","0.8434","2017-05-01 00:01:03.208"
"8e6e2053-4aa2-4e75-8740-ea89202f727a","GBPUSD","1.29325","1.29339","2017-05-01 00:01:03.208"
"8b1ff49d-57bd-4bfd-8305-38e76ed24672","GBPUSD","1.29324","1.29339","2017-05-01 00:01:03.287"
"a65d1000-0603-4131-95b8-30c9e9c63df4","GBPJPY","143.955002","143.983994","2017-05-01 00:01:03.301"
"ebc03747-c019-4aa2-9fb0-36f7177546d0","EURGBP","0.84328","0.8434","2017-05-01 00:01:03.389"
"abb5f2b9-869d-48d0-ab69-9d312018fdf9","GBPUSD","1.29324","1.29339","2017-05-01 00:01:03.401"
"b8a26b37-8863-497c-aafd-9ba7281e2e40","GBPJPY","143.955002","143.983994","2017-05-01 00:01:03.417"
"52fccae5-7af7-4e9d-8fb3-f9f0d8b25241","EURGBP","0.84328","0.8434","2017-05-01 00:01:03.44"
"78690be1-773b-4872-a0ab-6f77c5a0a5e7","GBPUSD","1.29324","1.29339","2017-05-01 00:01:03.587"
"b3d6fd81-4c5a-49f4-896b-8b4790aa2a73","GBPJPY","143.955002","143.985001","2017-05-01 00:01:03.672"
"c9ae32fe-33d1-4486-bf79-3d23aaa42e59","GBPUSD","1.29324","1.29339","2017-05-01 00:01:03.792"
"0c005686-0b70-4d40-9dfb-913c2b4f0a01","EURGBP","0.84328","0.8434","2017-05-01 00:01:03.793"
"5b8789ff-2a66-46e8-aca8-ba3f24b91cd3","GBPUSD","1.29324","1.29339","2017-05-01 00:01:03.854"
"8ee7180a-df32-4411-ab76-795148532b2f","GBPUSD","1.29324","1.29339","2017-05-01 00:01:03.995"
"4ef53c94-3228-42a4-a1a7-932e11df1b28","EURGBP","0.84328","0.8434","2017-05-01 00:01:04.023"
"7016a0c0-297f-4299-b85b-c1cd27e7c992","GBPJPY","143.955002","143.985001","2017-05-01 00:01:04.023"
"474efc11-599d-456a-9e14-aeb906c8da8a","GBPJPY","143.955002","143.985001","2017-05-01 00:01:04.195"
"d6011bd8-4a16-40dd-9648-f401cf7d4bcb","GBPUSD","1.29324","1.29339","2017-05-01 00:01:04.199"
"1c8e0210-4198-4cb2-9758-73d9c68c5681","EURGBP","0.84328","0.8434","2017-05-01 00:01:04.25"
"98f975ae-b410-4d00-812d-2c0f289f9ac6","GBPJPY","143.955994","143.985001","2017-05-01 00:01:04.273"
"75808e7b-be90-4284-b491-b577928e0a69","GBPUSD","1.29324","1.29339","2017-05-01 00:01:04.382"
"dc10ab53-1e6b-4bd2-a77e-be3fd8bf8a42","EURGBP","0.84328","0.8434","2017-05-01 00:01:04.383"
"e40ab9d5-8e2e-49e5-8748-5b3c43024a6d","GBPJPY","143.955994","143.985992","2017-05-01 00:01:04.383"
"8ee41b6c-e56b-4aa0-9e79-6204cc4785a0","GBPUSD","1.29324","1.29339","2017-05-01 00:01:04.656"

正如组中行号的额外信息一样。第一秒显然缺少rn2 = 1:

"rn1","rn2","grp","start_range","end_range","uid","instrument","bid","ask","dt","row_position"
"1","46","1","2017-05-01 00:01:00","2017-05-01 00:01:01","62d857cd-4830-4146-9516-dfff57abf9de","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.057","first"
"6","42","1","2017-05-01 00:01:00","2017-05-01 00:01:01","83e5eb95-4e02-4c5d-917f-770eb232c54b","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.096",""
"10","38","1","2017-05-01 00:01:00","2017-05-01 00:01:01","9f4a0c3b-d902-4d17-8e3d-f4867e58ed9f","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.29",""
"21","27","1","2017-05-01 00:01:00","2017-05-01 00:01:01","eb8691da-2480-4853-b49f-ba9ee9d86086","EURGBP","0.84319","0.8433","2017-05-01 00:01:00.485",""
"25","23","1","2017-05-01 00:01:00","2017-05-01 00:01:01","9685ee23-3365-4db5-84da-041f1d26c6cb","EURGBP","0.8432","0.8433","2017-05-01 00:01:00.507",""
"27","21","1","2017-05-01 00:01:00","2017-05-01 00:01:01","1b09afe4-5970-43c9-9101-81be1be9c032","EURGBP","0.84321","0.8433","2017-05-01 00:01:00.519",""
"31","17","1","2017-05-01 00:01:00","2017-05-01 00:01:01","eacc0e09-4712-4d7c-9974-1ff4f8884a60","EURGBP","0.84322","0.84332","2017-05-01 00:01:00.553",""
"33","15","1","2017-05-01 00:01:00","2017-05-01 00:01:01","37f5d872-1e21-4e92-bb7d-76c17e9e0278","EURGBP","0.84321","0.84332","2017-05-01 00:01:00.579",""
"37","11","1","2017-05-01 00:01:00","2017-05-01 00:01:01","1c9b66f9-6558-44dd-94b7-892689ed6a8e","EURGBP","0.84321","0.84333","2017-05-01 00:01:00.67",""
"40","8","1","2017-05-01 00:01:00","2017-05-01 00:01:01","90931fdf-5507-44cb-8b25-2e06a634caad","EURGBP","0.84323","0.84333","2017-05-01 00:01:00.672",""
"43","5","1","2017-05-01 00:01:00","2017-05-01 00:01:01","4d2cbbec-06e3-450c-857d-97bb9a4b4271","EURGBP","0.84323","0.84333","2017-05-01 00:01:00.833",""
"46","2","1","2017-05-01 00:01:00","2017-05-01 00:01:01","bf9293a1-d52e-4354-9583-c3c02178f985","EURGBP","0.84323","0.84333","2017-05-01 00:01:00.888",""
"2","35","2","2017-05-01 00:01:01","2017-05-01 00:01:02","96977442-9f60-4c02-87e9-c4ffa685e324","EURGBP","0.84323","0.84333","2017-05-01 00:01:01.017",""
"7","30","2","2017-05-01 00:01:01","2017-05-01 00:01:02","e8b9b67a-3708-4463-83d9-39b0286af14e","EURGBP","0.84323","0.84333","2017-05-01 00:01:01.193",""
"9","29","2","2017-05-01 00:01:01","2017-05-01 00:01:02","abbb50d6-e764-48e6-9172-98dbfe04f961","EURGBP","0.84324","0.84333","2017-05-01 00:01:01.195",""
"11","26","2","2017-05-01 00:01:01","2017-05-01 00:01:02","a1a5c7c9-8579-4477-ba55-f3bb6f9888c7","EURGBP","0.84324","0.84334","2017-05-01 00:01:01.231",""
"14","23","2","2017-05-01 00:01:01","2017-05-01 00:01:02","91db0d7e-422a-4236-90f9-dbd1ca569c14","EURGBP","0.84324","0.84334","2017-05-01 00:01:01.38",""
"17","20","2","2017-05-01 00:01:01","2017-05-01 00:01:02","763237fc-85ba-4263-9ab6-4348b08c5cfc","EURGBP","0.84324","0.84334","2017-05-01 00:01:01.461",""
"18","19","2","2017-05-01 00:01:01","2017-05-01 00:01:02","85a77007-c176-4ce0-9500-0d3919552fe5","EURGBP","0.84325","0.84334","2017-05-01 00:01:01.573",""
"20","17","2","2017-05-01 00:01:01","2017-05-01 00:01:02","a80c4a57-8c3f-42f4-a6c1-54ab8f9fa13e","EURGBP","0.84327","0.84334","2017-05-01 00:01:01.576",""
"23","14","2","2017-05-01 00:01:01","2017-05-01 00:01:02","65f794fb-278f-40b1-9974-142b0fc03125","EURGBP","0.84327","0.84338","2017-05-01 00:01:01.649",""
"27","10","2","2017-05-01 00:01:01","2017-05-01 00:01:02","cde342f0-5bd3-469b-9f98-6053f9ea153e","EURGBP","0.84327","0.84338","2017-05-01 00:01:01.777",""
"29","8","2","2017-05-01 00:01:01","2017-05-01 00:01:02","60af62a4-524b-4ad3-a4a8-1db39a8bccf5","EURGBP","0.84327","0.84339","2017-05-01 00:01:01.783",""
"31","6","2","2017-05-01 00:01:01","2017-05-01 00:01:02","7ae9a50f-35b7-4a09-a392-1459abf12122","EURGBP","0.84327","0.8434","2017-05-01 00:01:01.832",""
"32","5","2","2017-05-01 00:01:01","2017-05-01 00:01:02","00c63fdc-ddef-4dde-9f44-69d7482882e0","EURGBP","0.84328","0.8434","2017-05-01 00:01:01.846",""
"35","2","2","2017-05-01 00:01:01","2017-05-01 00:01:02","ca6ac100-8fd8-46e7-afc7-9966526a5c14","EURGBP","0.84329","0.8434","2017-05-01 00:01:01.96",""

它是某种Postgres错误吗?

非常欢迎任何想法如何修复它!

2 个答案:

答案 0 :(得分:0)

没有CTE和calendar-seconds的类似查询:

var response;
getDeviceId().then( function(value) { 
    response =  value;
});

答案 1 :(得分:0)

以下查询有效。问题出在WHERE子句位置。它必须在组内,否则行号会受到其他工具的影响。

WITH ranges as (
    SELECT dd as start_range, 
           dd + '1 seconds'::interval as end_range, 
           ROW_NUMBER() over () as grp
    FROM generate_series
            ( '2017-05-01 00:01:00'::timestamp 
            , '2017-05-01 00:01:00'::timestamp
            , '1 seconds'::interval) dd
), create_grp as (
    SELECT r.grp, r.start_range, r.end_range, p.*
    FROM prices p
    JOIN ranges r
      ON p.dt >= r.start_range
      AND p.dt < r.end_range
-- WHERE moves here from the end of the query
    WHERE instrument='EURGBP'
), minmax as ( 
   SELECT row_number() over (partition by grp
                             order by dt asc) as rn1,
          row_number() over (partition by grp
                             order by dt desc) as rn2,              
          create_grp.*
   FROM create_grp 
)
SELECT *,
       CASE WHEN rn1 = 1 and rn2 = 1 THEN 'first and last'
            WHEN rn1 = 1 THEN 'first'
            WHEN rn2 = 1 THEN 'last'
       END as row_position
FROM minmax
WHERE 
    1 IN (rn1, rn2) 
--    AND instrument='EURGBP'
ORDER BY dt
;

问题解决了,但它引发了新的问题:对查询make的这种改变是运行得非常慢!尽管有仪器索引。我提出了关于它的新问题并将其链接到此处。