Forum Replies Created

Viewing 15 posts - 271 through 285 (of 4,087 total)

  • Reply To: Pivot in multiple inner joins

    In addition, PIVOT can only do one aggregate, but you need two different aggregates in this example:

    1. Number of complaints
    2. Average closing time

    Crosstabs are more flexible in addition to what...

  • Reply To: Calculating totals based on a column flag

    Brandie Tarvin wrote:

    The ABS(CAST(BE.Buck1 AS tinyint) - 1) doesn't work for me. It is zeroing out the flag that tells me whether or not to include the bucket.

    Here's what I've come...

  • Reply To: Filtering data from a subquery

    The whole purpose of having a CROSS APPLY/OUTER APPLY with a TOP(1) is so that you can avoid multiple reads of the table (although the sort may offset that advantage...

    • This reply was modified 5 years, 8 months ago by drew.allen. Reason: Corrected spelling mistake and changed the OUTER APPLY to a CROSS APPLY. The ISNULL() was no longer necessary due to the change to CROSS APPLY
  • Reply To: Filtering data from a subquery

    Without sample data/expected results, it's pretty difficult to come up with a solution.  I suspect that you'll either want to use a CROSS APPLY/OUTER APPLY with a TOP(1) or a...

  • Reply To: Calculating totals based on a column flag

    I think this is simpler.  I used a CROSS JOIN, because it wasn't clear how to link the two tables.

    SELECT *
    FROM #OrigTable AS ot
    CROSS JOIN #BucketExclude AS...
  • Reply To: how to find gaps between range

    I believe that this solves your problem.  It's a variation on interval packing as discussed by Itzik Ben-Gan.

    create table #t ( b decimal(13,4) not null, c decimal(13,4)...
  • Reply To: TSQL Help

    Since you've posted this in the SQL-2019 forum, you can use STRING_AGG() (introduced in SQL-2017).

    Drew

  • Reply To: Partition by and count

    Please provide sample data in the form of a script to either create a temporary table or declare a table variable and an INSERT statement to insert values into said...

  • Reply To: Looking Back at the Year

    For me, 2019 was mostly good, but the last month was absolutely devastating.  My best friend of 40 years and the only person from college that I kept in touch...

  • Reply To: convert FOR XML as text

    The problem is the order of precedence of operators.  The FOR XML is evaluated before the UNION, but your query requires that it be evaluated after.  Use a CTE for...

  • Reply To: Need Correct Query Code Please? Thx. merry Christmas SQL Kids

    You'll learn a lot more if you do your own homework.

    Drew

  • Reply To: Reusable Variable

    You oversimplified to the point where you did not provide enough information to answer the question, so, no, we don't get the idea.  I suspect that you should be using...

  • Reply To: Limit result to just one row based on date

    Use a CROSS APPLY with a TOP(1).

    If you provide consumable data, you'll get tested code.

    Drew

    PS: I highly recommend NEVER using NULLs when working with intervals.  NULL values make the calculations...

  • Reply To: Issue using ROWNUM

     

    You should ALWAYS provide the whole query, because the location where the error is detected is often not the location that needs to be fixed.

    The logical order of processing is...

  • Reply To: Grouping data based on each team

    Jonathan AC Roberts wrote:

    An alternative solution

    ;WITH cte AS
    (
    SELECT s.Teams, s.Category, SUM(s.Sales) Sales
    FROM dbo.Sales s
    GROUP...

Viewing 15 posts - 271 through 285 (of 4,087 total)