Solved

database query group union find latest

Posted on 2011-02-22
3
300 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
Comment Utility
please check this article to find if that could help: http://www.experts-exchange.com/A_3203.html
0
 

Author Comment

by:NewtonianB
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now