473,750 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex Query 3 Tables combine ORDER BY + GROUP BY

dlite922
1,584 Recognized Expert Top Contributor
hey fellas (and ladies)

I need help with making a query.

My tables are: (Simplified)

case:
id
violatorID
number

violator:
id
name

trial:
id
caseID
date



A Case can have Multiple trials with different dates.

I need a query that gets returns the following information
caseID, caseNumber, violatorName, and the FIRST trial date.

So i need distinct cases, but it should return the earliest trial date. If there's on in March and one in July, it should return the one in March.

MY ATTEMPT:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT c.id as NUM, c.caseNumber, v.firstName, v.lastName, t.courtDate 
  3. FROM `case` as c, violator as v, trial as t 
  4. WHERE c.violatorID = v.id AND t.CaseID = c.id 
  5. GROUP BY c.id;
  6.  
  7.  
the above returns the last trial date (in above example, the one in July)

Then i tried this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT c.id as NUM, c.caseNumber, v.firstName, v.lastName, t.courtDate 
  3. FROM `case` as c, violator as v, trial as t 
  4. WHERE c.violatorID = v.id AND t.CaseID = c.id
  5. ORDER BY t.courtDate 
  6. GROUP BY c.id;
  7.  
  8.  
  9. //and got...
  10.  
  11. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY c.id' at line 1
  12.  
  13.  
  14.  
can't use Order By and Group By together? at least in this instance i can't.

any tips much appreciate it.

for the record, I know violator should have caseID, instead of vice versa.
Feb 18 '08 #1
1 4898
code green
1,726 Recognized Expert Top Contributor
can't use Order By and Group By together? at least in this instance i can't
No, you've just got them the wrong way around.
GROUP BY has to be before ORDER BY
Feb 18 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2976
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and attempting to perform a "left join" query to build a cross-reference table. The left join query is currently taking nearly 2 hours for MySQL to process, using Navicat as a front-end. My system specs are 1.4Mhz Pentium Processor with 1GB of RAM...
1
3456
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes in trees which form parent-child relationships, sort of like newsgroups. For example, the parent_id field points to another element. Indent_level is there for denormalization purposes, to avoid costly recursive issues in querying. The...
14
611
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN PartsJournal ON PartLocations.LocationID = PartsJournal.LocationID GROUP BY PartLocations.LocationName, PartsJournal.PartsLotNumber, PartsJournal.PartNumber, PartsJournal.LocationID HAVING (((Sum(PartsJournal.Quantity))>0) AND...
2
3548
by: Pete | last post by:
Before I get started with the question, does anyone have a (single) good book recommendation for database design? Not an Access-specific book, but something geared toward helping me figure out *what the user wants*. I've had brief formal education about data flow diagramming, but I'm looking for ... more, now that I'm actually running into problems I think stem from the fact that my users can't explain what they need done, compounded by...
1
3210
by: ravi | last post by:
I have created the following interest to calculate the interest for the following currency pairs. I have tried to combine them in macros using conditions but the next query that is run in the macro ends up deleting the previous interest value that has been generated by the query. For example if query 1 is run on the table with currency pair USD/CHF then the interest will be updated without any problem but if there is another entry in the...
4
2021
by: ED | last post by:
I am attempting to to write a query that has a numerous nested IIf statements. The problem that I am having is that it is to long of a query to be built in design mode and when I build it in sql mode after a certain point it give me the error message that the expression is to complex. Below is the sql code that I am using (this works so far, anything added to the code will give me the to complex error message.) SELECT .WONUM, .,...
3
6871
by: comp_databases_ms-sqlserver | last post by:
This post is related to SQL server 2000 and SQL Server 2005 all editions. Many of my stored procedures create temporary tables in the code. I want to find a way to find the query plan for these procs Repro --*********************************** use pubs go CREATE PROCEDURE Test @percentage int
0
7497
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i can make report from my database on the one sheet rather than querying each select statement ( I will use MS Query from the Excel) ========================================================= 1) select convert(varchar, create_date_time, 112) as Date,...
2
1334
by: mgsn | last post by:
I Have This this Query --------------------------------- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER proc . @userId int=null, -- filter: user id. overrides custid when supplied @custId int=null, -- filter: customer id @vcsId int=null, -- filter: status
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
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
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2226
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.