473,386 Members | 1,752 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,386 software developers and data experts.

SQL query dilemma

Say you have table with that is filled with the amount of dollars for a
project for given dates.

i.e. 3 columns - Project, Amount, Date

A project could have multiple entries in the tabke but this will vary
depending on the Date

How would you query the top 10 projects in regards to most amount of
dollars where each project has to use the latest date in regards to
amount of dollars.

I tried to use Group By but got stumped as I didn't know how to group
Amount using the Max(Date).

So does anyone know how to achieve this?

Aug 22 '06 #1
4 1473
On 21 Aug 2006 23:26:25 -0700, re****@gmail.com wrote:
>Say you have table with that is filled with the amount of dollars for a
project for given dates.

i.e. 3 columns - Project, Amount, Date

A project could have multiple entries in the tabke but this will vary
depending on the Date

How would you query the top 10 projects in regards to most amount of
dollars where each project has to use the latest date in regards to
amount of dollars.

I tried to use Group By but got stumped as I didn't know how to group
Amount using the Max(Date).

So does anyone know how to achieve this?
create view v1 as
select project, max(date) as max_date
from t
group by project

go

create view v2 as
select t.project, sum(t.amount) as sum_amount
from t join v1 on t.project = v1.project and t.date = v1.max_date
group by t.project

go

select top 10 project
from v2
order by sum_amount

(You can probably collapse some of this using compound queries
if you want.)
Aug 22 '06 #2
On 21 Aug 2006 23:26:25 -0700, re****@gmail.com wrote:
>Say you have table with that is filled with the amount of dollars for a
project for given dates.

i.e. 3 columns - Project, Amount, Date

A project could have multiple entries in the tabke but this will vary
depending on the Date

How would you query the top 10 projects in regards to most amount of
dollars where each project has to use the latest date in regards to
amount of dollars.

I tried to use Group By but got stumped as I didn't know how to group
Amount using the Max(Date).

So does anyone know how to achieve this?
Hi ree321,

Try if this works:

SELECT TOP 10 a.Project, SUM(a.Amount) AS TotalAmount
FROM YourTable AS a
WHERE a.Date = (SELECT MAX(b.Date)
FROM YourTable AS b
WHERE b.Project = a.Project)
GROUP BY a.Project
ORDER BY TotalAmount DESC

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP
Aug 22 '06 #3
Thanks that worked great when I changed the SUM(a.Amount) to
Max/Min(a.Amount) as I want the latest amount and not the sum of the
Amounts.

Hugo Kornelis wrote:
On 21 Aug 2006 23:26:25 -0700, re****@gmail.com wrote:

Hi ree321,

Try if this works:

SELECT TOP 10 a.Project, SUM(a.Amount) AS TotalAmount
FROM YourTable AS a
WHERE a.Date = (SELECT MAX(b.Date)
FROM YourTable AS b
WHERE b.Project = a.Project)
GROUP BY a.Project
ORDER BY TotalAmount DESC

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP
Aug 22 '06 #4
Thanks but haven't tried this out as I went for the simpler solution
below

Aug 22 '06 #5

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

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
1
by: Robert Neville | last post by:
The solution to my dilemma seems straight-forward, yet my mind has not been forthcoming with a direct route. My Project form has a tab control with multiple sub-forms; these distinct sub-forms...
2
by: phillip.s.powell | last post by:
SELECT s.id, s.student_first_name, s.student_last_name, IF(s.student_ethnicity_interest_other IS NOT NULL AND s.student_ethnicity_interest_other != '', CONCAT(s.student_ethnicity_interest_other,...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
3
by: RLN | last post by:
Re: Access 2003/Oracle 9i I have an Access app that connects to an Oracle DB via OLEDB/VBA code (no DSN or ODBC) Queries against straight Oracle tables run fine. For this query, however,...
2
by: LLLiddle | last post by:
As an Access learner, I'm tearing my hair out over what is probably a simple problem. I hope someone can help. I have a table for Clients (tbl.Clients with pkClientID). Each client may use...
5
by: JSaks | last post by:
I'm attempting to create a query that will sum data based on a certain field. i.e. Column 1-Date Column 2-Pitches Thrown on That Day Column 3-Pitchers Name I'd like the query to count...
2
by: hwalker | last post by:
Hello all. Long time reader, first time poster :) I'm creating a repot that shows "the last two weeks of data the next 6 weeks of data, week over week". So, I have a calculated field called...
1
by: sweatha | last post by:
Dear Friends This is Sweatha. Right now I am working in the ASP.NET 2005 with VB coding & the back end is SQL SERVER 2000. My dilemma is I have to design a form with 2 DropDownListboxes. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.