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

SQL Query: Count with "having" date in the last five years

Hello, I need to code a query that:
1-counts the number of time two companies have been in a deal together
2-in the five years that preceded the deal

Lead Participant DealDate
AAA BBB 01/01/1990
BBB AAA 01/01/1991
AAA BBB 01/01/1992
AAA BBB 01/01/1993
AAA BBB 01/01/1994
BBB AAA 01/01/1995
AAA BBB 01/01/1996
BBB AAA 01/01/1997

I managed to find the code for the first part:

SELECT DISTINCT Lead, Participant, Count(DealDate) AS Count
FROM test
GROUP BY Lead, Participant
ORDER BY Lead;

It gave me this and it is perfect

Lead Participant Count
AAA BBB 5
BBB AAA 3

However, I'm puzzled by the second part. How do I query this table so
I have a column that counts the deal in the PREVIOUS 5 years?

I would need this result

Lead Participant DealDate Count last 5 years
AAA BBB 01/01/1990 0
BBB AAA 01/01/1991 0
AAA BBB 01/01/1992 1
AAA BBB 01/01/1993 2
AAA BBB 01/01/1994 3
BBB AAA 01/01/1995 1
AAA BBB 01/01/1996 4
BBB AAA 01/01/1997 2

Where the Count last 5 years is the number of deals in the previous 5
years.

Thanks

Nov 13 '05 #1
2 2958
ja
try this:
SELECT DISTINCT Lead, Participant, Count(DealDate) AS Count
FROM test
(assuming 2005 is the present year)
where dealdate > '12/31/2000'
GROUP BY Lead, Participant
ORDER BY Lead;


On 26 May 2005 17:40:24 -0700, pi*******@gmail.com wrote:
Hello, I need to code a query that:
1-counts the number of time two companies have been in a deal together
2-in the five years that preceded the deal

Lead Participant DealDate
AAA BBB 01/01/1990
BBB AAA 01/01/1991
AAA BBB 01/01/1992
AAA BBB 01/01/1993
AAA BBB 01/01/1994
BBB AAA 01/01/1995
AAA BBB 01/01/1996
BBB AAA 01/01/1997

I managed to find the code for the first part:

SELECT DISTINCT Lead, Participant, Count(DealDate) AS Count
FROM test
GROUP BY Lead, Participant
ORDER BY Lead;

It gave me this and it is perfect

Lead Participant Count
AAA BBB 5
BBB AAA 3

However, I'm puzzled by the second part. How do I query this table so
I have a column that counts the deal in the PREVIOUS 5 years?

I would need this result

Lead Participant DealDate Count last 5 years
AAA BBB 01/01/1990 0
BBB AAA 01/01/1991 0
AAA BBB 01/01/1992 1
AAA BBB 01/01/1993 2
AAA BBB 01/01/1994 3
BBB AAA 01/01/1995 1
AAA BBB 01/01/1996 4
BBB AAA 01/01/1997 2

Where the Count last 5 years is the number of deals in the previous 5
years.

Thanks


Nov 13 '05 #2
Hello,

The problem in the code you gave me is that I want the where to be

where "dealdate of each line">"dealdate of this particular line"-5
years.

Look at the table I gave, the "Count last 5 years" does that

Nov 13 '05 #3

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

Similar topics

1
by: George | last post by:
Hi, I am trying to write a query in Oracle which I have not done before, and are having some difficulty getting my result. Please check my query and my results. select max(note.datetime),...
1
by: malcolm | last post by:
Hello, We have a small team building a project that involves some 30 or so c# assembly dlls. It is a client server application with 1 exe as the starting point. The dlls and exe are sharing an...
14
by: MLH | last post by:
I have the following code to list query names in Access 2.0's Immediate Window. Currently, it lists names of all saved queries. Can you suggest a modification that will result in it listing only...
5
by: a | last post by:
Is there a performance hit if I have many "using" statements that are unnecessary? For example: using System.Collections; when nowhere in my code I'm using System.Collections namespace. I'm...
13
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my...
2
by: whitsey | last post by:
Trying to concatenate two fields into one however I seem unable to do so. All I want is to execute the following SELECT MONTH(L.DATE) ||' '|| YEAR(L.DATE) as MTH_OF_YEAR FROM LOG L But I...
3
by: =?Utf-8?B?QXhlbCBEYWhtZW4=?= | last post by:
Hi, we've got a strange problem here: We've created an ASP.NET 2.0 web application using Membership.ValidateUser() to manually authenticate users with our website. The problem is: If the...
5
by: Cirene | last post by:
I seem to remember seeing a solution where a databound dropdownlist (used as a FILTER for a gridview) had the 1st item as "ALL ITEMS". It used a query with a UNION. But I can't find the example....
7
by: TriAdmin | last post by:
I am working with a system that allow me to add custom fields but I can not add OnChange() language to the custom fields. So I want to have a function in the header that recognizes when fieldx is...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.