Solved

SQL Query

Posted on 2015-01-06
17
162 Views
Last Modified: 2015-01-06
Hello,

I'm just having trouble with this simple query.  The following query is giving me two results.  I only want one.  It needs to find the ID number I specify and take the max POS_NR, then give me all the other fields in only that record.  What am I doing wrong?

Here's the query:

SELECT Max(SYSADM_BW_AUFTR_POS.POS_NR) AS MaxOfPOS_NR, SYSADM_BW_AUFTR_POS.PROD_BEZ1, SYSADM_BW_AUFTR_POS.PROD_BEZ2
FROM SYSADM_BW_AUFTR_POS
WHERE (((SYSADM_BW_AUFTR_POS.ID)=12200838))
GROUP BY SYSADM_BW_AUFTR_POS.PROD_BEZ1, SYSADM_BW_AUFTR_POS.PROD_BEZ2
ORDER BY Max(SYSADM_BW_AUFTR_POS.POS_NR);


And here's the result:

MaxOfPOS_NR      PROD_BEZ1                            PROD_BEZ2
1                            Dluminum Series Frames      Aluminum Covered Steel Profiles
2                            Aesigner Series Frames      Steel Profiles

Thanks!
Joel
0
Comment
Question by:Genius123
  • 6
  • 4
  • 3
  • +2
17 Comments
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Use a subquery to get the max(POS_NR)

select *  From SYSADM_BW_AUFTR_POS
where pos_nr = (
                               select max(sysadm_BW_AUFTR.POS_NR)
                               FROM SYSADM_BW_AUFTR_POS
                               WHERE ID=12200838
                              )
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility

1.

Since there's only one table in your SQL Statement, lose all the table references other than in the FROM clause.  Doing that, and deleting the unnecessary parentheses marks, makes the query much easier to read.
SELECT Max(POS_NR) AS MaxOfPOS_NR, PROD_BEZ1, PROD_BEZ2
FROM SYSADM_BW_AUFTR_POS
WHERE ID = 12200838 
GROUP BY PROD_BEZ1, PROD_BEZ2

Open in new window

2.

Looks like we're grouping by two columns PROD_BEZ1 and PROD_BEZ2, so the return set will be all possible combinations of these two columns AND THEN the MaxOfPOS_NR
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility

3.

Then do the max in an in clause (or subquery, whatever floats your boat), and return the other values in the main query.
SELECT PROD_BEZ1, PROD_BEZ2, POS_NR as MaxOfPOSNbr
FROM SYSADM_BW_AUFTR_POS 
WHERE POS_NR IN (SELECT Max(POS_NR) FROM SYSADM_BW_AUFTR_POS WHERE ID = 12200838) 

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
That works only if the POS_NR is unique, so we need to change that approach if that is not the case.
0
 

Author Comment

by:Genius123
Comment Utility
Sorry, neither one of those worked.  Let me simplify my example.

Two records.

Record 1
ID = 12200838
POS_NR = 1
PROD_BEZ1 = blah blah blah
PROD_BEZ2 = blah blah blah

Record 2
ID = 12200838
POS_NR = 2
PROD_BEZ1 = blah blah blah
PROD_BEZ2 = blah blah blah

I want to return Record 2 and all of the fields because it has the highest number is POS_NR.  ID and POS_NR are the unique identifiers.

Thanks.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
SELECT TOP (1) sbap.POS_NR, sbap.PROD_BEZ1, sbap.PROD_BEZ2
FROM SYSADM_BW_AUFTR_POS sbap
WHERE (((sbap.ID)=12200838))
ORDER BY sbap.POS_NR DESC;
0
 

Author Comment

by:Genius123
Comment Utility
What's sbap?  Never seen that before.  Also it gives me an error.  says .... misspelling, missing, or punctuation incorrect.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Give this a whirl, although I'm thinking it's the same as previous answers..

SELECT PROD_BEZ1, PROD_BEZ2, pos_max.MaxOfPOSNbr
FROM SYSADM_BW_AUFTR_POS pos
   JOIN (Max(POS_NR) as MaxOfPOSNbr FROM SYSADM_BW_AUFTR_POS WHERE ID = 12200838) pos_max ON pos.POS_NR = pos_max.MaxOfPOSNbr

Open in new window


Also, give us a sample data set of what we're querying here, as I'm guessing there are other things going on not in the question that may be impacting the results.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
I'd say mine did work. See this SQL Fiddle example with your simplified query.

If this doesn't work for you, please confirm your SQL environment - is it MS SQL Server? Which version?

You wrote:
>ID and POS_NR are the unique identifiers.

But, you've used the same ID value for both records in your simplified example??

Edit: I included Scott's method in the SQL Fiddle example as well for good measure because I liked it.

As Jim suggests, it would help if you posted some samples of the actual datasets if these aren't working for you.
0
 

Author Comment

by:Genius123
Comment Utility
Both still didn't work.  I'm running SQL from Access.  Maybe that's the problem?

Maybe saying unique identifier wasn't the right terminology.   Basically find the record where ID = 12200838 it's max POS_NR, then return the contents of that particular record.
0
 
LVL 18

Assisted Solution

by:SimonAdept
SimonAdept earned 250 total points
Comment Utility
Are you querying a linked table or using a passthru query?

The very simple syntax I posted above and here should work in Access as well:
select *  From t1
where pos_nr = (
                               select max(POS_NR)
                               FROM t1
                               WHERE ID=12200838
                              ) 

Open in new window


Switch to the SQL view in the query builder and paste it in.

When you say they didn't work, can you be more specific? What results are you getting, or what error message?
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
Comment Utility
>> What's sbap?  Never seen that before. <<

It's a table alias.  Better than having to type "SYSADM_BW_AUFTR_POS" over and over -- I tend to use the first letter of the table name segments as the alias, so s(YSADM_)b(W_)a(UFTR_)p(OS) = sbap :-).

If you are still on SQL 2000, try:

SELECT TOP 1 sbap.POS_NR, sbap.PROD_BEZ1, sbap.PROD_BEZ2
FROM SYSADM_BW_AUFTR_POS sbap
WHERE (((sbap.ID)=12200838))
ORDER BY sbap.POS_NR DESC

This is SQL Server and not DB2 or Oracle, right?
0
 

Author Comment

by:Genius123
Comment Utility
Thanks for sticking with me on this.  Ok, but of the last two queries offered up do not produce errors, but when i run the query the hour glass pops up for a microsecond and nothing happens.  i'm running Access 2003 and it's linked to a SQL 2008 table.  i've run other queries on it - it's just this one i'm having issues with.
0
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Picking up from Scott's last question: If you're generating SQL dynamically for a passthru query from Access, you need to use the particular SQL syntax of the server, whether that is Microsoft SQL Server, IBM DB2, Oracle, MySQL or whatever.

Edit: Just seen your latest post. If you're using linked tables, then it is Access SQL syntax you need, and most if not all of what has been posted above should work.

As it is only one linked table you're querying...
Can you open that linked table in datasheet view and copy a few rows of it to a spreadsheet and post that (after anonymising any sensitive data) please.
0
 

Author Comment

by:Genius123
Comment Utility
Ok, I ran the query out of SQL Server Management Studio and it worked.  I think this should get me what I need. I need to run it from another software program to see if it works from there.  Standby.
0
 

Author Closing Comment

by:Genius123
Comment Utility
Thank you both.  It works now.  That was a bit frustrating, but we got through it!
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
I know it's redundant in a sense, but I would include the ID condition in the other SQL just because it might help SQL with the query plan, depending on existing indexes:

select *  From t1
where pos_nr = (
                               select max(POS_NR)
                               FROM t1
                               WHERE ID=12200838
                              )
and ID=12200838

It certainly won't hurt anything.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now