Question

I have a previously created SQL view that contains the data I am looking to get. It has Case when's and it is making it confusing for me to run MAX(date)... I have tried to run a view based on it and it just keeps timing out... I can create a new view with the data I need but I still need the data manipulated and must leave in some of these Case when's...

This view currently produces this info

THDATE              ComponentItemNumber Issued Quantity
12/23/2013 12:00:00 AM  TTMS-12.7-9 19
12/23/2013 12:00:00 AM  TTMS-4.8-9  8.34
12/23/2013 12:00:00 AM  TTMS-4.8-9  10.66
12/23/2013 12:00:00 AM  44476-3112  2
12/23/2013 12:00:00 AM  M80-0130005 NULL

What I want to do is simply Get the Max date for each item# here is the current view... Can someone help me with creating a SQL Statement that does this? Please... I am using SQL 2000

SELECT  THDATE
      , CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4,
                                         7) = 'C' THEN NULL
             ELSE dbo.udf_GetHistoryVarchar(THBODY + THBODY2 + THBODY3
                                            + THBODY4, 10)
        END AS ComponentItemNumber
      , CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4,
                                         3) = 'C' THEN NULL
             WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4,
                                         2) = 'D' THEN NULL
             ELSE dbo.udf_GetHistoryFloat(THBODY + THBODY2 + THBODY3 + THBODY4,
                                          17)
        END AS IssuedQuantity
FROM    dbo.Mfg_TRANHIS AS T1
WHERE   ( THTRID = 'PICK' )
        AND ( THFLAG = '1' )
        AND ( dbo.udf_GetHistoryChar(THBODY + THBODY2 + THBODY3 + THBODY4, 1) = 'C' )
        AND ( NOT ( ( CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
                                                       + THBODY3 + THBODY4, 7) = 'C'
                           THEN NULL
                           ELSE dbo.udf_GetHistoryVarchar(THBODY + THBODY2
                                                          + THBODY3 + THBODY4,
                                                          10)
                      END ) LIKE 'CU%' )
            )
        AND ( NOT ( ( CASE WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
                                                       + THBODY3 + THBODY4, 2) LIKE '[MP]'
                           THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
                                                          + THBODY3 + THBODY4,
                                                          4)
                           WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
                                                       + THBODY3 + THBODY4, 3) = 'C'
                           THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
                                                          + THBODY3 + THBODY4,
                                                          5)
                           WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
                                                       + THBODY3 + THBODY4, 2) = 'D'
                           THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
                                                          + THBODY3 + THBODY4,
                                                          5)
                           WHEN dbo.udf_GetHistoryChar(THBODY + THBODY2
                                                       + THBODY3 + THBODY4, 7) = 'C'
                           THEN dbo.udf_GetHistoryVarchar(THBODY + THBODY2
                                                          + THBODY3 + THBODY4,
                                                          4)
                           ELSE dbo.udf_GetHistoryVarchar(THBODY + THBODY2
                                                          + THBODY3 + THBODY4,
                                                          4)
                      END ) LIKE 'VM%' )
            )
Was it helpful?

Solution

If I understand correctly you are asking how to get past the Timeout issue.

I have experienced this in the past and so far the best solution I have come up with is to push the data into a table based on the starting view. Then create the second view based on that table. However this required that I have a set time (in some cases every 30 minutes) that a stored procedure would do this for me and that may not work for you.

Some other things that I found worked were to limit the amount of data I was working with before doing the formatting. Also making sure you have proper keys/indexes on the table(s) you are working with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top