Solved

database query group union find latest

Posted on 2011-02-22
3
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
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…

705 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