Solved

Group By Min, SQL Query

Posted on 2013-11-25
5
616 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 65

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 65

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 65

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

752 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