Advertisement

04.05.2006 at 09:34PM PDT, ID: 21803584
[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.6

Convert T-SQL to use in UDF (can not use temp tables)

Asked by Data-Man in MS SQL Server

Tags: ,

Hello everyone,

Here is what I have
--PID is INT
--SYSTEM_USER is VARCHAR(20)
--StartDate is DATETIME
--EventCount is INT
--EventOffset is INT

            DELETE FROM ztblRotationGroupedData WHERE UserName = SYSTEM_USER
            DELETE FROM ztblEventCountData WHERE UserName = SYSTEM_USER

            SELECT       
                  tblEmployee.PID,
                  SYSTEM_USER,
                  e.StartDate,
                  ABS(SUM(ee.EventActive)) AS 'EventCount'
            INTO ztblRotationGroupedData
            FROM
                  tblEvents e
                  INNER JOIN tblEventEmployee ee ON e.EventID = ee.EventID
                  INNER JOIN tblEmployeeListAssignment ela ON ee.PID = ela.PID
                  INNER JOIN tblEmployee ON ee.PID = tblEmployee.PID
            
            WHERE
                  ela.EmployeeListDescription = @strEmployeeListDesc
                  AND ela.Inactive = 0
                  AND DATEPART(dw, e.StartDate) = 7 --Saturday
            
            GROUP BY
                  tblEmployee.PID,
                  e.StartDate
            HAVING      
                  SUM(ee.EventActive) <> 0
            

            SELECT a.PID, SYSTEM_USER, Count(a.PID) As EventCount
            INTO ztblEventCountData
            FROM ztblRotationGroupedData a
            GROUP BY PID
            
            
            SELECT erv.PID, erv.EventOffset, IsNull(a.EventCount,0)
            FROM tblEventRotationValues erv
                  LEFT JOIN ztblEventCountData a ON erv.PID = a.PID
            WHERE erv.EventRotationID = 7 --Saturday
      END

Unfortunately I can't use this T-SQL in a UDF.  So I'm wondering if any of you super savy people can possibly make it all happen from a single SELECT statement or tell me how to work around these issues.

Here is what the data looks like from the first Select Statement without the user name

6072      2006-03-04 00:00:00.000      1
6073      2006-03-04 00:00:00.000      1
6074      2006-03-04 00:00:00.000      1
6075      2006-03-04 00:00:00.000      1
6076      2006-03-04 00:00:00.000      1
6077      2006-03-04 00:00:00.000      1
6078      2006-03-04 00:00:00.000      1
6089      2006-03-11 00:00:00.000      1
6090      2006-03-11 00:00:00.000      1
6091      2006-03-11 00:00:00.000      1
6092      2006-03-11 00:00:00.000      1
6093      2006-03-11 00:00:00.000      1
6072      2006-03-18 00:00:00.000      1
6073      2006-03-18 00:00:00.000      1
6074      2006-03-18 00:00:00.000      1
6075      2006-03-18 00:00:00.000      1
6076      2006-03-18 00:00:00.000      1
6077      2006-03-18 00:00:00.000      1
6078      2006-03-18 00:00:00.000      1
6079      2006-03-18 00:00:00.000      1
6080      2006-03-18 00:00:00.000      1
6081      2006-03-18 00:00:00.000      1
6082      2006-03-18 00:00:00.000      1
6083      2006-03-18 00:00:00.000      1
6084      2006-03-18 00:00:00.000      1
6085      2006-03-18 00:00:00.000      1
6086      2006-03-18 00:00:00.000      1
6087      2006-03-18 00:00:00.000      1
6098      2006-03-18 00:00:00.000      1
6099      2006-03-18 00:00:00.000      1
6100      2006-03-18 00:00:00.000      1
6101      2006-03-18 00:00:00.000      1


Here is what the data looks like after the second SELECT

6072      2
6073      2
6074      2
6075      2
6076      2
6077      2
6078      2
6079      1
6080      1
6081      1
6082      1
6083      1
6084      1
6085      1
6086      1
6087      1
6089      1
6090      1
6091      1
6092      1
6093      1
6098      1
6099      1
6100      1
6101      1


And here is the final set of data...this is what I need
6072      0      2
6073      0      2
6074      0      2
6075      0      2
6076      0      2
6077      0      2
6078      0      2
6079      0      1
6080      0      1
6081      0      1
6082      0      1
6083      0      1
6084      0      1
6085      0      1
6086      0      1
6087      0      1
6088      0      0
6089      0      1
6090      0      1
6091      0      1
6092      0      1
6093      0      1
6094      0      0
6095      0      0
6096      0      0
6097      0      0
6098      0      1
6099      0      1
6100      0      1
6101      0      1

Notice that in the last select statement I use a left join to bring back in any missing PID's from the dataset.

Because I think this is a hard one and I need it yesterday, I've assigned the max point value.

Thanks
Mike

Start Free Trial
[+][-]04.05.2006 at 09:41PM PDT, ID: 16388992

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]04.05.2006 at 09:52PM PDT, ID: 16389021

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.

 
[+][-]04.05.2006 at 09:52PM PDT, ID: 16389022

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]04.05.2006 at 09:56PM PDT, ID: 16389038

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.

 
[+][-]04.05.2006 at 10:28PM PDT, ID: 16389124

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

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

 
[+][-]04.05.2006 at 10:36PM PDT, ID: 16389146

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.

 
[+][-]04.06.2006 at 12:44AM PDT, ID: 16389604

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

Zone: MS SQL Server
Tags: udf, sql
Sign Up Now!
Solution Provided By: aneeshattingal
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.06.2006 at 06:07AM PDT, ID: 16391261

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