database query group union find latest

I'm dealing with a complex system and below is the simplest cut down I've managed to establish so please let me know if I was not clear on something.

DETAILS:
MetaRun is the main table which tracks executions.
Each metarun executions belongs to a metarungroups so you could have several metarun entries from the same metarungroupid.
Each MetaRun links to a DNAID and DNBID.

GOAL: What I need is given a specific MetaRun->RunGroupID how can I find the previous MetaRun->RunGroupID which matches the following criterias The list combination of DNA->DNB matches and the ConfigTypeID is the same

EXAMPLES: The previous MetaRunGroupID of MetaRunGroup5 would be MetaRunGroup1 because it has the middle two columns 1-1, 1-2 and 2-1 present as part of the group and the last column the same.

The previous MetaRunGroupID of MetaRunGroup6 would be MetaRunGroup2 because it has the middle two columns 1-1, 3-2 present as part of the group and the last column the same.

The previous MetaRunGroupID of MetaRunGroup4 would NOT!!!!!! be MetaRunGroup1 because although it has the middle two columns 1-1, 1-2, 2-1 present as part of the group and the last column is NOT!!!! the same.

MetaRunGroup1
(1, 1, 1, 2);
(1, 1, 2, 2);
(1, 2, 1, 2);

MetaRunGroup2
(2, 1, 1, 4);
(2, 3, 2, 4);

MetaRunGroup3
(3, 3, 2, 1);
(3, 1, 2, 1);
(3, 3, 1, 1);

MetaRunGroup4
(4, 1, 1, 3);
(4, 2, 1, 3);
(4, 1, 2, 3);

MetaRunGroup5
(5, 1, 1, 2);
(5, 2, 1, 2);
(5, 1, 2, 2);

MetaRunGroup6
(5, 3, 2, 4);
(5, 1, 1, 4);

Open in new window


CREATE DATABASE DNATEST;
USE DNATEST;

CREATE TABLE DNA (
ID              INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name            VARCHAR(255) NOT NULL,
UNIQUE(Name)
) ENGINE=InnoDB;

INSERT INTO DNA (Name) VALUES ("dna_bla1"), ("dna_bla2"), ("dna_bla3"), ("dna_bla4"), ("dna_bla5");

CREATE TABLE DNB (
ID              INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name            VARCHAR(255) NOT NULL,
UNIQUE(Name)
) ENGINE=InnoDB;
INSERT INTO DNB (Name) VALUES ("dnb_bla1"), ("dnb_bla2"), ("dnb_bla3"), ("dnb_bla4"), ("dnb_bla5");

CREATE TABLE CONFIGTYPE (
ID              INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name            VARCHAR(255) NOT NULL,
UNIQUE(Name)
) ENGINE=InnoDB;
INSERT INTO CONFIGTYPE (Name) VALUES ("config_1"), ("config_2"), ("config_3"), ("config_4"), ("config_5");


CREATE TABLE MetaRunGroups (
    ID              INT NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB;

INSERT INTO MetaRunGroups (ID) VALUES (1),(2),(3),(4),(5);

CREATE TABLE MetaRun (
ID                   INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
RunGroupID           INT NOT NULL,
DNAID                INT NOT NULL,
DNBID                INT NOT NULL,
CONFIGTYPEID         INT NOT NULL,
DateTime             DATETIME DEFAULT NULL,
FOREIGN KEY     (DNAID) REFERENCES DNA(ID) ON DELETE CASCADE,
FOREIGN KEY     (DNBID) REFERENCES DNB(ID) ON DELETE CASCADE,
FOREIGN KEY     (ConfigTypeID) REFERENCES CONFIGTYPE(ID) ON DELETE CASCADE,
FOREIGN KEY     (RunGroupID) REFERENCES MetaRunGroups(ID) ON DELETE CASCADE,
UNIQUE(RunGroupID, DNAID, DNBID, ConfigTypeID)
) ENGINE=InnoDB;

INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (1, 1, 1, 2);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (1, 1, 2, 2);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (1, 2, 1, 2);

INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (2, 1, 1, 4);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (2, 3, 2, 4);

INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (3, 3, 2, 1);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (3, 1, 2, 1);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (3, 3, 1, 1);

INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (4, 1, 1, 3);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (4, 2, 1, 3);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (4, 1, 2, 3);

INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (5, 1, 1, 2);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (5, 2, 1, 2);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (5, 1, 2, 2);

INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (5, 3, 2, 4);
INSERT INTO MetaRun (RunGroupID, DNAID, DNBID, CONFIGTYPEID) VALUES (5, 1, 1, 4);

Open in new window





schema.JPG
NewtonianBAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please check this article to find if that could help: http://www.experts-exchange.com/A_3203.html
0
 
NewtonianBAuthor Commented:
thanks alot angelll that actually helped got me closer to where i need to be
select RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime from metarun group by RunGroupID order by RunGroupID,DNAID ASC,DNBID ASC;

+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
|          1 | 111221                                  |            2 | NULL     |
|          2 | 1132                                    |            4 | NULL     |
|          3 | 123132                                  |            1 | NULL     |
|          4 | 111221                                  |            3 | NULL     |
|          5 | 111221                                  |            2 | NULL     |
|          6 | 1132                                    |            4 | NULL     |
+------------+-----------------------------------------+--------------+----------+

Then technicall my previous is the second row ordered by Date DESC where group_concat and configtypeid matches.
0
 
NewtonianBAuthor Commented:
One problem for RunGroupID # 5 the ordering of the concat works for the first query but for the second its ignored

why does
mysql> select RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime from metarun group by RunGroupID order by RunGroupID,DNAID ASC,DNBID ASC
+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
|          1 | 111221                                  |            2 | NULL     |
|          2 | 1132                                    |            4 | NULL     |
|          3 | 123132                                  |            1 | NULL     |
|          4 | 111221                                  |            3 | NULL     |
|          5 | 111221                                  |            2 | NULL     |
|          6 | 1132                                    |            4 | NULL     |
+------------+-----------------------------------------+--------------+----------+


mysql> select RunGroupID, GROUP_CONCAT(DNAID, DNBID SEPARATOR ''), ConfigTypeID, DateTime from metarun where configtypeid=2 group by RunGroupID order by RunGroupID,DNAID ASC,DNBID ASC;
+------------+-----------------------------------------+--------------+----------+
| RunGroupID | GROUP_CONCAT(DNAID, DNBID SEPARATOR '') | ConfigTypeID | DateTime |
+------------+-----------------------------------------+--------------+----------+
|          1 | 111221                                  |            2 | NULL     |
|          5 | 112112                                  |            2 | NULL     |
+------------+-----------------------------------------+--------------+----------+


|          5 | 111221                                  |            2 | NULL     |
vs
|          5 | 112112                                  |            2 | NULL     |
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.