Forum Replies Created

Viewing 15 posts - 316 through 330 (of 4,085 total)

  • Reply To: How to apply rank when fetching data from table using sql query?

    This gives the same results but doesn't require creating a second temp table.

    WITH status_ranges AS
    (
    SELECT
    s.Starttime
    ,s.Endtime
    ,s.Status
    ,CASE WHEN s.Status = LAG(s.Status) OVER(ORDER BY s.Starttime) THEN 0 ELSE 1 END...

    • This reply was modified 6 years, 6 months ago by drew.allen. Reason: Forgot to include the ROWS UNBOUNDED PRECEDING
    • This reply was modified 6 years, 6 months ago by drew.allen.

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Select last non null value in field

    Both Jeff and Sumathi's solutions require two scans of the table.  The following only requires one scan.

    SELECT 
    tfu.rptMth
    ,SUBSTRING(MAX(tfu.rptMth + NULLIF(tfu.Buiding, '')) OVER(ORDER BY tfu.rptMth ROWS UNBOUNDED PRECEDING),...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: SELECT processed before WHERE clause

    The following is from ITPro Logical Query Processing: What It Is And What It Means to You

    Figure 3: Logical query processing order of query clauses

    1 FROM...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: SELECT processed before WHERE clause

    Alan Burstein wrote:

    The last three things to be processed are : SELECT then TOP / OFFSET then ORDER BY

    That's not what the SQL documentation says and that doesn't make sense either. ...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Using CASE Statement to SET variable when null value

    SGT_squeequal wrote:

    Just because your previous employer forbid you to use isnull does not mean it cant be used.

    I never said it "can't" be used.  I said it SHOULDN'T be used.

    I...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Find records from the last 24 hours

    As with all things SQL, it depends.  I did a quick test using an index on VisitID and ActivityDateTime and Jonathan's original query won hands down based on the query...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Find records from the last 24 hours

    Depending on your indexes, this may perform better.

    WITH Last24Hrs AS (
    SELECT
    VisitID
    ,IdentifierID
    ,ActivityDateTime
    ,DATEADD(HOUR,-24,LAST_VALUE(ActivityDateTime) OVER (PARTITION BY VisitID ORDER BY ActivityDateTime ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)) AS _24HrsBefore
    FROM...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Using CASE Statement to SET variable when null value

    SGT_squeequal wrote:

    you can also use isnull function

    Update APFinal

    Set Comments = isnull(@Comments,  'No Comments Provided')

    Where DocID = @DocID and ID = @ID

    I recommend NOT using the ISNULL() function, which is why...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Using CASE Statement to SET variable when null value

    You want help troubleshooting an error message, but you didn't think that the text of the error message was relevant?

    Why are you using a CASE expression rather than a COALESCE(). ...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: Get latest address column with Flag = Y in all rows

    Jeffrey Williams wrote:

    Declare @tempAddress Table (
    Id int
    , Name1 varchar(10)
    ...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: last day of the month minus 1 day

    ZZartin wrote:

    Also, as noted before, -1 has caused errors because of out-of-order date calcs.  To me, it's just not worth the potential issues.  Not to say you can't use it,...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: t-sql 2012 cross join

    You've been around long enough to know to use the Insert/edit code sample when entering code to preserve the formatting.

    You're missing part of your code.  You're using an alias that...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: last day of the month minus 1 day

    ZZartin wrote:

    drew.allen wrote:

    I think it's a circular argument.  You don't use it, because it's not familiar, but it's not familiar, because you don't use it.  I've been using the -1...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: last day of the month minus 1 day

    I think it's a circular argument.  You don't use it, because it's not familiar, but it's not familiar, because you don't use it.  I've been using the -1 variant for...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Reply To: last day of the month minus 1 day

    ScottPletcher wrote:

    The standard pattern for getting the first day of the month is:

    SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)

    Stick to that pattern, don't corrupt it with -1 tricks!  That just makes it...

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 15 posts - 316 through 330 (of 4,085 total)