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

Query Help Please.

Mat
The code below returns the number of Service Visits logged to Each
Property.

What I'd like to do is summarise this data by displaying the number of
Properties with 1,2,3,4,n Visits.

I assume I need to nest the first sproc ?

Ideas please ?????

---------------------------------------------------------------------------

Alter Procedure rsp_ServicesToCompletion

@CONTRACT VARCHAR(3)

As

DECLARE @YEARSTART DATETIME

SELECT @YEARSTART = (SELECT DATEADD(YY, CONTRACTYEAR, CONTRACTSTART)
FROM TBL_CONTRACT WHERE (CONTRACT = @CONTRACT))

SELECT COUNT(PROPREF) FROM (SELECT COUNT(TBL_SERVICE.SERVICEDATE) AS
VISITS, TBL_SERVICE.PROPREF
FROM TBL_PROPERTY INNER JOIN TBL_SERVICE ON TBL_PROPERTY.PROPREF =
TBL_SERVICE.PROPREF
WHERE (TBL_PROPERTY.SERVICESTATUS = N'SERVICED') AND
(TBL_PROPERTY.CONTRACT = @CONTRACT) AND
(TBL_SERVICE.SERVICEDATE BETWEEN @YEARSTART AND GETDATE())
GROUP BY TBL_SERVICE.PROPREF) GROUP BY VISITS
Jul 20 '05 #1
1 1419

"Mat" <MA*@OLDHALL.TELE2.CO.UK> wrote in message
news:a2**************************@posting.google.c om...
The code below returns the number of Service Visits logged to Each
Property.

What I'd like to do is summarise this data by displaying the number of
Properties with 1,2,3,4,n Visits.

I assume I need to nest the first sproc ?

Ideas please ?????

-------------------------------------------------------------------------- -
Alter Procedure rsp_ServicesToCompletion

@CONTRACT VARCHAR(3)

As

DECLARE @YEARSTART DATETIME

SELECT @YEARSTART = (SELECT DATEADD(YY, CONTRACTYEAR, CONTRACTSTART)
FROM TBL_CONTRACT WHERE (CONTRACT = @CONTRACT))

SELECT COUNT(PROPREF) FROM (SELECT COUNT(TBL_SERVICE.SERVICEDATE) AS
VISITS, TBL_SERVICE.PROPREF
FROM TBL_PROPERTY INNER JOIN TBL_SERVICE ON TBL_PROPERTY.PROPREF =
TBL_SERVICE.PROPREF
WHERE (TBL_PROPERTY.SERVICESTATUS = N'SERVICED') AND
(TBL_PROPERTY.CONTRACT = @CONTRACT) AND
(TBL_SERVICE.SERVICEDATE BETWEEN @YEARSTART AND GETDATE())
GROUP BY TBL_SERVICE.PROPREF) GROUP BY VISITS


Without more details, this is a guess, but it may be close enough to help
you:

SELECT
COUNT(PROPREF)
FROM (
SELECT
COUNT(TS.SERVICEDATE) AS VISITS,
TS.PROPREF
FROM
TBL_PROPERTY TP
INNER JOIN TBL_SERVICE TS
ON TP.PROPREF = TS.PROPREF
INNER JOIN (
select CONTRACT, DATEADD(YY, CONTRACTYEAR, CONTRACTSTART) as YEARSTART
FROM TBL_CONTRACT ) YS
ON TP.CONTRACT = YS.CONTRACT
WHERE
TP.CONTRACT = @CONTRACT
TP.SERVICESTATUS = N'SERVICED' AND
TS.SERVICEDATE BETWEEN YS.YEARSTART AND GETDATE()
GROUP BY
TS.PROPREF) DT
GROUP BY VISITS
Instead of using your procedure, you can join on a view or derived table (YS
in my example), which has the YEARSTART for each contract. If this isn't
helpful, please consider posting CREATE TABLE scripts, sample data, and your
expected results.

Simon
Jul 20 '05 #2

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: 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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
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...

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.