Need SQL Query help with avg

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Need SQL Query help with avg

    Alright SQL fans, I need to create a query for MSSQL that does the following:

    Finds the average difference between the date an order entered the shipping
    line and either the first partial ship date or total ship date for orders
    between specific dates.

    The tables are an order table and an order location table that uses the order
    primary key. The order location table will have multiple entries per order as
    an order goes from received to shipped.

    The end result should be a single decimal value.

    athanasiusrc Guest

  2. Similar Questions and Discussions

    1. Query of Queries on query New type query
      In CF5 we have a page that creates a query, using queryNew and querySetCell and the like, we then used dbtype="query" and gave it's name so we could...
    2. query of query throwing weird exception
      One workaround: I had added rows to a cfsearch query, and set a numeric value in custom1 field that that query provides. A query of queries...
    3. Convert a query to a list, or find an item in a query
      Hi All, I am using CFPOP to retrieve mail from a server, then delete each message after I retrieve it. What I want to do is to check that I don;t...
    4. CAML Query: Multiple Query Fields Issue
      I need to Create a CAML Query Dynamically with VB to a Sharepoint WebService GetListItems Method. The User Could Select 1 to X Number of IDs...
    5. BCP query out executed by xp_cmdshell works fine from query analyzer but fails from VB Component
      Hi all, I have a stored procedure which returns a vast number of record and i have to write the output into a csv file. I'm using BCP utility to...
  3. #2

    Default Re: Need SQL Query help with avg

    OK. Before I essay a response, I need two things:

    1) Your first best try at the query.
    2) A schema for the tables, pointing out the key date fields for use in the query.

    tks,
    philh Guest

  4. #3

    Default Re: Need SQL Query help with avg

    This is a similar query I wrote but since there are multiple possible values
    for the first date, I'm not sure how to formulate that.

    SET
    @OpenOrderAge =
    (SELECT
    avg(datediff(day, getDate(), orderDate))
    FROM
    Orders
    JOIN
    OrderStatuses
    ON
    OrderStatus_Pk = OrderStatus_Fk
    WHERE
    Processing = 1)

    Here is a schema for the tables with the necessary fields

    Orders
    Order_Pk (primary key)

    OrderLocations
    PartialShipment (int),
    DefaultShipped (int)
    OrderLocation_Pk (primary key)

    OrderLocationHistory
    Order_Fk (foreign key from Orders)
    OrderLocation_Fk (foreign key from OrderLocations)
    OrderLocationHistoryTS (datetime field stamped with date order changed
    locations)

    The OrderLocationHistoryTS for when partiallyshipped = 1 or defaultshipped = 1
    needs to be between two dates. The difference will need to be found between the
    ship date and the first partially shipped date or the defaultshipped date -
    which ever is older.

    athanasiusrc Guest

  5. #4

    Default Re: Need SQL Query help with avg

    'what's the schema for OrderStatuses? By "partiallyshipped" are you referring to the PartialShipment field in OrderLocations? What table and field holds the ship date?
    philh Guest

  6. #5

    Default Re: Need SQL Query help with avg

    Order Statuses contains a text description of the status (Received, In Process,
    Shipped First Class, etc.) and a primary key (orderstatus_pk)

    The date of shipment is contained in the orderlocationhistory table. One of
    the possible options in this table is partially shipped.

    I could add a "first shipment" timestamp to the orders table if that would
    make the query work better.

    athanasiusrc Guest

  7. #6

    Default Re: Need SQL Query help with avg

    In your original query, where is the field "OrderDate" from? I thought we
    were looking for an average of the difference in final ship date vs earliest
    default or partial ship date.

    I'm still having trouble understanding how you differentiate "ship date" from
    "partial ship date" or "default ship date." Please explain further.

    philh Guest

  8. #7

    Default Re: Need SQL Query help with avg

    The original query was a sample that did something similar to what I am doing
    now. The orderdate is the date the order was placed but is irrelevant to this
    question.

    The Orderlocation field has columns for "default shipped" and "partial" that
    take 1 or 0 as values. This allows the system to properly choose which location
    to use when an order is shipped. If the entire order is shipped, it uses the
    "default shipped" row. If the order is only partially shipped, it chooses the
    partial row.

    I need to know the average time orders in a date range take to go from the
    Order Location row "shipping line" to either "partial" or "default shipped".

    athanasiusrc Guest

  9. #8

    Default Re: Need SQL Query help with avg

    Well.

    I had asked for your first best attempt at a query that returns exactly the
    results you're seeking, not an unrelated query.

    Help me to understand your shipment status and location scheme. Your
    OrderLocations table holds two states of shipments (partial and default), but
    in your discussion you mention three states (partial, default, and shipped).
    How is the actual ship date flagged? Do multiple partials constitute a
    complete shipment? Is a "defaultshipped" a single shipment? Can you have both
    partial and defaults for the same order?

    philh Guest

  10. #9

    Default Re: Need SQL Query help with avg

    The OrderLocationHistory table keeps a record of all the places an order has
    been. Other locations can include "pending payment", "partial shelf", etc. An
    order can have several partial ship statuses that eventually end in the default
    shipped status when the whole order goes out. An order can also skip the
    partial ship status and go straight to default shipped if there is only one
    shipment.

    It is possible that an order could have the following history:
    Printer
    In process
    Shipping line
    Partial shipment
    Shipping line
    default shipped

    Would it be easier to put a date stamp in the order itself when an order is
    shipped for the first time and then subtract the initial shipping line status
    from the order location table?

    athanasiusrc Guest

  11. #10

    Default Re: Need SQL Query help with avg

    OK, got it.
    It may be easier to mark the order, but that wouldn't satisfy your business
    rules.

    I take it a shipping line entry always precedes a shipment.

    Show me the Order Statuses schema and how it relates to the Locations table,
    and we can get started on a query.

    philh Guest

  12. #11

    Default Re: Need SQL Query help with avg

    OrderStatuses is a more detailed explanation of what was done with an order and
    includes things like partially shipped, shipped first class, shipped airmail,
    pending payment, cancelled, etc. Apart from different statuses triggering the
    orderlocationhistory additions, there isn't a correlation.

    For example, all statuses in the orderstatuses table that involve shipped
    products have a field "shipped" marked 1. When one of these is chosen, it
    triggers the addition of the shipped orderlocation history addition in the
    orderlocationhistory table. If a partial status is chosen it triggers the
    partially shipped status in the orderlocationhistory.

    When I say trigger, I mean that the status is added to orderlocationhistory
    through sql logic, not using a trigger statement in the database.

    I guess another possible option would be to only use the orderstatuses table
    and create a location history from that instead of from a separate table.

    athanasiusrc Guest

  13. #12

    Default Re: Need SQL Query help with avg

    Originally posted by: athanasiusrc


    Finds the average difference between the date an order entered the shipping
    line and either the first partial ship date or total ship date for orders
    between specific dates.

    q]

    In what table/field, or combinations thereof, would I determine the state
    "shipping line"?



    philh Guest

  14. #13

    Default Re: Need SQL Query help with avg

    The OrderLocations table has a column that can either be 1 or 0 for Shipping Line.
    athanasiusrc Guest

  15. #14

    Default Re: Need SQL Query help with avg

    Originally posted by: athanasiusrc


    It is possible that an order could have the following history:
    Printer
    In process
    Shipping line
    Partial shipment
    Shipping line
    default shipped

    ____________
    The OrderLocations table has a column that can either be 1 or 0 for Shipping
    Line.


    Which column? I don't see a column in your schema that relates to this
    information.

    I assume from your history example that each line I quoted above is a distinct
    entry. How do I tell the difference between "Shipping line" and "partial
    shipment"?



    philh Guest

  16. #15

    Default Re: Need SQL Query help with avg

    Sorry, here is the whole schema for the orderlocations table

    orderlocation_pk int
    orderlocation varchar
    defaultpending int
    defaultshipped int
    defaultprinter int
    shippingline int
    partialshipment int

    The various rows are marked either 1 or 0 in these columns to determine what
    kind of location they are.

    athanasiusrc Guest

  17. #16

    Default Re: Need SQL Query help with avg

    OK. I'll pick at this over the weekend.
    philh Guest

  18. #17

    Default Re: Need SQL Query help with avg

    I understand that. If the query is too convoluted I can change the way we keep track of shipping dates to make this work better.

    Thanks for your help.
    athanasiusrc Guest

  19. #18

    Default Re: Need SQL Query help with avg

    As long as you can live with a stored procedure, this should be doable. I'll try again today.
    philh Guest

  20. #19

    Default Re: Need SQL Query help with avg

    A stored procedure is actually preferable since that is what we use for almost all database access on the site.
    athanasiusrc Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139