Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Group By Min, SQL Query

Posted on 2013-11-25
5
Medium Priority
?
623 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 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.

722 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