473,545 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get a certain element in a GROUP BY

Hi,

I am trying to control which element gets picked when I do a group by,
but I can't figure out how to do it.

First some example data:

CREATE TABLE t ( id int not null primary key auto_increment, group_id
int not null, level int not null);
INSERT INTO t VALUES (1,1,2);
INSERT INTO t VALUES (2,1,3);
INSERT INTO t VALUES (3,1,1);
INSERT INTO t VALUES (4,2,1);
INSERT INTO t VALUES (5,2,1);
INSERT INTO t VALUES (6,2,1);
INSERT INTO t VALUES (7,4,4);
INSERT INTO t VALUES (8,1,1);
INSERT INTO t VALUES (9,2,2);

mysql> SELECT * from t;
+----+----------+-------+
| id | group_id | level |
+----+----------+-------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 1 |
| 4 | 2 | 1 |
| 5 | 2 | 1 |
| 6 | 2 | 1 |
| 7 | 4 | 4 |
| 8 | 1 | 1 |
| 9 | 2 | 2 |
+----+----------+-------+
9 rows in set (0.00 sec)
The real schema is of course much more complex.

I want to get one line for each "group_id" and it must be the one with
the lowest "level,id".

Adding "order by level" just orders the results when they are already
grouped.

mysql> select *,MIN(level) as min_level from t group by group_id
order by level,id;
+----+----------+-------+-----------+
| id | group_id | level | min_level |
+----+----------+-------+-----------+
| 4 | 2 | 1 | 1 |
| 1 | 1 | 2 | 1 |
| 7 | 4 | 4 | 4 |
+----+----------+-------+-----------+

In this case I wanted to get row 3 (with level 1) for group 1 for
example.

I think I understand why MySQL can't guess what I want in this case;
how can I explain it better in SQL? :-)
- ask

--
http://www.askbjoernhansen.com/
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 1935

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3158
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm running Tomcat 4.1.27 under Eclipse 2.1.0 using the Sysdeo Tomcat plugin using j2re1.4.1_02 under Windows 2000 SP4. I've digested this down to a small...
8
15470
by: SAN CAZIANO | last post by:
i have to do in the onkeypress or in onchange the float (real) field validation I try something: function ValidaCampo(nomeCampo,TotInteri,TotDecimali) { DecimalPos=NomeCampo.value.Indexof(','); Lungh=TotInteri+TotDecimali+1; //sum the ineger + decimal + decimal separator
2
20225
by: arne | last post by:
Hi what would be the easiest way to find all tag's with a certain name (or class) below (within) a certain tag. Pref jscript DOM thx Arne
1
1759
by: jrmsmo | last post by:
Hi there, I have an interesting problem that maybe you pros can suggest how I solve. I'm working with a third party program that serializes an XML document (it was obviously not designed with schema in mind). I created a schema from this document. It works fine. Except for some unknown reason, in a small part of the XML document, this program...
0
1295
by: eSapient | last post by:
For the following XSD: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="E1" type="CT1"/> <xs:complexType name="CT1"> <xs:sequence> <xs:element name="CT1_E1"
5
3402
by: j.tremlett | last post by:
Hi, I have read various articles on this and I have had a few suggestions but would like to know which is correct. I have an XML file with the element <root> Within <root> I can have a number of child elements occuring in any order and any number of times.
1
1445
by: Tookelso | last post by:
Hello, I would like to have a group of elements which are *required* in one context, but each one is *optional* in another context. For example: I have a configuration file which has a <defaultselement. There are 5 settings which *must* be defined in the <defaultssection. The configuration file also has <databaseelements. A database...
5
1665
by: thisis | last post by:
Hi All, Hi All, (this is not the same topic as the my previous topic) What objects/methods/properties does VBScript offer for: Assuring/guarantee/make certain that ASP/VBSCript an ELEMENT - e.g. <img ... / - was FULLY Loaded into web Browser
3
1767
by: =?Utf-8?B?TWF4IFR1cmF2YW5p?= | last post by:
Hi, Is there any way I can cause a login page to appear, using Forms authentication of course, when access a particular page only. In my case, I am building a site for rental properties. There are a group of files ( HTML, ASPX files ) that are freely publicly accessable. There are certain pages however that my client will use to update...
0
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5962
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1869
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.