473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Need help with a query

Hi all,

I'm trying to
Select Top 1 value1 from table1
where CompID = 10
Order by mydate Desc

What this does is gives me the value of value1 for the max date.
Which works very well.

What I want to achieve is that get the max records for different compids = 7 or 9 or 10 etc. in the table.


Any ideas?

Thanks.
Dec 19 '07 #1
3 749
deepuv04
227 Expert 100+
Hi all,

I'm trying to
Select Top 1 value1 from table1
where CompID = 10
Order by mydate Desc

What this does is gives me the value of value1 for the max date.
Which works very well.

What I want to achieve is that get the max records for different compids = 7 or 9 or 10 etc. in the table.


Any ideas?

Thanks.
Try the following query..... hope will work

SELECT value1 FROM table1 WHERE mydate IN (SELECT MAX(mydate ) FROM table1
GROUP BY CompID )

thanks
Dec 20 '07 #2
Thanks for the reply. I tried your query and it fails for the following example of data:

Table1:

ctid val dt
7 5 1/1/2006
7 8 4/1/2006
7 16 1/1/2007
9 4 2/24/2006
9 12 6/6/2006
9 6 12/12/2006
9 20 5/3/2007
10 3 12/12/2007
10 13 1/1/2007


I want to return:
ctid val dt
7 16 1/1/2007
9 20 5/3/2007
10 3 12/12/2007

Your query will fail and get the value:
10 13 1/1/2007


Please help.

Thanks,
Deepali.



Try the following query..... hope will work

SELECT value1 FROM table1 WHERE mydate IN (SELECT MAX(mydate ) FROM table1
GROUP BY CompID )

thanks
Dec 20 '07 #3
deepuv04
227 Expert 100+
Thanks for the reply. I tried your query and it fails for the following example of data:

Table1:

ctid val dt
7 5 1/1/2006
7 8 4/1/2006
7 16 1/1/2007
9 4 2/24/2006
9 12 6/6/2006
9 6 12/12/2006
9 20 5/3/2007
10 3 12/12/2007
10 13 1/1/2007


I want to return:
ctid val dt
7 16 1/1/2007
9 20 5/3/2007
10 3 12/12/2007

Your query will fail and get the value:
10 13 1/1/2007


Please help.

Thanks,
Deepali.

Ya you are right....

now try the following

SELECT Table1.* FROM Table1 inner join
(SELECT MAX(mydate) dt,ID FROM Table1 GROUP BY ID) as T
on Table1.mydate = T.dt and T.id = Table1.id
Dec 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
3
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype,...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
7
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First...
3
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.