Question : T-SQL: Get the count of the valid/current 1:N references as return field

Hello,

I have a T-SQL Stored Procedure running on MS SQL Server 2005.


Thats my existing and working Stored Procedure I have to extend with another feature:

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
 ALTER PROCEDURE [dbo].[Get_OUAufsatz]
      @InternalOUID uniqueidentifier     
      ,@asOfDate as datetime = null
    AS
    BEGIN

      SET NOCOUNT ON;
      IF @asOfDate is null
            SET @asOfDate = getdate()
      SELECT [AufsatzId]
                  ,[H_ValidSince]
                  ,[H_ValidUntil]
                  ,[H_UID]
                  ,[InternalOUId]
                  ,[Test1]
                  ,[Test2]                           
             
            FROM [dbo].[OUAufsatz]
            WHERE
                  [InternalOUId] = @InternalOUID
                  AND @asOfDate BETWEEN [dbo].[OUAufsatz].[H_ValidSince]
                  AND [dbo].[OUAufsatz].[H_ValidUntil]

   

The above SP returns a list of OUAufsatz objects/rows that satisfy the where clause.
Thats an easy query...

Now I have to extend the query. The following code is a code snippet which works fine for itself, but I have to integrate its functionality into the above Store Procedure Get_OUAufsatzwerte:


 

 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
  INNER JOIN(
    SELECT Count(*) AS [Count], Assessment.AufsatzId
                                   FROM Assessment
                                   INNER JOIN OUAufsatz
                                   ON Assessment.AufsatzId = OUAufsatz.AufsatzId
                                   AND OUAufsatz.H_ValidUntil = '3000-01-01'
 
                                   WHERE Assessment.H_ValidUntil = '3000-01-01'   
                                   GROUP BY Assessment.AufsatzId
                          ) AS MAP
                  ON MAP.[AufsatzId] = [OUAufsatz].[AufsatzId]




What the heck is that H_ValidSince and H_ValidUntil at all? explanation =>

We historize every change in the database that means if any field in the e.g. OUAufsatz table is changed a insert/update script is executed which checks if a Aufsatz dataset/row with certain H_ValidSince and certain H_ValidUntil values are already existing. If yes the H_ValidSince field gets the datetime.now value and the H_ValidUntil field gets the value '3000-01-01' which means that dataset/row is the current/actual row. If no some other stuff is done... its just a historize method to track everything hehe.

And if you look now above the code snippet and the compares I do you see that I filter to the current/actual Assessment and OUAufsatz.

Now I need to combine my code snippet with the above Store Procedure with stuff like that

PSEUDO Code:


   
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
 SELECT [AufsatzId]
                  ,[H_ValidSince]
                  ,[H_ValidUntil]
                  ,[H_UID]
                  ,[InternalOUId]
                  ,[Test1]
                  ,[Test2]                           
               [b]   , return the [Count] if 0 then false else true As [IsInUse][/b]
            FROM [dbo].[OUAufsatz]
            WHERE
                  [InternalOUId] = @InternalOUID
                  AND @asOfDate BETWEEN [dbo].[OUAufsatz].[H_ValidSince]
                  AND [dbo].[OUAufsatz].[H_ValidUntil]


and the code snippet must combined here with the Existing SP-code.

My output I expect in tabular form is this (visible columns in the user interface):

Test1...:...Test2...:...IsInUse (bool field aka checkbox)

The relation is that 1 OUAufsatz can have many Assessments but I am only interested in the Assessment which is current/VALID means H_ValidUntil = '3000-01-01'.

Any help is appreciated :)

If you need more info just say, I try to help as much as I can!

Answer : T-SQL: Get the count of the valid/current 1:N references as return field

note that it must be LEFT join, otherwise those that are "not in use" will not be returned ...
Random Solutions  
 
programming4us programming4us