select up-to-date values from Firebird DB












-1














I have a table in Firebird DB - see the picture






I need to know the up-to-date location information (LOCATION) of each device. Every device has unique serial number (SERIAL_NUMBER). The latest location is the row with highest all counter values (COUNTER1...COUNTER6).
I tried the following query, but it has to be complemented to get what I need. Unfortunately I don't have idea how to modify it.



SELECT LOCATION 
FROM TABLE
WHERE SERIAL_NUMBER IN (SELECT DISTINCT SERIAL_NUMBER
FROM TABLE)
ORDER BY COUNTER1, COUNTER2, COUNTER3, COUNTER4, COUNTER5, COUNTER6


From the pseudo-answer, now deleted:




I need to find a row for every SERIAL_NUMBER with the greatest vaule
of COUNTER1 and if equal for COUNTER2 and if equal for
COUNTER3...COUNTER6. So finally it will be a set of rows for each
SERIAL_NUMBER with the latest location information. The Firebird ver.
is 2.5 The design is as it is and cannot be changed. There is no
timestamp column in the table to find a latest location information in
a straight way, so the greatest counters value represents the most
current location.











share|improve this question
























  • Can you explain what you mean by "highest all counter values"? What would be the correct location for LSE3400308 if the row 204750 was missing, and why?
    – Corion
    Nov 19 '18 at 10:19










  • Please make desirable query output, so we would understand the logic.
    – Mr. Nice
    Nov 19 '18 at 11:23










  • Please explain what a "device" is. I don't see it in the table.
    – Gordon Linoff
    Nov 19 '18 at 12:02










  • Which Firebird version are you using?
    – a_horse_with_no_name
    Nov 19 '18 at 12:42










  • Please specify the relevant Firebird version and provide sample data and expected output as text. Also, what do you mean with "it has to be complemented to get what I need"?
    – Mark Rotteveel
    Nov 19 '18 at 14:57
















-1














I have a table in Firebird DB - see the picture






I need to know the up-to-date location information (LOCATION) of each device. Every device has unique serial number (SERIAL_NUMBER). The latest location is the row with highest all counter values (COUNTER1...COUNTER6).
I tried the following query, but it has to be complemented to get what I need. Unfortunately I don't have idea how to modify it.



SELECT LOCATION 
FROM TABLE
WHERE SERIAL_NUMBER IN (SELECT DISTINCT SERIAL_NUMBER
FROM TABLE)
ORDER BY COUNTER1, COUNTER2, COUNTER3, COUNTER4, COUNTER5, COUNTER6


From the pseudo-answer, now deleted:




I need to find a row for every SERIAL_NUMBER with the greatest vaule
of COUNTER1 and if equal for COUNTER2 and if equal for
COUNTER3...COUNTER6. So finally it will be a set of rows for each
SERIAL_NUMBER with the latest location information. The Firebird ver.
is 2.5 The design is as it is and cannot be changed. There is no
timestamp column in the table to find a latest location information in
a straight way, so the greatest counters value represents the most
current location.











share|improve this question
























  • Can you explain what you mean by "highest all counter values"? What would be the correct location for LSE3400308 if the row 204750 was missing, and why?
    – Corion
    Nov 19 '18 at 10:19










  • Please make desirable query output, so we would understand the logic.
    – Mr. Nice
    Nov 19 '18 at 11:23










  • Please explain what a "device" is. I don't see it in the table.
    – Gordon Linoff
    Nov 19 '18 at 12:02










  • Which Firebird version are you using?
    – a_horse_with_no_name
    Nov 19 '18 at 12:42










  • Please specify the relevant Firebird version and provide sample data and expected output as text. Also, what do you mean with "it has to be complemented to get what I need"?
    – Mark Rotteveel
    Nov 19 '18 at 14:57














-1












-1








-1







I have a table in Firebird DB - see the picture






I need to know the up-to-date location information (LOCATION) of each device. Every device has unique serial number (SERIAL_NUMBER). The latest location is the row with highest all counter values (COUNTER1...COUNTER6).
I tried the following query, but it has to be complemented to get what I need. Unfortunately I don't have idea how to modify it.



SELECT LOCATION 
FROM TABLE
WHERE SERIAL_NUMBER IN (SELECT DISTINCT SERIAL_NUMBER
FROM TABLE)
ORDER BY COUNTER1, COUNTER2, COUNTER3, COUNTER4, COUNTER5, COUNTER6


From the pseudo-answer, now deleted:




I need to find a row for every SERIAL_NUMBER with the greatest vaule
of COUNTER1 and if equal for COUNTER2 and if equal for
COUNTER3...COUNTER6. So finally it will be a set of rows for each
SERIAL_NUMBER with the latest location information. The Firebird ver.
is 2.5 The design is as it is and cannot be changed. There is no
timestamp column in the table to find a latest location information in
a straight way, so the greatest counters value represents the most
current location.











share|improve this question















I have a table in Firebird DB - see the picture






I need to know the up-to-date location information (LOCATION) of each device. Every device has unique serial number (SERIAL_NUMBER). The latest location is the row with highest all counter values (COUNTER1...COUNTER6).
I tried the following query, but it has to be complemented to get what I need. Unfortunately I don't have idea how to modify it.



SELECT LOCATION 
FROM TABLE
WHERE SERIAL_NUMBER IN (SELECT DISTINCT SERIAL_NUMBER
FROM TABLE)
ORDER BY COUNTER1, COUNTER2, COUNTER3, COUNTER4, COUNTER5, COUNTER6


From the pseudo-answer, now deleted:




I need to find a row for every SERIAL_NUMBER with the greatest vaule
of COUNTER1 and if equal for COUNTER2 and if equal for
COUNTER3...COUNTER6. So finally it will be a set of rows for each
SERIAL_NUMBER with the latest location information. The Firebird ver.
is 2.5 The design is as it is and cannot be changed. There is no
timestamp column in the table to find a latest location information in
a straight way, so the greatest counters value represents the most
current location.








sql firebird greatest-n-per-group






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 19:53









Arioch 'The

12.9k1647




12.9k1647










asked Nov 19 '18 at 9:44









cez

1




1












  • Can you explain what you mean by "highest all counter values"? What would be the correct location for LSE3400308 if the row 204750 was missing, and why?
    – Corion
    Nov 19 '18 at 10:19










  • Please make desirable query output, so we would understand the logic.
    – Mr. Nice
    Nov 19 '18 at 11:23










  • Please explain what a "device" is. I don't see it in the table.
    – Gordon Linoff
    Nov 19 '18 at 12:02










  • Which Firebird version are you using?
    – a_horse_with_no_name
    Nov 19 '18 at 12:42










  • Please specify the relevant Firebird version and provide sample data and expected output as text. Also, what do you mean with "it has to be complemented to get what I need"?
    – Mark Rotteveel
    Nov 19 '18 at 14:57


















  • Can you explain what you mean by "highest all counter values"? What would be the correct location for LSE3400308 if the row 204750 was missing, and why?
    – Corion
    Nov 19 '18 at 10:19










  • Please make desirable query output, so we would understand the logic.
    – Mr. Nice
    Nov 19 '18 at 11:23










  • Please explain what a "device" is. I don't see it in the table.
    – Gordon Linoff
    Nov 19 '18 at 12:02










  • Which Firebird version are you using?
    – a_horse_with_no_name
    Nov 19 '18 at 12:42










  • Please specify the relevant Firebird version and provide sample data and expected output as text. Also, what do you mean with "it has to be complemented to get what I need"?
    – Mark Rotteveel
    Nov 19 '18 at 14:57
















Can you explain what you mean by "highest all counter values"? What would be the correct location for LSE3400308 if the row 204750 was missing, and why?
– Corion
Nov 19 '18 at 10:19




Can you explain what you mean by "highest all counter values"? What would be the correct location for LSE3400308 if the row 204750 was missing, and why?
– Corion
Nov 19 '18 at 10:19












Please make desirable query output, so we would understand the logic.
– Mr. Nice
Nov 19 '18 at 11:23




Please make desirable query output, so we would understand the logic.
– Mr. Nice
Nov 19 '18 at 11:23












Please explain what a "device" is. I don't see it in the table.
– Gordon Linoff
Nov 19 '18 at 12:02




Please explain what a "device" is. I don't see it in the table.
– Gordon Linoff
Nov 19 '18 at 12:02












Which Firebird version are you using?
– a_horse_with_no_name
Nov 19 '18 at 12:42




Which Firebird version are you using?
– a_horse_with_no_name
Nov 19 '18 at 12:42












Please specify the relevant Firebird version and provide sample data and expected output as text. Also, what do you mean with "it has to be complemented to get what I need"?
– Mark Rotteveel
Nov 19 '18 at 14:57




Please specify the relevant Firebird version and provide sample data and expected output as text. Also, what do you mean with "it has to be complemented to get what I need"?
– Mark Rotteveel
Nov 19 '18 at 14:57












1 Answer
1






active

oldest

votes


















0














Here you have it: http://sqlfiddle.com/#!17/ff969/1



You would need to make obvious transitions to Firebird SQL flavor:




  • Firebird can not insert many rows with one statement.

  • Firebird uses select FIRST(1) ..... syntax instead of select .... LIMIT 1


Notice how I use one SELECT query after another, from simple queries building complex ones, testing every simple one (that it really returns what I need) before making mor complex constructions on top of them.



Read here more details about the process HOW to grow queries: https://stackoverflow.com/a/51398120/976391





PostgreSQL 9.6 Schema Setup:



create table tab(
id /* INTeger NOT NULL AUTO_INCREMENT */ serial primary key,
ser varchar(20) not null,
Loc varchar(20) not null,
c1 integer not null,
c2 integer not null,
c3 integer not null,
c4 integer not null,
c5 integer not null,
c6 integer not null
);

insert into tab(ser,loc,c1,c2,c3,c4,c5,c6) values
('LSE4300308', 'Address 1', 0,0,0, 0,0,0),
('LHD4x22414', 'Address 2', 601,504,8, 130,0,0),
('LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0),
('LSE4300308', 'Address 4', 2209,0,323, 0,0,0),
('LSE4300308', 'Address 5', 2257,0,330, 1661,926,0),
('LSE4300308', 'Address 6', 2278,0,330, 0,0,0),
('LSE4300308', 'Address 7', 2257,0,330, 1661,926,0),
('LSE4300308', 'Address 8', 2262,0,330, 1661,926,0),
('L873702373', 'Address 9', 12165,25467,1578, 2619,0,0),
('L873702373', 'Address A', 12165,25467,1578, 2619,0,0);

create index t_ser on tab(ser);
create /* descending */ index t_counters on tab(c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc);


Query 1:



Select * from tab


Results:



| id |        ser |       loc |    c1 |    c2 |   c3 |   c4 |  c5 | c6 |
|----|------------|-----------|-------|-------|------|------|-----|----|
| 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | LHD4x22414 | Address 2 | 601 | 504 | 8 | 130 | 0 | 0 |
| 3 | LHD4x22414 | Address 3 | 2385 | 1163 | 54 | 150 | 0 | 0 |
| 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
| 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
| 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
| 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
| 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
| 9 | L873702373 | Address 9 | 12165 | 25467 | 1578 | 2619 | 0 | 0 |
| 10 | L873702373 | Address A | 12165 | 25467 | 1578 | 2619 | 0 | 0 |


Query 2:



Select distinct ser from tab


Results:



|        ser |
|------------|
| LSE4300308 |
| L873702373 |
| LHD4x22414 |


Query 3:



Select /* First(10) */ * from tab
where ser = 'LSE4300308'
order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
limit 10


Results:



| id |        ser |       loc |   c1 | c2 |  c3 |   c4 |  c5 | c6 |
|----|------------|-----------|------|----|-----|------|-----|----|
| 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
| 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
| 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
| 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
| 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
| 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |


Query 4:



Select /* First(1) */ * from tab
where ser = 'LSE4300308'
order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
limit 1


Results:



| id |        ser |       loc |   c1 | c2 |  c3 | c4 | c5 | c6 |
|----|------------|-----------|------|----|-----|----|----|----|
| 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |


Query 5:



Select t1.ser, 
( Select /* First(1) */ loc from tab
Where ser = t1.ser
order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
Limit 1)
From (Select distinct ser from tab) as t1


Results:



|        ser |       loc |
|------------|-----------|
| LSE4300308 | Address 6 |
| L873702373 | Address 9 |
| LHD4x22414 | Address 3 |




Firebird 2.1 conversion.
Note - unless this query is run very seldom, you STILL may be better amending the database structure. No matter what is already there, you still can ADD new helper tables and you still can add new triggers to keep those new tables up to date automatically. For example one single add-on table "most recent ID for every serial" would make your life simpler, executing update or insert command every time new row is inserted into locations.



create table so53371903(
id integer primary key,
ser varchar(20) not null,
Loc varchar(20) not null,
c1 integer not null,
c2 integer not null,
c3 integer not null,
c4 integer not null,
c5 integer not null,
c6 integer not null
);

insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(0,'LSE4300308', 'Address 1', 0,0,0, 0,0,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(1,'LHD4x22414', 'Address 2', 601,504,8, 130,0,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(2,'LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(3,'LSE4300308', 'Address 4', 2209,0,323, 0,0,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(4,'LSE4300308', 'Address 5', 2257,0,330, 1661,926,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(5,'LSE4300308', 'Address 6', 2278,0,330, 0,0,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(6,'LSE4300308', 'Address 7', 2257,0,330, 1661,926,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(7,'LSE4300308', 'Address 8', 2262,0,330, 1661,926,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(8,'L873702373', 'Address 9', 12165,25467,1578, 2619,0,0)
;
insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
(9,'L873702373', 'Address A', 12165,25467,1578, 2619,0,0)
;

create index t_ser on so53371903(ser);
create descending index t_counters on so53371903(c1,c2,c3,c4,c5,c6);

Select * from so53371903;

Select distinct ser from so53371903;

Select first(10) * from so53371903
where ser = 'LSE4300308'
order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

Select first(1) * from so53371903
where ser = 'LSE4300308'
order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

Select t1.ser,
( Select First(1) loc from so53371903
Where ser = t1.ser
order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
)
From (Select distinct ser from so53371903) as t1;




Note: Address 9 and Address 10 have the same values in counters, so do Address 5 and Address 7. Because there is no "maximum counters" between then - the "winning" row would be chosen at random. It may be that today there will be one winner, and running the same query month or year later would give different result in those rows.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371903%2fselect-up-to-date-values-from-firebird-db%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here you have it: http://sqlfiddle.com/#!17/ff969/1



    You would need to make obvious transitions to Firebird SQL flavor:




    • Firebird can not insert many rows with one statement.

    • Firebird uses select FIRST(1) ..... syntax instead of select .... LIMIT 1


    Notice how I use one SELECT query after another, from simple queries building complex ones, testing every simple one (that it really returns what I need) before making mor complex constructions on top of them.



    Read here more details about the process HOW to grow queries: https://stackoverflow.com/a/51398120/976391





    PostgreSQL 9.6 Schema Setup:



    create table tab(
    id /* INTeger NOT NULL AUTO_INCREMENT */ serial primary key,
    ser varchar(20) not null,
    Loc varchar(20) not null,
    c1 integer not null,
    c2 integer not null,
    c3 integer not null,
    c4 integer not null,
    c5 integer not null,
    c6 integer not null
    );

    insert into tab(ser,loc,c1,c2,c3,c4,c5,c6) values
    ('LSE4300308', 'Address 1', 0,0,0, 0,0,0),
    ('LHD4x22414', 'Address 2', 601,504,8, 130,0,0),
    ('LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0),
    ('LSE4300308', 'Address 4', 2209,0,323, 0,0,0),
    ('LSE4300308', 'Address 5', 2257,0,330, 1661,926,0),
    ('LSE4300308', 'Address 6', 2278,0,330, 0,0,0),
    ('LSE4300308', 'Address 7', 2257,0,330, 1661,926,0),
    ('LSE4300308', 'Address 8', 2262,0,330, 1661,926,0),
    ('L873702373', 'Address 9', 12165,25467,1578, 2619,0,0),
    ('L873702373', 'Address A', 12165,25467,1578, 2619,0,0);

    create index t_ser on tab(ser);
    create /* descending */ index t_counters on tab(c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc);


    Query 1:



    Select * from tab


    Results:



    | id |        ser |       loc |    c1 |    c2 |   c3 |   c4 |  c5 | c6 |
    |----|------------|-----------|-------|-------|------|------|-----|----|
    | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 2 | LHD4x22414 | Address 2 | 601 | 504 | 8 | 130 | 0 | 0 |
    | 3 | LHD4x22414 | Address 3 | 2385 | 1163 | 54 | 150 | 0 | 0 |
    | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
    | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
    | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
    | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
    | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
    | 9 | L873702373 | Address 9 | 12165 | 25467 | 1578 | 2619 | 0 | 0 |
    | 10 | L873702373 | Address A | 12165 | 25467 | 1578 | 2619 | 0 | 0 |


    Query 2:



    Select distinct ser from tab


    Results:



    |        ser |
    |------------|
    | LSE4300308 |
    | L873702373 |
    | LHD4x22414 |


    Query 3:



    Select /* First(10) */ * from tab
    where ser = 'LSE4300308'
    order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
    limit 10


    Results:



    | id |        ser |       loc |   c1 | c2 |  c3 |   c4 |  c5 | c6 |
    |----|------------|-----------|------|----|-----|------|-----|----|
    | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
    | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
    | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
    | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
    | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
    | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |


    Query 4:



    Select /* First(1) */ * from tab
    where ser = 'LSE4300308'
    order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
    limit 1


    Results:



    | id |        ser |       loc |   c1 | c2 |  c3 | c4 | c5 | c6 |
    |----|------------|-----------|------|----|-----|----|----|----|
    | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |


    Query 5:



    Select t1.ser, 
    ( Select /* First(1) */ loc from tab
    Where ser = t1.ser
    order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
    Limit 1)
    From (Select distinct ser from tab) as t1


    Results:



    |        ser |       loc |
    |------------|-----------|
    | LSE4300308 | Address 6 |
    | L873702373 | Address 9 |
    | LHD4x22414 | Address 3 |




    Firebird 2.1 conversion.
    Note - unless this query is run very seldom, you STILL may be better amending the database structure. No matter what is already there, you still can ADD new helper tables and you still can add new triggers to keep those new tables up to date automatically. For example one single add-on table "most recent ID for every serial" would make your life simpler, executing update or insert command every time new row is inserted into locations.



    create table so53371903(
    id integer primary key,
    ser varchar(20) not null,
    Loc varchar(20) not null,
    c1 integer not null,
    c2 integer not null,
    c3 integer not null,
    c4 integer not null,
    c5 integer not null,
    c6 integer not null
    );

    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (0,'LSE4300308', 'Address 1', 0,0,0, 0,0,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (1,'LHD4x22414', 'Address 2', 601,504,8, 130,0,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (2,'LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (3,'LSE4300308', 'Address 4', 2209,0,323, 0,0,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (4,'LSE4300308', 'Address 5', 2257,0,330, 1661,926,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (5,'LSE4300308', 'Address 6', 2278,0,330, 0,0,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (6,'LSE4300308', 'Address 7', 2257,0,330, 1661,926,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (7,'LSE4300308', 'Address 8', 2262,0,330, 1661,926,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (8,'L873702373', 'Address 9', 12165,25467,1578, 2619,0,0)
    ;
    insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
    (9,'L873702373', 'Address A', 12165,25467,1578, 2619,0,0)
    ;

    create index t_ser on so53371903(ser);
    create descending index t_counters on so53371903(c1,c2,c3,c4,c5,c6);

    Select * from so53371903;

    Select distinct ser from so53371903;

    Select first(10) * from so53371903
    where ser = 'LSE4300308'
    order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

    Select first(1) * from so53371903
    where ser = 'LSE4300308'
    order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

    Select t1.ser,
    ( Select First(1) loc from so53371903
    Where ser = t1.ser
    order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
    )
    From (Select distinct ser from so53371903) as t1;




    Note: Address 9 and Address 10 have the same values in counters, so do Address 5 and Address 7. Because there is no "maximum counters" between then - the "winning" row would be chosen at random. It may be that today there will be one winner, and running the same query month or year later would give different result in those rows.






    share|improve this answer


























      0














      Here you have it: http://sqlfiddle.com/#!17/ff969/1



      You would need to make obvious transitions to Firebird SQL flavor:




      • Firebird can not insert many rows with one statement.

      • Firebird uses select FIRST(1) ..... syntax instead of select .... LIMIT 1


      Notice how I use one SELECT query after another, from simple queries building complex ones, testing every simple one (that it really returns what I need) before making mor complex constructions on top of them.



      Read here more details about the process HOW to grow queries: https://stackoverflow.com/a/51398120/976391





      PostgreSQL 9.6 Schema Setup:



      create table tab(
      id /* INTeger NOT NULL AUTO_INCREMENT */ serial primary key,
      ser varchar(20) not null,
      Loc varchar(20) not null,
      c1 integer not null,
      c2 integer not null,
      c3 integer not null,
      c4 integer not null,
      c5 integer not null,
      c6 integer not null
      );

      insert into tab(ser,loc,c1,c2,c3,c4,c5,c6) values
      ('LSE4300308', 'Address 1', 0,0,0, 0,0,0),
      ('LHD4x22414', 'Address 2', 601,504,8, 130,0,0),
      ('LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0),
      ('LSE4300308', 'Address 4', 2209,0,323, 0,0,0),
      ('LSE4300308', 'Address 5', 2257,0,330, 1661,926,0),
      ('LSE4300308', 'Address 6', 2278,0,330, 0,0,0),
      ('LSE4300308', 'Address 7', 2257,0,330, 1661,926,0),
      ('LSE4300308', 'Address 8', 2262,0,330, 1661,926,0),
      ('L873702373', 'Address 9', 12165,25467,1578, 2619,0,0),
      ('L873702373', 'Address A', 12165,25467,1578, 2619,0,0);

      create index t_ser on tab(ser);
      create /* descending */ index t_counters on tab(c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc);


      Query 1:



      Select * from tab


      Results:



      | id |        ser |       loc |    c1 |    c2 |   c3 |   c4 |  c5 | c6 |
      |----|------------|-----------|-------|-------|------|------|-----|----|
      | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |
      | 2 | LHD4x22414 | Address 2 | 601 | 504 | 8 | 130 | 0 | 0 |
      | 3 | LHD4x22414 | Address 3 | 2385 | 1163 | 54 | 150 | 0 | 0 |
      | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
      | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
      | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
      | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
      | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
      | 9 | L873702373 | Address 9 | 12165 | 25467 | 1578 | 2619 | 0 | 0 |
      | 10 | L873702373 | Address A | 12165 | 25467 | 1578 | 2619 | 0 | 0 |


      Query 2:



      Select distinct ser from tab


      Results:



      |        ser |
      |------------|
      | LSE4300308 |
      | L873702373 |
      | LHD4x22414 |


      Query 3:



      Select /* First(10) */ * from tab
      where ser = 'LSE4300308'
      order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
      limit 10


      Results:



      | id |        ser |       loc |   c1 | c2 |  c3 |   c4 |  c5 | c6 |
      |----|------------|-----------|------|----|-----|------|-----|----|
      | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
      | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
      | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
      | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
      | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
      | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |


      Query 4:



      Select /* First(1) */ * from tab
      where ser = 'LSE4300308'
      order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
      limit 1


      Results:



      | id |        ser |       loc |   c1 | c2 |  c3 | c4 | c5 | c6 |
      |----|------------|-----------|------|----|-----|----|----|----|
      | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |


      Query 5:



      Select t1.ser, 
      ( Select /* First(1) */ loc from tab
      Where ser = t1.ser
      order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
      Limit 1)
      From (Select distinct ser from tab) as t1


      Results:



      |        ser |       loc |
      |------------|-----------|
      | LSE4300308 | Address 6 |
      | L873702373 | Address 9 |
      | LHD4x22414 | Address 3 |




      Firebird 2.1 conversion.
      Note - unless this query is run very seldom, you STILL may be better amending the database structure. No matter what is already there, you still can ADD new helper tables and you still can add new triggers to keep those new tables up to date automatically. For example one single add-on table "most recent ID for every serial" would make your life simpler, executing update or insert command every time new row is inserted into locations.



      create table so53371903(
      id integer primary key,
      ser varchar(20) not null,
      Loc varchar(20) not null,
      c1 integer not null,
      c2 integer not null,
      c3 integer not null,
      c4 integer not null,
      c5 integer not null,
      c6 integer not null
      );

      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (0,'LSE4300308', 'Address 1', 0,0,0, 0,0,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (1,'LHD4x22414', 'Address 2', 601,504,8, 130,0,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (2,'LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (3,'LSE4300308', 'Address 4', 2209,0,323, 0,0,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (4,'LSE4300308', 'Address 5', 2257,0,330, 1661,926,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (5,'LSE4300308', 'Address 6', 2278,0,330, 0,0,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (6,'LSE4300308', 'Address 7', 2257,0,330, 1661,926,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (7,'LSE4300308', 'Address 8', 2262,0,330, 1661,926,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (8,'L873702373', 'Address 9', 12165,25467,1578, 2619,0,0)
      ;
      insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
      (9,'L873702373', 'Address A', 12165,25467,1578, 2619,0,0)
      ;

      create index t_ser on so53371903(ser);
      create descending index t_counters on so53371903(c1,c2,c3,c4,c5,c6);

      Select * from so53371903;

      Select distinct ser from so53371903;

      Select first(10) * from so53371903
      where ser = 'LSE4300308'
      order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

      Select first(1) * from so53371903
      where ser = 'LSE4300308'
      order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

      Select t1.ser,
      ( Select First(1) loc from so53371903
      Where ser = t1.ser
      order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
      )
      From (Select distinct ser from so53371903) as t1;




      Note: Address 9 and Address 10 have the same values in counters, so do Address 5 and Address 7. Because there is no "maximum counters" between then - the "winning" row would be chosen at random. It may be that today there will be one winner, and running the same query month or year later would give different result in those rows.






      share|improve this answer
























        0












        0








        0






        Here you have it: http://sqlfiddle.com/#!17/ff969/1



        You would need to make obvious transitions to Firebird SQL flavor:




        • Firebird can not insert many rows with one statement.

        • Firebird uses select FIRST(1) ..... syntax instead of select .... LIMIT 1


        Notice how I use one SELECT query after another, from simple queries building complex ones, testing every simple one (that it really returns what I need) before making mor complex constructions on top of them.



        Read here more details about the process HOW to grow queries: https://stackoverflow.com/a/51398120/976391





        PostgreSQL 9.6 Schema Setup:



        create table tab(
        id /* INTeger NOT NULL AUTO_INCREMENT */ serial primary key,
        ser varchar(20) not null,
        Loc varchar(20) not null,
        c1 integer not null,
        c2 integer not null,
        c3 integer not null,
        c4 integer not null,
        c5 integer not null,
        c6 integer not null
        );

        insert into tab(ser,loc,c1,c2,c3,c4,c5,c6) values
        ('LSE4300308', 'Address 1', 0,0,0, 0,0,0),
        ('LHD4x22414', 'Address 2', 601,504,8, 130,0,0),
        ('LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0),
        ('LSE4300308', 'Address 4', 2209,0,323, 0,0,0),
        ('LSE4300308', 'Address 5', 2257,0,330, 1661,926,0),
        ('LSE4300308', 'Address 6', 2278,0,330, 0,0,0),
        ('LSE4300308', 'Address 7', 2257,0,330, 1661,926,0),
        ('LSE4300308', 'Address 8', 2262,0,330, 1661,926,0),
        ('L873702373', 'Address 9', 12165,25467,1578, 2619,0,0),
        ('L873702373', 'Address A', 12165,25467,1578, 2619,0,0);

        create index t_ser on tab(ser);
        create /* descending */ index t_counters on tab(c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc);


        Query 1:



        Select * from tab


        Results:



        | id |        ser |       loc |    c1 |    c2 |   c3 |   c4 |  c5 | c6 |
        |----|------------|-----------|-------|-------|------|------|-----|----|
        | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |
        | 2 | LHD4x22414 | Address 2 | 601 | 504 | 8 | 130 | 0 | 0 |
        | 3 | LHD4x22414 | Address 3 | 2385 | 1163 | 54 | 150 | 0 | 0 |
        | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
        | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
        | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
        | 9 | L873702373 | Address 9 | 12165 | 25467 | 1578 | 2619 | 0 | 0 |
        | 10 | L873702373 | Address A | 12165 | 25467 | 1578 | 2619 | 0 | 0 |


        Query 2:



        Select distinct ser from tab


        Results:



        |        ser |
        |------------|
        | LSE4300308 |
        | L873702373 |
        | LHD4x22414 |


        Query 3:



        Select /* First(10) */ * from tab
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        limit 10


        Results:



        | id |        ser |       loc |   c1 | c2 |  c3 |   c4 |  c5 | c6 |
        |----|------------|-----------|------|----|-----|------|-----|----|
        | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
        | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
        | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
        | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |


        Query 4:



        Select /* First(1) */ * from tab
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        limit 1


        Results:



        | id |        ser |       loc |   c1 | c2 |  c3 | c4 | c5 | c6 |
        |----|------------|-----------|------|----|-----|----|----|----|
        | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |


        Query 5:



        Select t1.ser, 
        ( Select /* First(1) */ loc from tab
        Where ser = t1.ser
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        Limit 1)
        From (Select distinct ser from tab) as t1


        Results:



        |        ser |       loc |
        |------------|-----------|
        | LSE4300308 | Address 6 |
        | L873702373 | Address 9 |
        | LHD4x22414 | Address 3 |




        Firebird 2.1 conversion.
        Note - unless this query is run very seldom, you STILL may be better amending the database structure. No matter what is already there, you still can ADD new helper tables and you still can add new triggers to keep those new tables up to date automatically. For example one single add-on table "most recent ID for every serial" would make your life simpler, executing update or insert command every time new row is inserted into locations.



        create table so53371903(
        id integer primary key,
        ser varchar(20) not null,
        Loc varchar(20) not null,
        c1 integer not null,
        c2 integer not null,
        c3 integer not null,
        c4 integer not null,
        c5 integer not null,
        c6 integer not null
        );

        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (0,'LSE4300308', 'Address 1', 0,0,0, 0,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (1,'LHD4x22414', 'Address 2', 601,504,8, 130,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (2,'LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (3,'LSE4300308', 'Address 4', 2209,0,323, 0,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (4,'LSE4300308', 'Address 5', 2257,0,330, 1661,926,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (5,'LSE4300308', 'Address 6', 2278,0,330, 0,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (6,'LSE4300308', 'Address 7', 2257,0,330, 1661,926,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (7,'LSE4300308', 'Address 8', 2262,0,330, 1661,926,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (8,'L873702373', 'Address 9', 12165,25467,1578, 2619,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (9,'L873702373', 'Address A', 12165,25467,1578, 2619,0,0)
        ;

        create index t_ser on so53371903(ser);
        create descending index t_counters on so53371903(c1,c2,c3,c4,c5,c6);

        Select * from so53371903;

        Select distinct ser from so53371903;

        Select first(10) * from so53371903
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

        Select first(1) * from so53371903
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

        Select t1.ser,
        ( Select First(1) loc from so53371903
        Where ser = t1.ser
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        )
        From (Select distinct ser from so53371903) as t1;




        Note: Address 9 and Address 10 have the same values in counters, so do Address 5 and Address 7. Because there is no "maximum counters" between then - the "winning" row would be chosen at random. It may be that today there will be one winner, and running the same query month or year later would give different result in those rows.






        share|improve this answer












        Here you have it: http://sqlfiddle.com/#!17/ff969/1



        You would need to make obvious transitions to Firebird SQL flavor:




        • Firebird can not insert many rows with one statement.

        • Firebird uses select FIRST(1) ..... syntax instead of select .... LIMIT 1


        Notice how I use one SELECT query after another, from simple queries building complex ones, testing every simple one (that it really returns what I need) before making mor complex constructions on top of them.



        Read here more details about the process HOW to grow queries: https://stackoverflow.com/a/51398120/976391





        PostgreSQL 9.6 Schema Setup:



        create table tab(
        id /* INTeger NOT NULL AUTO_INCREMENT */ serial primary key,
        ser varchar(20) not null,
        Loc varchar(20) not null,
        c1 integer not null,
        c2 integer not null,
        c3 integer not null,
        c4 integer not null,
        c5 integer not null,
        c6 integer not null
        );

        insert into tab(ser,loc,c1,c2,c3,c4,c5,c6) values
        ('LSE4300308', 'Address 1', 0,0,0, 0,0,0),
        ('LHD4x22414', 'Address 2', 601,504,8, 130,0,0),
        ('LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0),
        ('LSE4300308', 'Address 4', 2209,0,323, 0,0,0),
        ('LSE4300308', 'Address 5', 2257,0,330, 1661,926,0),
        ('LSE4300308', 'Address 6', 2278,0,330, 0,0,0),
        ('LSE4300308', 'Address 7', 2257,0,330, 1661,926,0),
        ('LSE4300308', 'Address 8', 2262,0,330, 1661,926,0),
        ('L873702373', 'Address 9', 12165,25467,1578, 2619,0,0),
        ('L873702373', 'Address A', 12165,25467,1578, 2619,0,0);

        create index t_ser on tab(ser);
        create /* descending */ index t_counters on tab(c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc);


        Query 1:



        Select * from tab


        Results:



        | id |        ser |       loc |    c1 |    c2 |   c3 |   c4 |  c5 | c6 |
        |----|------------|-----------|-------|-------|------|------|-----|----|
        | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |
        | 2 | LHD4x22414 | Address 2 | 601 | 504 | 8 | 130 | 0 | 0 |
        | 3 | LHD4x22414 | Address 3 | 2385 | 1163 | 54 | 150 | 0 | 0 |
        | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
        | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
        | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
        | 9 | L873702373 | Address 9 | 12165 | 25467 | 1578 | 2619 | 0 | 0 |
        | 10 | L873702373 | Address A | 12165 | 25467 | 1578 | 2619 | 0 | 0 |


        Query 2:



        Select distinct ser from tab


        Results:



        |        ser |
        |------------|
        | LSE4300308 |
        | L873702373 |
        | LHD4x22414 |


        Query 3:



        Select /* First(10) */ * from tab
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        limit 10


        Results:



        | id |        ser |       loc |   c1 | c2 |  c3 |   c4 |  c5 | c6 |
        |----|------------|-----------|------|----|-----|------|-----|----|
        | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |
        | 8 | LSE4300308 | Address 8 | 2262 | 0 | 330 | 1661 | 926 | 0 |
        | 5 | LSE4300308 | Address 5 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 7 | LSE4300308 | Address 7 | 2257 | 0 | 330 | 1661 | 926 | 0 |
        | 4 | LSE4300308 | Address 4 | 2209 | 0 | 323 | 0 | 0 | 0 |
        | 1 | LSE4300308 | Address 1 | 0 | 0 | 0 | 0 | 0 | 0 |


        Query 4:



        Select /* First(1) */ * from tab
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        limit 1


        Results:



        | id |        ser |       loc |   c1 | c2 |  c3 | c4 | c5 | c6 |
        |----|------------|-----------|------|----|-----|----|----|----|
        | 6 | LSE4300308 | Address 6 | 2278 | 0 | 330 | 0 | 0 | 0 |


        Query 5:



        Select t1.ser, 
        ( Select /* First(1) */ loc from tab
        Where ser = t1.ser
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        Limit 1)
        From (Select distinct ser from tab) as t1


        Results:



        |        ser |       loc |
        |------------|-----------|
        | LSE4300308 | Address 6 |
        | L873702373 | Address 9 |
        | LHD4x22414 | Address 3 |




        Firebird 2.1 conversion.
        Note - unless this query is run very seldom, you STILL may be better amending the database structure. No matter what is already there, you still can ADD new helper tables and you still can add new triggers to keep those new tables up to date automatically. For example one single add-on table "most recent ID for every serial" would make your life simpler, executing update or insert command every time new row is inserted into locations.



        create table so53371903(
        id integer primary key,
        ser varchar(20) not null,
        Loc varchar(20) not null,
        c1 integer not null,
        c2 integer not null,
        c3 integer not null,
        c4 integer not null,
        c5 integer not null,
        c6 integer not null
        );

        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (0,'LSE4300308', 'Address 1', 0,0,0, 0,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (1,'LHD4x22414', 'Address 2', 601,504,8, 130,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (2,'LHD4x22414', 'Address 3', 2385,1163,54, 150,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (3,'LSE4300308', 'Address 4', 2209,0,323, 0,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (4,'LSE4300308', 'Address 5', 2257,0,330, 1661,926,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (5,'LSE4300308', 'Address 6', 2278,0,330, 0,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (6,'LSE4300308', 'Address 7', 2257,0,330, 1661,926,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (7,'LSE4300308', 'Address 8', 2262,0,330, 1661,926,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (8,'L873702373', 'Address 9', 12165,25467,1578, 2619,0,0)
        ;
        insert into so53371903(id,ser,loc,c1,c2,c3,c4,c5,c6) values
        (9,'L873702373', 'Address A', 12165,25467,1578, 2619,0,0)
        ;

        create index t_ser on so53371903(ser);
        create descending index t_counters on so53371903(c1,c2,c3,c4,c5,c6);

        Select * from so53371903;

        Select distinct ser from so53371903;

        Select first(10) * from so53371903
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

        Select first(1) * from so53371903
        where ser = 'LSE4300308'
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc;

        Select t1.ser,
        ( Select First(1) loc from so53371903
        Where ser = t1.ser
        order by c1 desc,c2 desc,c3 desc,c4 desc,c5 desc,c6 desc
        )
        From (Select distinct ser from so53371903) as t1;




        Note: Address 9 and Address 10 have the same values in counters, so do Address 5 and Address 7. Because there is no "maximum counters" between then - the "winning" row would be chosen at random. It may be that today there will be one winner, and running the same query month or year later would give different result in those rows.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 13:57









        Arioch 'The

        12.9k1647




        12.9k1647






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371903%2fselect-up-to-date-values-from-firebird-db%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?