473,811 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1488
On 21 Aug 2006 23:26:25 -0700, re****@gmail.co m 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.co m 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.co m 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
3078
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
2716
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 relate addresses (multiple addresses); companies, contacts, and tasks to each project (one to many). My challenge lies with the task sub-form which links to the Project form through ProjID. The task record links back to the respective master...
2
1299
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, ',', GROUP_CONCAT(e.ethnicity_name ORDER BY upper(e.ethnicity_name))), GROUP_CONCAT(e.ethnicity_name ORDER BY upper(e.ethnicity_name)) ) AS ethnicity_name
16
3167
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 parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
3
4361
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, the condition for finding DeptID's in the Oracle table have to be filtered by a list of DeptID's found in "tblDeptID" which is a simple local Access table containing one column
2
1405
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 one, two or three services (tblFood, tblMoney, and tblMentoring). Each of these tables contains a field, fkClientID. Within each of the three service tables there is a date field indicating each occasion a client used the service. A client may...
5
1235
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 the total number of pitches thrown by each pitcher on a user-specified time frame (<) and, if possible, count the occurences of records for that pitcher as well (would be the # of starts this season). Any help is appreciated!
2
5632
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 "Week number" as the column header in a crosstab and then in the criteria for that field I put: DateDiff("ww",#12/22/2007#,Now(),0,#12/22/2007#)-2 DateDiff("ww",#12/22/2007#,Now(),0,#12/22/2007#)-1 DateDiff("ww",#12/22/2007#,Now(),0,#12/22/2007#)...
1
1118
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 have to load the ‘names of the theater’ on the 1st DropDownListbox & the names of the movie on the second DropDownListbox. That’s why I have created 2 tables named ‘theater’ and ‘movie’ on sql server 2000. The ‘theater’ table consists of 2 fields ...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10403
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.