7

I need to formulate a t-sql query and so far I have been unable to do so. The table that I need to query is called Operations with two columns ,an FK OperationTypeID and an OperationDate. The query needs to return a result which comprises of the count of operation type id during the range specified.

Through the application interface the user can specify multiple operationtype Ids as well as their individual date ranges so for instance, the operationtype id 'A' can be looked for in the range 22/04/2010 to 22/04/2012 and operationtype Id 'B' can be searched in 15/10/2012 to 15/11/2013 and so on for other operation type ids. Now I need to return a count for each operationtype id during each of the range specified for individual operation type Ids.

What is the most efficient way to achieve this in a single t-sql query considering the performance issues ... a rough layout presented below, i am not very good at formatting so i hope it will still give an idea.

+---------------+----------+----------+-----+
|OperationTypeID|Min date  |Max Date  |Count|
+---------------+----------+----------+-----+
|A              |22/04/2010|22/04/2012|899  |
+---------------+----------+----------+-----+
|B              |15/10/2012|15/11/2013|789  |
+---------------+----------+----------+-----+

.... and so on

Would appreciate if anyone can help. The query needs to return a count for each operationtype id based on the min/max date range specified by the user. The Min/Max functions available in sql server probably don't apply here. One possible approach that I have thought of so far makes use of the Union All approach, where I formulate a single query for a single operation type id based on the date range and then do a UNION All, any performance impacts?

5
  • 1
    I don't understand the question :-( Commented Jan 13, 2014 at 6:24
  • Where date ranges for each OperationTypeID come from? Commented Jan 13, 2014 at 7:03
  • @John could you post a sample output you're trying to achieve? as SamB stated, the question is a bit unclear. Commented Jan 13, 2014 at 7:09
  • guys the date range is something that the user specifies through the application. Think of it as a simple report with an interface interactive enough to allow the user to specify multiple operationtype along with their specific date ranges. The operation type accompanied with the date ranges then forms the parameter for my query. Commented Jan 13, 2014 at 8:44
  • @Mureinik The sample is the table i have drawn above. Its the result set, operationtype id is the first column, the next two date columns are just the criteria returned specified by the user. The last column is the count of the operationtype during the date range. Commented Jan 13, 2014 at 9:00

1 Answer 1

6

You will need to store the search criteria somewhere. The best place, would probably be a temporary table with the following columns:

CREATE TABLE #SearchCriteria (
    OperationTypeId VARCHAR(1)
    MinDate DATETIME
    MaxDate DATETIME
)

Now, once you have populated this table, a simple query like this, should give you what you want:

SELECT OperationTypeId, 
    MinDate, 
    MaxDate,
    (SELECT COUNT(*) FROM Operations 
      WHERE OperationDate BETWEEN SC.MinDate AND SC.MaxDate 
        AND OperationTypeId = SC.OperationTypeId) AS [Count]
FROM
    #SearchCriteria SC

If you must have everything in a single query (without using a temporary table), do something like this:

SELECT OperationTypeId, 
    MinDate, 
    MaxDate,
    (SELECT COUNT(*) FROM Operations 
      WHERE OperationDate BETWEEN SC.MinDate AND SC.MaxDate 
        AND OperationTypeId = SC.OperationTypeId) AS [Count]
FROM
    (VALUES ('A', '22/04/2010', '22/04/2012')
           ,('B', '15/10/2012', '15/11/2013')
         /* ... etc ... */
    ) SC(OperationTypeId, MinDate, MaxDate)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Dan, I am going with the second approach. Didn't knew you can create a table like that at querytime.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.