Forum Replies Created

Viewing 15 posts - 166 through 180 (of 1,413 total)

  • Reply To: Retrieving First Word, First + Second Word, First + Second + Third Word, First

    An alternative

    select v1.CompanyName,
    left(v1.CompanyName, sum(iif(ss.ordinal<=1, v.str_len, 0))) _1_word,
    left(v1.CompanyName, sum(iif(ss.ordinal<=2, v.str_len, 0))+1) _2_word,
    ...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: JSON data with Pivoted

    This mostly borrows your FROM clause except I changed DaysToAdd to be 'int' which the JSON supports (it's not in quotations).  Instead of PIVOT I always use conditional aggregation.  It's...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: JSON data with Pivoted

    100% agree with Phil.  Fix the JSON please 🙂  To save myself from toil I used ChatGPT-4 and here's the result

    You're right, embedding SQL code in JSON is not a...

    • This reply was modified 2 years, 6 months ago by Steve Collins. Reason: Fixed JSON to work as a declared variable in SQL Server

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Lead Updates Not Working, Only Rendering Nulls

    If you want the odd numbered 'rotation' column value(d) rows to be updated from the even numbered rows then LAG would seem the better choice.  First tho, I agree with...

    • This reply was modified 2 years, 6 months ago by Steve Collins. Reason: Even/odd were reversed in the first sentence

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Slicing status based on conditions

    Maybe something like this.  The OUTER APPLY looks for the next occurrence of [Status] in('collected', 'shipper') for each dropoff and preparation.  Also, LEAD(DateShipped) looks for the next occurrence of [Status]...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: T-SQL : Slicing status by related time periods and other status

    Maybe get rid of the MAX OVER and just evaluate the Status column

    select *, iif([Status] in('Dropoff', 'preparation'), 1, 0) d_or_p
    from #ShipperStatusHistory
    order by OrderNumber, DateShipper;

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Singular or Plural

    Plural.  Each row is a singular instance

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: T-SQL : Slicing status by related time periods and other status

    Happy Thanksgiving

    Based on the written requirements (as interpreted) this query might be helpful.  The comparisons being made across rows are partitioned by (OrderNumber, RelayPoint) and ordered by DateShipper.  To identify...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Sample between two dates for two date ranges ???

    The date columns are of data type DATE? It's not confirmed by question. For this answer the dates must be stored as DATE.

    ;with example_cte as (
    ...

    • This reply was modified 2 years, 6 months ago by Steve Collins. Reason: Removed two unnecessary pairs of parentheses

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: How to find second position after decimal

    Thank you Jeff.  44.9% or 31%?  Either way there's no implicit type conversion 🙂

    Ken McKelvey's solution in the "Pivot with grouping intervals" topic is interesting.  It cross join's a 2...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Pivot with grouping internvals

    The top two CTEs could be replaced with only one.  Possibly the bottom two could be combined as well using the "smudge" method?

    with 
    pvt_cte as (
    ...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Pivot with grouping internvals

    Afaik this returns the same output.  The CTEs expand the date intervals, pivot by role name and summarize by day, identify gaps in GROUP BY columns, and create groups using...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: Insert records based on value

    From your query results you could CROSS APPLY a row generator created by using the row cardinality from sys.all_columns (which in my test instance is 11,745 rows) and TOP.  The...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: How to find second position after decimal

    You could test if the modulo .1 of HRS equals zero

    ;WITH cte_data AS (
    SELECT CAST(50.10 AS numeric(5, 2)) AS HRS
    ...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Reply To: convert to date from datetime

    Also there's EOMONTH which has an optional second parameter 'offset'.  Regardless of which date/datetime datatype is passed to the EOMONTH function it returns DATE.  Ha, some SSC members were wondering...

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

Viewing 15 posts - 166 through 180 (of 1,413 total)