Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 3 of 3

Thread: Help with SQL for Report Looping/Grouping ???

  1. #1
    BFG 9000 is offline Senior Member
    Join Date
    Oct 2006
    Location
    South UK
    Posts
    882

    Question Help with SQL for Report Looping/Grouping ???

    Hi All,

    I have a query that shows what percentage of orders we've shipped in less than 2 working days (i.e. ignoring weekends) - which is working perfectly, but I have to specify the year & month.

    I'd like some help in creating one query which outputs this data by month for the past 2 years.


    Here's the query - you'll see that this is specifying last month only...



    C#/VB.NET Code:
    SELECT CAST(100*(

    SELECT CAST(COUNT(OrderNumber) AS decimal(8,2))
    FROM Orders WHERE YEAR(OrderDate) = 2011 AND Month(OrderDate) = 10 AND (TransactionState'AUTHORIZED' OR TransactionState'CAPTURED') AND CAST(DATEDIFF(d,OrderDate,ShippedOn)+0
    - (DATEDIFF(wk,OrderDate,ShippedOn) + CASE WHEN DATEPART(dw,OrderDate)=1 then 1 else 0 End )
    - (
    DATEDIFF(wk,OrderDate,ShippedOn) + CASE WHEN DATEPART(dw,ShippedOn)=7 then 1 else 0 End ) AS decimal) < 2

    ) / (

    SELECT CAST(COUNT(OrderNumber) AS decimal(8,2))
    FROM Orders WHERE YEAR(OrderDate) = 2011 AND Month(OrderDate) = 10 AND (TransactionState'AUTHORIZED' OR TransactionState'CAPTURED')

    )
     AS 
    decimal(8,2)) 


    Can anyone help?



    TTFN

    BFG

  2. #2
    esedirect is offline Senior Member
    Join Date
    Feb 2010
    Location
    Norfolk, UK
    Posts
    343

    Default

    Something like this:

    Code:
    select t1.theyear, t1.themonth, t1.thecount, t2.theorders
    from (
    select year(orderdate) as theyear, month(orderdate) as themonth, count(*) as thecount
    from orders (nolock)
    where (transactionstate IN ('AUTHORIZED','CAPTURED'))
    and cast(datediff(d,orderdate,shippedon) + 0 -
    (DATEDIFF(wk,OrderDate,ShippedOn) + CASE WHEN DATEPART(dw,OrderDate)=1 then 1 else 0 End ) -
    (DATEDIFF(wk,OrderDate,ShippedOn) + CASE WHEN DATEPART(dw,ShippedOn)=7 then 1 else 0 End ) AS int) < 2
    group by year(orderdate), month(orderdate) ) as t1
    join (
    select year(orderdate) as theyear, month(orderdate) as themonth, count(*) as theorders
    from orders (nolock)
    where (transactionstate IN ('AUTHORIZED','CAPTURED'))
    group by year(orderdate), month(orderdate)) as t2
    on t1.theyear = t2.theyear and t1.themonth = t2.themonth
    order by 1 desc, 2 desc
    http://www.esedirect.co.uk
    --------------------------------------------------------------------------
    Using MS 9.2.0.0 with the following customisations:

    Lightbox/Fancybox enlarged images;
    Auto-suggest searchbox;
    Extra product information shown only to our IP Address (such as supplier info, costs, etc.);
    Failed transactions emailed via trigger;
    Custom app to show basket contents when customer online;
    Orders pushed through to accounting systems.

    All the above without source!

  3. #3
    BFG 9000 is offline Senior Member
    Join Date
    Oct 2006
    Location
    South UK
    Posts
    882

    Default

    Thanks Mate - that is awesome !!!



    TTFN

    BFG