Forum Replies Created

Viewing 15 posts - 931 through 945 (of 1,419 total)

  • Reply To: Using Pivot in SQL

    Horses for courses and everybody thinks their way is the best.  Why not have a sense of humor about it?  I agree Pivot and Unpivot are kludge compared to conditional...

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

  • Reply To: Using Pivot in SQL

    ScottPletcher wrote:

    Here's a simple CASE, what wouldn't this take embedded IIFs?

    Idk why don't you give it a try and let us know

    ScottPletcher wrote:

    ... since I write SQL now not a...

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

  • Reply To: Using Pivot in SQL

    Ha I think's the other way around.  CASE WHEN is an abomination of over wordiness that never should've been created.  First, back in the day you used to have to...

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

  • Reply To: Using Pivot in SQL

    Imo IIF gets hated on because it was available first in MS Access.  IIF gets interpreted back to "CASE WHEN" so it's really the same (which is a shame because...

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

  • Reply To: Using Pivot in SQL

    It also works without the tally table and just using CROSS APPLY

    select
    po.ProductCategory, po.ProductSubCategory, v.Region, v.OrdersPlaced,
    (v.OrdersPlaced*100.0/SUM(v.OrdersPlaced) over (partition by po.ProductCategory)) PercentPlaced
    from #po po
    ...

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

  • Reply To: Using Pivot in SQL

    sgmunson wrote:

    Why not just UNPIVOT, as shown here in a CTE:

    Both work.  The tally based is probably more efficient depending on scale.  It didn't occur to use a tally table...

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

  • Reply To: Using Pivot in SQL

    Ok ok I changed the decode to 'South Africa' and now it does work.  Misdiagnosed due to misleading code?  Yea, if it scans once and there's no dependency then it...

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

  • Reply To: Using Pivot in SQL

    Wait a sec.  North Africa is in the code twice!

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

  • Reply To: Using Pivot in SQL

    The bottom grouping is the code with tally ordered by 1,2,3.  The top is UNION ALL with same ordering

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

  • Reply To: Using Pivot in SQL

    SSC

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

  • Reply To: Using Pivot in SQL

    When CROSS JOIN with the tally table the resulting ordering may not align with Region.  It's not known which region will be 1 or 2 or ... unless joined back...

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

  • Reply To: Azure SQL - Handling JSON column in the table

    When the code posted above runs it produces the following output:

    CountryStatenamesurnameagegender
    USAlabamaJohnDon45NULL
    CanadaTorontoMarkBagwell35Male

    This is exactly what was requested no?  Is it a requirement the query be dynamic because the...

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

  • Reply To: Azure SQL - Handling JSON column in the table

    drop table if exists dbo.TestAZURE_JSON;
    go
    CREATE TABLE dbo.TestAZURE_JSON (
    RowID int IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
    Country varchar(10),
    [State] varchar(10),
    [JSON_Value] nvarchar(max));
    go

    INSERT dbo.TestAZURE_JSON(Country, [State], [JSON_Value]) VALUES
    ('US', 'Alabama', '{"name":"John","surname":"Don","age":45}'),
    ('Canada', 'Toronto', '{"name":"Mark","surname":"Bagwell","age":35,"gender":"Male"}');

    select
    ...

    • This reply was modified 6 years, 1 months ago by Steve Collins. Reason: made JSON nvarchar(max)

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

  • Reply To: Simple query question

    This uses the daterange function described here:

    https://www.sqlservercentral.com/scripts/a-daterange-table-valued-function

    (the actual daterange code used is closest to this one)

    drop table if exists dbo.TestUserAccount;
    go
    create table dbo.TestUserAccount(
    AccountName varchar(20) primary...

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

  • Reply To: SQL View Question

    ;with
    details_summary_cte(Manage_GUID, details_row_count) as (
    select Manage_GUID,
    count(*) /* ... other aggregate functions */
    ...

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

Viewing 15 posts - 931 through 945 (of 1,419 total)