I have three related tables and need to select rows that show data from two tables based on a value (serial number) from the third. I am only interested in the max value of the serial number. I have tried multiple solutions suggested here on stackoverflow and I still cannot get my head around this.
A sample code for my tables with a straight forward SELECT for all values are available here: http://sqlfiddle.com/#!6/6b8f7/4/0
My end goal is to obtain a table like this:
reference groupname serialnum
C:123 Group2 3
C:125 Group1 4
C:126 Group1 1
Ordering with LIMIT does not seem to work. Any ideas how this might be addressed?
DDL + DML for Sample data:
CREATE TABLE pm_process
([pm_guid] int, [Descr] varchar(4), [usr_newref] varchar(5))
;
INSERT INTO pm_process
([pm_guid], [Descr], [usr_newref])
VALUES
(11111, 'aaaa', 'C:123'),
(22222, 'bbbb', 'C:125'),
(33333, 'cccc', 'C:126')
;
CREATE TABLE tps_group
([tps_title] varchar(6), [tps_guid] int)
;
INSERT INTO tps_group
([tps_title], [tps_guid])
VALUES
('Group1', 99999),
('Group2', 88888)
;
CREATE TABLE pm_process_assignment
([pm_group_guid] int, [pm_process_guid] int, [pm_serial_number] int)
;
INSERT INTO pm_process_assignment
([pm_group_guid], [pm_process_guid], [pm_serial_number])
VALUES
(99999, 11111, 1),
(99999, 11111, 2),
(88888, 11111, 3),
(88888, 22222, 1),
(99999, 22222, 2),
(88888, 22222, 3),
(99999, 22222, 4),
(99999, 33333, 1)
;