Solved

database query group union find latest

Posted on 2011-02-22
3
302 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:NewtonianB
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34954837
please check this article to find if that could help: http://www.experts-exchange.com/A_3203.html
0
 

Author Comment

by:NewtonianB
ID: 34956591
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
 

Author Comment

by:NewtonianB
ID: 34956621
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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question