473,399 Members | 2,478 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,399 software developers and data experts.

Query Help Please

I have the following query:

SELECT TOP 100 PERCENT Company_Code, Well_Code, Effective_Date,
Interest
FROM dbo.HUD_Expense_Interest_Group1
I need to have the query return one row for each distinct Company_Code,
Well_Code combination along with returning the values in Interest and
Effective_Date. There will be multiple Interest and Effective_Date
values for each distinct Well_Code, Company_Code combination; I only
want the values (Interest & Date) from the records represented by the
newest date(Max) in Effective_date.

I have tried both Distinct and Group By and can get it close but not
quite where I need it.
Thanks

Jeff

Jul 23 '05 #1
2 1056
[posted and mailed, please reply in news]

JeffM (je****@huddlestonco.com) writes:
I have the following query:

SELECT TOP 100 PERCENT Company_Code, Well_Code, Effective_Date,
Interest
FROM dbo.HUD_Expense_Interest_Group1
I need to have the query return one row for each distinct Company_Code,
Well_Code combination along with returning the values in Interest and
Effective_Date. There will be multiple Interest and Effective_Date
values for each distinct Well_Code, Company_Code combination; I only
want the values (Interest & Date) from the records represented by the
newest date(Max) in Effective_date.


If I understand this correctly, this query should cut it:

SELECT a.Company_Code, a.Well_code, a.Effective_Date, a.Interest
FROM dbo.HUD_Expense_Interest_Group1 a
JOIN (SELECT Company_Code, Well_code,
Effective_Date = MAX(Effective_Date)
FROM dbo.HUD_Expense_Interest_Group1
GROUP BY Company_Code, Well_code) b
ON a.Company_Code = b.Company_Code
AND a.Well_code = b.Well_Code
AND a.Effective_Date = b.Effective_Date
What you have here is a derived table, which is a kind of a temp table
within the query, except that it is not necessarily materialised. As
long as the result is not affected, SQL Server may rearrange the
computation order.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2

That works great!

Thank you very much.
*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
4
by: sah | last post by:
I need some help with the following query: DECLARE @SRV VARCHAR(20), @date smalldatetime SET @SRV = (select @@servername) SET @date = '20040901' select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
4
by: Max Harvey | last post by:
Hi, I have looked at the example called "Open Parameter queries from code" from the site http://www.mvps.org/access/queries/qry0003.htm I made up a test which I though looked pretty close...
7
by: Nicolae Fieraru | last post by:
I have two tables, they contain: Table1: ID1, Name1, Address1, Purchase1 Table2: ID2, Name2, Address2, Purchase2 I need a query which creates Table3 with content from Table1 and Table2. The...
6
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
5
by: Ryan Hubbard | last post by:
Is it possible to get the recordset from an open query window? So you run the query. The window is open. Can vba retrieve this data?
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.