Solved

Group By Min, SQL Query

Posted on 2013-11-25
5
618 Views
Last Modified: 2013-11-25
I have a query that is showing multiple results.  I would like to see the minimum sequence number. .  .

SELECT     WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, MIN(SEQUENCE_NO)
FROM         OPERATION
GROUP BY WORKORDER_TYPE, WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, STATUS, SEQUENCE_NO
HAVING      (WORKORDER_TYPE = 'W') AND (STATUS = 'R')
ORDER BY WORKORDER_BASE_ID

This is my table

WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, SEQUENCE_NO
100, 1, 1, 10, C
100, 1, 1, 20, R
100, 1, 1, 30, R
100, 1, 1, 40, R

I would like to see this, please. . .but I'm missing something

WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, SEQUENCE_NO
100, 1, 1, 20, R

My query results are this. . .
WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, SEQUENCE_NO
100, 1, 1, 20, R
100, 1, 1, 30, R
100, 1, 1, 40, R

TIA!
0
Comment
Question by:CadenceAerospaceIT
[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
  • 3
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39675983
>HAVING      (WORKORDER_TYPE = 'W') AND (STATUS = 'R')
For starters, HAVING is essentially a WHERE clause on an aggregate such as SUM, COUNT, MIN, etc., so what you have here should be in a WHERE clause.

SQL Server GROUP BY Solutions has a couple of demos that would be a good read.
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 300 total points
ID: 39675990
>SELECT     WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID,
MIN(SEQUENCE_NO)

>GROUP BY WORKORDER_TYPE, WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, STATUS, SEQUENCE_NO

Also, the GROUP BY clause should contain all columns in the SELECT that aren't part of an aggregate, such as BASE, LOT, and SPLIT, so what's the deal-i-o with having TYPE and STATUS columns in the GROUP BY clause?
0
 
LVL 11

Assisted Solution

by:Simone B
Simone B earned 200 total points
ID: 39675999
You are also grouping by SEQUENCE_NO, although that is in your MIN aggregate. You need to remove SEQUENCE_NO from the GROUP BY. As Jim says, GROUP BY should contain the columns in the SELECT  that aren't part of an aggregate.
0
 

Author Comment

by:CadenceAerospaceIT
ID: 39676634
jimhorn . .

Where throws back a SQL error . .

>HAVING      (WORKORDER_TYPE = 'W') AND (STATUS = 'R')
For starters, HAVING is essentially a WHERE clause on an aggregate such as SUM, COUNT, MIN, etc., so what you have here should be in a WHERE clause.

SELECT     WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, MIN(SEQUENCE_NO)
FROM         OPERATION
GROUP BY WORKORDER_TYPE, WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, STATUS, SEQUENCE_NO
WHERE      WORKORDER_TYPE = 'W' AND STATUS = 'R'
ORDER BY WORKORDER_BASE_ID
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39676657
SWEATY - FEET - WILL - GIVE - HORRIBLE - ODORS

SELECT - FROM - WHERE - GROUP BY - HAVING - ORDER BY

Looking at your query, the WHERE needs to be before the GROUP BY.

Good luck getting that out of your head.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

635 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