Sloan's profileGranada Coder, C#, Sql S...PhotosBlogLists Tools Help
    8/13/2009

    CTE Running Total Example

     

    I happened across a CURSOR based "Running Total" example on the web today.
    Everyone with whom I work knows I detest Cursors in TSQL.  Baring teeth

    So I coded up (one) alternate solution.

    Below is (one variation) of a 'Running Total' solution using a CTE.
    http://technet.microsoft.com/en-us/library/ms175972.aspx  (CTE Link)

    -----------START TSQL

    Use Northwind

    GO

     

    declare

    @CustomerID varchar(6)

    declare

    @BeginDate datetime

    declare

    @EndDate datetime

    select

    @CustomerID = (select top 1 CustomerID from dbo.Orders )

    select

    @BeginDate = '01/01/1900'

    select

    @EndDate = '12/31/2010'

     

    ;

    WITH

    MyCTE -- http://technet.microsoft.com/en-us/library/ms175972.aspx

    ( ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,[Address],

    City

    ,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,

    ProductID

    ,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight,ROWID) AS

    (

    SELECT

    ShipName

    ,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,[Address],

    City

    ,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,

    ProductID

    ,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight

    , ROW_NUMBER() OVER ( ORDER BY OrderDate , ProductName ASC ) as ROWID

    FROM

    dbo

    .Invoices inv -- "Invoices" is a VIEW, FYI

    where

    inv

    .CustomerID = @CustomerID

    and (inv.OrderDate between @BeginDate and @EndDate)

    )

    SELECT

    /*

    ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,[Address],

    City,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,

    ProductID,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight,

    */

    --trim the list down a little for the final output

    CustomerID

    ,Salesperson,OrderID,OrderDate,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight,

     

    (ExtendedPrice + Freight) as ComputedTotal

    --The below line is the "trick". I reference the above CTE, but only get data that is less than or equal to the row that I am on (outerAlias.ROWID)

    , (Select SUM (ExtendedPrice + Freight) from MyCTE innerAlias where innerAlias.ROWID <= outerAlias.ROWID ) as RunningTotal

    , ROWID as ROWID_SHOWN_FOR_KICKS , OrderDate as OrderDateASecondTimeForConvenience

    FROM

    MyCTE outerAlias

    --Two Order By Options

    --ORDER BY outerAlias.OrderDate , ProductName -- << Whatever the ORDER BY is here, should match the "ROW_NUMBER() OVER ( ORDER BY ________ ASC )" statement inside the CTE

    ORDER

    BY outerAlias.ROWID -- << Or, to keep is more "trim", ORDER BY the ROWID, which will of course be the same as the "ROW_NUMBER() OVER ( ORDER BY" inside the CTE

     

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    好站推薦~
    http://longsong.com.tw , http://fzlog.com , http://27867446.com.tw , http://lgid.com.tw , http://gnlog.com , http://meimei888.com , http://jylog.com , http://goldenwish.com.tw , http://kmtai53.com.tw , http://ywlog.com , http://ufc.com.tw , http://fqlog.com , http://borg-build.com.tw , http://hukang.com.tw , http://harplo.com.tw , http://ericart.com.tw , http://mms4u.com.tw , http://kjm.com.tw , http://kllog.com , http://e-new.com.tw , http://fplog.com , http://yehching.com.tw , http://46-1k.com.tw , http://hummer.idv.tw , http://cygift.com.tw , http://xweb.com.tw , http://hllog.com , http://friendstalk.com.tw , http://lhlog.com , http://znet.com.tw , http://richforever.com.tw , http://revive.com.tw , http://fashion-tokyo.idv.tw , http://88u.com.tw , http://cw123.com.tw , http://hitrontech.com.tw , http://ttj.com.tw , http://cpbio.com.tw , http://yhlog.com , http://101clinic.com.tw , http://calf.com.tw , http://artofliving.com.tw , http://huban.com.tw , http://dfarm.idv.tw , http://cmr.com.tw , http://lovecookie.com.tw , http://overurl.com , http://shome.idv.tw , http://eleer.com.tw , http://wiwe.com.tw , http://98guide.com.tw , http://elpaso.com.tw , http://xhlog.com , http://siraya.idv.tw , http://renata.idv.tw , http://tzlog.com , http://tplog.com , http://wfg.com.tw , http://jllog.com , http://mobitek.com.tw , http://kt-sl.com.tw , http://rjlog.com , http://pastaonion.com.tw , http://gmwear.com.tw , http://phtourass.com.tw , http://photoshock.com.tw , http://yes-queen.com , http://0492801699.com.tw , http://xqlog.com , http://top-prize.com , http://pspd.com.tw , http://guppy.idv.tw , http://goodgame.com.tw , http://mysun.com.tw , http://wang-shian.com.tw , http://hjlog.com , http://welljob.com.tw , http://vdlog.com , http://hongching.com.tw , http://czlog.com , http://maltose.com.tw , http://lerouge.com.tw , http://jdbook.com.tw , http://balsamico.com.tw , http://qxlog.com , http://clnwoman.com.tw , http://bhfow.com , http://penny.com.tw , http://eatviva.com.tw , http://qglog.com , http://chyhua.com.tw , http://findtea.com.tw , http://happydv.com.tw , http://uqlog.com , http://foldcat.com.tw , http://club165.net , http://mhms.com.tw , http://ijlog.com , http://aylog.com , http://hideyoshi.com.tw ,
    4 days ago

    Trackbacks

    The trackback URL for this entry is:
    http://sholliday.spaces.live.com/blog/cns!A68482B9628A842A!595.trak
    Weblogs that reference this entry
    • None