Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Group By Min, SQL Query

Posted on 2013-11-25
5
Medium Priority
?
629 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
  • 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 1200 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 800 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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 part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

886 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