1

postgresql select get the same id

when i handle BI data,I found the same data is not i need,so,i want use postgresql handle data, I want get the reuslt like this style,the postgesql select how write ?

enter image description here

    -- ----------------------------
-- Table structure for data_test
-- ----------------------------
DROP TABLE IF EXISTS "public"."data_test";
CREATE TABLE "public"."data_test" (
  "c_id" int4 NOT NULL,
  "n_id" int4,
  "b_id" int4
)
;

-- ----------------------------
-- Records of data_test
-- ----------------------------
INSERT INTO "public"."data_test" VALUES (58748, 587, 55);
INSERT INTO "public"."data_test" VALUES (5895, 587, 55);
INSERT INTO "public"."data_test" VALUES (12131, 567, 53);
INSERT INTO "public"."data_test" VALUES (33412, 568, 54);
INSERT INTO "public"."data_test" VALUES (5841, 569, 52);
INSERT INTO "public"."data_test" VALUES (587, 500, 51);
INSERT INTO "public"."data_test" VALUES (5871, 500, 51);

1 Answer 1

1

You can use window functions:

select c_id, n_id,
       (case when row_number() over (partition by n_id order by c_id) = 1
             then b_id
        end) as b_id
from data_test
order by n_id, c_id;

Note that this returns NULL rather then - -- which is what I assume you actually intend by - given that - is not compatible with int4.

Here is a db<>fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.