Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

database query group union find latest

Posted on 2011-02-22
3
Medium Priority
?
307 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

926 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