Advertisement

03.12.2008 at 07:55PM PDT, ID: 23237422
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

TSQL UPDATE field with Count in Subquery

Asked by mjenck-yvcc in SQL Server 2005, MS SQL Server, SQL Query Syntax

Tags:

My goal is to look up all previous test a student has taken and see if they are complying with testing policy.  I take the current test and find the date range that it falls in (that is which quarter/year the student took the test) and store this information in UploadVerifyDateRange.  
I then find all other test taken by the student and store them in UploadVerifyAllTestDates.  I then delete the first test and the current test (as it should not be counted as a retest) and count the number of test taken in the date range (C1 below)  I am attempting to update the udr_NumberOfTries from the subquery.  
In Try 1 - it changed all records to 1 (only one recorded matched) so I tried to filter to the correct record by moving the subquery to the FROM clause it won't parse now.  The error is
Msg 208, Level 16, State 1, Line 1
Invalid object name 'T1'.

Table structure is
CREATE TABLE [Placement].[UploadVerifyDateRange](
      [udr_ID] [int] NOT NULL,
      [udr_Department] [varchar](11) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [udr_SID_Verified] [varchar](9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [udr_StartDate] [datetime] NOT NULL,
      [udr_Enddate] [datetime] NOT NULL,
      [udr_NumberOfRetries] [int] NOT NULL,
      [udr_NumberOfTries] [int] NOT NULL
) ON [PRIMARY]

CREATE TABLE [Placement].[UploadVerifyAllTestDates](
      [uall_RecordIndex] [int] IDENTITY(1,1) NOT NULL,
      [uall_ID] [int] NOT NULL,
      [uall_Department] [varchar](11) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [uall_SID] [varchar](9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [uall_TestDate] [datetime] NOT NULL
) ON [PRIMARY]
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
Try 1
    UPDATE Placement.UploadVerifyDateRange
    SET udr_NumberOfTries =
        (SELECT C1
        FROM Placement.UploadVerifyDateRange INNER JOIN
            (SELECT uall_Department, uall_SID, COUNT(uall_SID) AS C1
             FROM Placement.UploadVerifyAllTestDates INNER JOIN Placement.UploadVerifyDateRange 
             ON ((uall_Department = udr_Department) AND
                 (uall_SID = udr_SID_Verified) AND
                 (udr_StartDate <= uall_TestDate) AND
                 (udr_Enddate > uall_TestDate))
             GROUP BY uall_Department, uall_SID) T1
        ON ((udr_Department = T1.uall_Department) AND 
            (udr_SID_Verified = T1.uall_SID)))
 
Try 2
 
UPDATE Placement.UploadVerifyDateRange
    SET udr_NumberOfTries =
        (SELECT C1
        FROM Placement.UploadVerifyDateRange INNER JOIN T1           
        ON ((udr_Department = T1.uall_Department) AND 
            (udr_SID_Verified = T1.uall_SID)))
    FROM (SELECT uall_Department, uall_SID, COUNT(uall_SID) AS C1
             FROM Placement.UploadVerifyAllTestDates INNER JOIN Placement.UploadVerifyDateRange 
             ON ((uall_Department = udr_Department) AND
                 (uall_SID = udr_SID_Verified) AND
                 (udr_StartDate <= uall_TestDate) AND
                 (udr_Enddate > uall_TestDate))
             GROUP BY uall_Department, uall_SID) T1
    WHERE ((T1.uall_Department = udr_Department) AND
           (T1.uall_SID = udr_SID_Verified))
 
Loading Advertisement...
 
[+][-]03.12.2008 at 08:52PM PDT, ID: 21113210

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, MS SQL Server, SQL Query Syntax
Tags: SQL (MS SQL Server 2005)
Sign Up Now!
Solution Provided By: dtodd
Participating Experts: 2
Solution Grade: B
 
 
[+][-]03.12.2008 at 09:18PM PDT, ID: 21113304

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.12.2008 at 09:27PM PDT, ID: 21113340

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628