Results 1 to 3 of 3

Thread: [RESOLVED] [SQL Server] Using CTE to reuse aliases

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Resolved [RESOLVED] [SQL Server] Using CTE to reuse aliases

    SQL Server 9.0.3068

    I'm doing some calculations and aggregations on data in my query and I'd like to reuse the results in other calculations. Ex.: ReservesWithheld and DistributionPercentage below.

    Instead of redoing the calculations when I want to use DistributionPercentage, I've done them in a CTE and then "reused" them in the query after the CTE. Is it acceptable to proceed like this with a CTE so I can again reference the aliases or is there a better way to do this? How is this approach performance wise?

    sql Code:
    1. use SalesData;
    2. go
    3. with SMSales as
    4. (
    5.     select
    6.         s.JournalRef Period,
    7.         s.ItemNumber,
    8.         s.ItemDescription,
    9.         s.CustomerNumber,
    10.         s.CustomerName,
    11.         sum(s.GrossQuantity) GrossUnits,
    12.         sum(s.ReturnQuantity) ReturnUnits,
    13.         sum(s.GrossAmount) GrossSales,
    14.         sum(s.GrossAmount) * .3 ReservesWithheld,
    15.         sum(s.ReturnAmount) ReturnSales,
    16.         case
    17.             when s.CustomerNumber = '0007520' or s.CustomerNumber = '6005019' or sum(s.GrossQuantity) = 0 then .1
    18.             when sum(s.GrossAmount) / sum(s.GrossQuantity) > 5 then 0.15
    19.             else .1
    20.         end as DistributionPercentage
    21.     from dbo.SalesByItem s
    22.     where
    23.         s.JournalRef = 'GPE_200903LM'
    24.     group by
    25.         s.JournalRef,
    26.         s.ItemNumber,
    27.         s.ItemDescription,
    28.         s.CustomerNumber,
    29.         s.CustomerName
    30. )
    31. select
    32.     sm.Period,
    33.     sm.ItemNumber,
    34.     sm.ItemDescription,
    35.     sm.CustomerNumber,
    36.     sm.CustomerName,
    37.     sm.GrossUnits,
    38.     sm.ReturnUnits,
    39.     sm.GrossUnits + sm.ReturnUnits NetUnits,
    40.     sm.GrossSales,
    41.     sm.ReservesWithheld,
    42.     sm.ReturnSales,
    43.     sm.GrossSales - sm.ReservesWithheld + sm.ReturnSales NetSales,
    44.     case
    45.         when sm.GrossUnits = 0 then 0
    46.         else sm.GrossSales / sm.GrossUnits
    47.     end as AvgWholesalePrice,
    48.     sm.DistributionPercentage,
    49.     (sm.GrossSales - sm.ReservesWithheld + sm.ReturnSales) * sm.DistributionPercentage DistributionFee
    50. from SMSales sm
    51. go

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [SQL Server] Using CTE to reuse aliases

    That is how I have used a CTE if needed in more then one place in the proc
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [SQL Server] Using CTE to reuse aliases

    Quote Originally Posted by GaryMazzone View Post
    That is how I have used a CTE if needed in more then one place in the proc
    Cool. I just wanted to make sure that this was a valid approach without any big performance problems if I need to reuse some calculations more than once.

    Thanks Gary!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width