Difference between revisions of "VbzCart/queries"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
(→‎Customers: moved query to separate page)
(→‎new queries: many more queries no longer in db)
 
(45 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
* '''qryCbx_''': queries used for filling comboboxes. The unique ID will always be the first field, and the text to display will always be the second field; additional fields may be provided for use in further '''qryCbx_''' queries which show a subset of the results.
 
* '''qryCbx_''': queries used for filling comboboxes. The unique ID will always be the first field, and the text to display will always be the second field; additional fields may be provided for use in further '''qryCbx_''' queries which show a subset of the results.
 
* '''qryCat_''': queries which build catalog numbers or information by joining multiple tables. (In Access, the convention was '''qryCatNum_'''.)
 
* '''qryCat_''': queries which build catalog numbers or information by joining multiple tables. (In Access, the convention was '''qryCatNum_'''.)
 +
==by category==
 
===Inactive===
 
===Inactive===
 
* [[/deprecated]]: queries we're trying to [[/discarded|get rid of]], once we're sure nothing uses them
 
* [[/deprecated]]: queries we're trying to [[/discarded|get rid of]], once we're sure nothing uses them
Line 13: Line 14:
 
===Catalog===
 
===Catalog===
 
* [[/qryCat_Depts]]
 
* [[/qryCat_Depts]]
* [[/qryTitles_ItTyps_grpItems]]
+
* '''Title'''-centric:
* [[/qryTitles_ItTyps_ItTyps]]
+
** [[/qryTitles_Item_info]]
* [[/qryTitles_ItTyps_Titles]]
+
** [[/qryTitles_ItTyps_grpItems]]
* [[/qryItTypsDepts_grpItems]]
+
** [[/qryTitles_ItTyps_ItTyps]]
* [[/qryItTypsDepts_ItTyps]]
+
** [[/qryTitles_ItTyps_Titles]]
* [[/qryCat_Titles]]
+
** [[/qryTitles_Imageless]] - titles with no active images
* [[/qryCat_Titles_Item_stats]]
+
** [[/qryCat_Titles]]
* [[/qryCat_Titles_web]]
+
** [[/qryCat_Titles_Item_stats]] -- item/stock statistics
* [[/qryCbx_Titles]]
+
** [[/qryCat_Titles_Item_count]] -- simpler query for maintenance
===Catalog Items===
+
** [[/qryCat_Titles_web]]
* [[/qryCat_Items]]
+
** [[/qryCbx_Titles]]
 +
* '''Item'''-centric:
 +
** [[/qryCat_Items_Stock]]: cat_items with stock info
 +
* '''ItTyp'''-centric:
 +
** [[/qryItTypsDepts_grpItems]]
 +
** [[/qryItTypsDepts_ItTyps]]
 +
* '''Image'''-centric:
 +
** [[/qryImgs_byTitle]]: Image info by Title
 +
* [[/qryCat_pages]]: maps http path info to catalog entities
 +
====Catalog Items====
 
* [[/qryCbx_Items_data]]
 
* [[/qryCbx_Items_data]]
 
** [[/qryCbx_Items]]
 
** [[/qryCbx_Items]]
Line 30: Line 40:
 
** [[/qryCbx_Items_opt]]: abbreviated version for contexts where Title is already known
 
** [[/qryCbx_Items_opt]]: abbreviated version for contexts where Title is already known
 
* [[/qryItems_prices]]: what uses this?
 
* [[/qryItems_prices]]: what uses this?
 
+
====Catalog Sources====
===Catalog Sources===
 
 
* [[/qryCtg_Sources_active]]
 
* [[/qryCtg_Sources_active]]
 
* [[/qryCtg_Items_updates]]
 
* [[/qryCtg_Items_updates]]
 
* [[/qryCtg_Items_updates_joinable]]
 
* [[/qryCtg_Items_updates_joinable]]
 
* [[/qryCtg_Items_active]]
 
* [[/qryCtg_Items_active]]
* [[/qryCtg_Titles_active]]
+
* <s>[[/qryCtg_build_sub]]</s>
* [[/qryCtg_build_sub]]
+
* <s>[[/qryCtg_build]]</s>
* [[/qryCtg_build]]
+
* '''[[VbzCart catalog building|building]] process''':
 +
** [[/qryCtg_Items_forUpdJoin]]
 +
** [[/qryCtg_Upd_join]]
 +
** [[/qryCtg_src_dups]]
 +
** [[/qryCtgCk_dup_keys]]
  
===Orders===
+
====Catalog Topics====
 +
* '''titles x topics''':
 +
** [[/qryTitleTopic_Titles]]: more title information
 +
** [[/qryTitleTopic_Topics]]: more topic information
 +
** [[/qryTitleTopic_Title_avail]]: title availability information
 +
===Ordering===
 +
====Carts====
 +
* [[/qryCarts_info]]
 +
** [[/qrySub_Carts_info_data]]
 +
** [[/qrySub_Carts_info_items]]
 +
====Customers====
 +
* [[/qryCustAddrs]]
 +
* [[/qryCbx_CustNames]]
 +
====Orders====
 
* [[/qryCbx_Orders]]
 
* [[/qryCbx_Orders]]
 
* [[/qryOrderLines_notPkgd]]
 
* [[/qryOrderLines_notPkgd]]
Line 52: Line 78:
 
* [[/qryItms_to_restock_union]]
 
* [[/qryItms_to_restock_union]]
 
* [[/qryItms_to_restock]]
 
* [[/qryItms_to_restock]]
* [[/qryItms_to_restock_w_info]]
+
====Packages====
 
+
* [[/qryPkgLines_byOrdLine_andItem]]
===Customers===
+
* [[/qryOrdLines_PkgdQtys]]
* [[/qryCustAddrs]]
+
* [[/qryOrdLines_open]]
 +
* [[/qryOrders_Pulled]]
 +
* [[/qryPkgs_Pull_status]]
 +
* for reports:
 +
** [[/qryRpt_Pkg_Lines]]
 +
*** [[/qryPkgLines_qtys_done]]
 +
*** <s>[[/qryPkgLines_qtys_done_ord_sum]]</s> - not used
 +
** [[/qryRpt_Pkg_Trx]]
  
===Packages===
+
====Restocks====
====qryPkgLines_byOrdLine_andItem====
+
* '''all restock requests''':
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryPkgLines_byOrdLine_andItem AS
+
** [[/qryRstks_info]]
SELECT
+
** [[/qryRstkReq_Item_Rcd_status]]
  ID_OrdLine,
+
** [[/qryRstkReq_Item_status]]
  ID_Item,
+
*** [[/qryRstkReq_Item_status_Req_info]]
  Sum(
+
*** [[/qryRstkReq_Items_expected]]: show only expected items
    IFNULL(pl.QtyShipped,0)+
+
** [[/qryCbx_RstkReq]]
    IFNULL(pl.QtyNotAvail,0)+
+
*** [[/qryRstkReq_by_status]]
    IFNULL(pl.QtyKilled,0)
+
*** [[/qryRstkReq_by_PurchOrd]]
    ) AS QtyDone,
+
* '''filtered by status''':
  Sum(pl.QtyShipped) AS QtyShipped,
+
** [[/qryRstks_active]]: not terminated = !(closed, orphaned or killed - see {{l/vc/term|restock/status}})
  Sum(pl.QtyNotAvail) AS QtyNotAvail,
+
*** [[/qryRstks_unsent]]: created but not ordered yet
  Sum(pl.QtyKilled) AS QtyKilled,
+
**** [[/qryRstkItms_unsent]]
  Sum(pl.QtyExtra) AS QtyExtra
+
***** [[/qryRstkItms_unsent_for_order]]
FROM ord_pkg_lines AS pl
+
** [[/qryRstks_inactive]]: all the rest
GROUP BY ID_OrdLine, ID_Item;</mysql>
 
<section end=sql />
 
====qryOrdLines_PkgdQtys====
 
* '''Depends on''': [[#qryPkgLines_byOrdLine_andItem]]
 
* '''Used by''': order detail in Item table
 
* '''History''':
 
** '''2008-11-21''': Added CatNum, Descr, OrdNum, WhenStarted, ID_Pull fields
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryOrdLines_PkgdQtys AS
 
SELECT
 
  ol.ID,
 
  ol.ID_Order,
 
  o.Number AS OrdNum,
 
  o.WhenStarted AS OrdDate,
 
  o.ID_Pull,
 
  ol.ID_Item,
 
  ol.CatNum,
 
  ol.Descr,
 
  ol.QtyOrd,
 
  pl.QtyDone,
 
  pl.QtyShipped,
 
  pl.QtyExtra,
 
  pl.QtyNotAvail,
 
  pl.QtyKilled
 
FROM
 
      (ord_lines AS ol
 
    LEFT JOIN qryPkgLines_byOrdLine_andItem AS pl ON pl.ID_OrdLine=ol.ID)
 
  LEFT JOIN core_orders AS o ON ol.ID_Order=o.ID;</mysql>
 
<section end=sql />
 
  
====qryOrdLines_open====
+
====Shipping====
=====old version=====
+
* [[/qryPkgs_status]]
'''Depends on''': [[#qryOrdLines_PkgdQtys]]
 
 
 
Very slow -- about takes 40 seconds
 
<mysql>CREATE OR REPLACE VIEW qryOrdLines_open AS
 
SELECT
 
  ol.ID,
 
  o.ID AS ID_Order,
 
  ol.ID_Item,
 
  Sum(ol.QtyOrd) AS QtyOrd,
 
  Sum(ol.QtyDone) AS QtyDone,
 
  Min(WhenOpened) AS WhenOldestOrder,
 
  Min(WhenNeeded) AS WhenSoonestDue
 
FROM qryOrders_Active AS o LEFT JOIN qryOrdLines_PkgdQtys AS ol ON ol.ID_Order=o.ID
 
WHERE IFNULL(ol.QtyOrd,0)-IFNULL(ol.QtyDone,0)<>0
 
GROUP BY ol.ID, o.ID, ol.ID_Item;</mysql>
 
=====new version=====
 
* '''Requires''': [[#qryOrders_Active]], [[#qryPkgLines_byOrdLine_andItem]]
 
* '''Notes''': Much faster -- about 7 seconds
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryOrdLines_open AS
 
SELECT
 
  ol.*,
 
  QtyDone,
 
  o.Number,
 
  WhenNeeded,
 
  WhenStarted
 
FROM
 
  (ord_lines AS ol
 
    LEFT JOIN qryOrders_Active AS o
 
    ON ol.ID_Order=o.ID)
 
  LEFT JOIN qryPkgLines_byOrdLine_andItem AS pl
 
  ON pl.ID_OrdLine=ol.ID
 
WHERE (o.ID IS NOT NULL) AND (IFNULL(QtyDone,0) <> QtyOrd)
 
ORDER BY Number, ID_Item;</mysql>
 
<section end=sql />
 
* '''History''':
 
** '''2008-11-02''' +WhenNeeded, +WhenStarted
 
 
 
===Restocks===
 
====qryCbx_Restocks====
 
''' Notes''': MySQL gives a "query cancelled" error when running this SQL to create the view, but actually it creates it just fine.
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryCbx_Restocks_byStatus AS
 
SELECT
 
  r.ID,
 
  CAST(
 
    CONCAT(
 
      CAST(COALESCE(
 
        CONCAT('X ',DATE_FORMAT(r.WhenKilled,'%Y-%m-%d')),
 
        CONCAT('R ',DATE_FORMAT(r.WhenReceived,'%Y-%m-%d')),
 
        CONCAT('O ',DATE_FORMAT(r.WhenOrdered,'%Y-%m-%d')),
 
        CONCAT('D ',DATE_FORMAT(r.WhenDebited,'%Y-%m-%d')),
 
        CONCAT('C ',DATE_FORMAT(r.WhenCreated,'%Y-%m-%d'))
 
      ) AS BINARY),
 
      ' ',
 
      s.CatKey,
 
      IFNULL(CONCAT(' [',r.PurchOrdNum,']'),'')
 
    ) AS CHAR
 
  ) AS Descr,
 
  NOT IsLocked AS IsOpen,
 
  r.WhenOrdered,
 
  r.WhenReceived,
 
  r.WhenConfirmed,
 
  r.ID_Supplier
 
FROM core_restocks AS r LEFT JOIN cat_supp AS s ON r.ID_Supplier=s.ID
 
ORDER BY COALESCE(r.WhenKilled,r.WhenReceived,r.WhenOrdered,r.WhenDebited,r.WhenCreated) DESC;
 
 
 
CREATE OR REPLACE VIEW qryCbx_Restocks_byPurchOrd AS
 
SELECT
 
  r.ID,
 
  CAST(r.PurchOrdNum AS CHAR) AS Descr,
 
  NOT IsLocked AS IsOpen,
 
  r.WhenOrdered,
 
  r.WhenReceived,
 
  r.WhenConfirmed,
 
  r.ID_Supplier
 
FROM core_restocks AS r WHERE r.PurchOrdNum IS NOT NULL
 
ORDER BY COALESCE(r.WhenKilled,r.WhenReceived,r.WhenOrdered,r.WhenDebited,r.WhenCreated) DESC;
 
 
 
CREATE OR REPLACE VIEW qryCbx_Restocks AS
 
  SELECT * FROM qryCbx_Restocks_byStatus
 
UNION
 
  SELECT * FROM qryCbx_Restocks_byPurchOrd;</mysql>
 
<section end=sql />
 
 
 
====qryFrm_RestockLines====
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryFrm_RestockLines AS
 
SELECT
 
  ri.ID,
 
  ri.ID_Restock,
 
  ri.ID_Item,
 
  ci.CatNum,
 
  ci.Supp_CatNum,
 
  ci.PriceSell,
 
  ci.QtyMin_Stk,
 
  o.Number AS OrderName,
 
  ci.Descr AS ItemDescr,
 
  o.WhenNeeded,
 
  If(t.RstkMin = 1, '', t.RstkMin) AS RstkMinStr,
 
  ri.QtyOrd,
 
  ri.QtyNeed,
 
  ri.QtyExp,
 
  ri.InvcLineNo,
 
  ri.InvcQtyOrd,
 
  ri.InvcQtySent,
 
  ri.isGone,
 
  ri.QtyRecd,
 
  ri.QtyFiled,
 
  ri.CostExpPer,
 
  ri.CostInvPer,
 
  ri.CostInvTot,
 
  ri.CostActTot,
 
  ri.CostActBal,
 
  ri.Notes
 
FROM
 
(
 
  (rstk_lines AS ri
 
    LEFT JOIN core_orders AS o ON ri.ID_Order=o.ID)
 
  LEFT JOIN qryCbx_Items_data AS ci ON ri.ID_Item=ci.ID)
 
LEFT JOIN cat_titles AS t ON ci.ID_Title=t.ID;</mysql>
 
<section end=sql />
 
 
 
* A lot of these fields were added back when this query was still the data source for qryFrm_RestockLines_byItem; this is no longer the case. Probably the only customer for this query is the Restock Items entry form, so any fields not needed there can probably be done away with.
 
 
 
====qryFrm_RestockLines_byItem====
 
Basically the same as qryFrm_RestockLines, but grouped by ID_Item and with more Item-related fields
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryFrm_RestockLines_byItem AS
 
SELECT
 
  CONCAT(ri.ID_Item,'-',ri.ID_Restock) AS Key_RstkItem,
 
  ri.ID_Item,
 
  ri.ID_Restock,
 
  i.Descr AS ItemDescr,
 
  i.CatNum,
 
  i.Supp_CatNum,
 
  SUM(IFNULL(ri.QtyOrd,0)) AS QtyOrd,
 
  SUM(IFNULL(ri.QtyExp,0)) AS QtyExp,
 
  ri.CostExpPer,
 
  i.PriceSell,
 
  i.QtyMin_Stk
 
FROM rstk_lines AS ri LEFT JOIN qryCbx_Items_data AS i ON ri.ID_Item=i.ID
 
GROUP BY ri.ID_Restock, ri.ID_Item, i.CatNum, i.Supp_CatNum, ri.CostExpPer, i.PriceSell, i.QtyMin_Stk
 
ORDER BY i.CatNum;</mysql>
 
<section end=sql />
 
====qryRstks_info====
 
* '''Returns''': Information about restock requests and receipts, one line per receipt (or request, if nothing received)
 
* '''History''':
 
** '''2008-11-30''' Created for new restock process
 
* '''Notes''':
 
** Creates one list by left-joining received restocks with their requests, another list of requests with no receipts, and then UNIONs them together
 
* '''Requires''': [[VbzCart/tables/rstk req|rstk_req]], [[VbzCart/tables/rstk rcd|rstk_rcd]]
 
* '''Used by''': (MS Access) sfrm_SuppRstks
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstks_info AS
 
SELECT
 
  rq.ID AS ID_Req,
 
  rq.ID_Supplier,
 
  rq.PurchOrdNum,
 
  rq.SuppOrdNum,
 
  rq.WhenCreated,
 
  rq.WhenOrdered,
 
  rq.WhenKilled,
 
  rq.WhenOrphaned,
 
  rq.WhenClosed,
 
  rq.Notes AS NotesReq,
 
  rc.ID AS ID_Rcd,
 
  rc.SuppInvcNum,
 
  rc.WhenReceived,
 
  rc.TotalInvMerch,
 
  rc.TotalInvFinal,
 
  rc.Notes AS NotesRcd
 
FROM
 
  rstk_rcd AS rc LEFT JOIN rstk_req AS rq ON rc.ID_Restock=rq.ID
 
UNION
 
SELECT
 
  rq.ID AS ID_Req,
 
  rq.ID_Supplier,
 
  rq.PurchOrdNum,
 
  rq.SuppOrdNum,
 
  rq.WhenCreated,
 
  rq.WhenOrdered,
 
  rq.WhenKilled,
 
  rq.WhenOrphaned,
 
  rq.WhenClosed,
 
  rq.Notes AS NotesReq,
 
  rc.ID AS ID_Rcd,
 
  rc.SuppInvcNum,
 
  rc.WhenReceived,
 
  rc.TotalInvMerch,
 
  rc.TotalInvFinal,
 
  rc.Notes AS NotesRcd
 
FROM
 
  rstk_req AS rq LEFT JOIN rstk_rcd AS rc ON rc.ID_Restock=rq.ID
 
  WHERE rc.ID IS NULL;</mysql>
 
<section end=sql />
 
====qryRstks_active====
 
* '''Returns''': List of restock requests for which we are currently expecting one or more shipments
 
* '''History''':
 
** '''2008-11-19''' Created for new restock process
 
** '''2008-11-20''' Including WhenClosed (new field) in the filter
 
* '''Notes''':
 
** We don't want to exclude requests just because a shipment has been received, because that shipment might not have included everything in the request. (We may need to add a WhenClosed field so we can indicate that no more shipments are expected.)
 
** At one point, doing a JOIN with rstk_rcd seemed to make the query run faster, but this may have been an illusion due to caching by MySQL and not enough testing.
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstks_active AS
 
  SELECT rq.* FROM rstk_req AS rq
 
    WHERE (rq.WhenClosed IS NULL) AND (rq.WhenKilled IS NULL) AND (rq.WhenOrphaned IS NULL);</mysql>
 
<section end=sql />
 
 
 
====qryRstkLines_en_route====
 
* '''Deprecated''': this uses the old restock tables
 
* '''Returns''': Lines from restocks which exist but have not yet received or cancelled. It's assumed that any restocks just sitting around (not ordered but not cancelled) are being prepared and will be used for an actual order.
 
<mysql>CREATE OR REPLACE VIEW qryRstkLines_en_route AS
 
SELECT ri.*
 
FROM rstk_lines AS ri LEFT JOIN core_restocks AS r ON ri.ID_Restock=r.ID
 
WHERE (r.WhenReceived IS NULL) AND (r.WhenKilled IS NULL) AND (rq.WhenOrphaned IS NULL);</mysql>
 
====qryRstkItms_active====
 
* '''Returns''': Data about items which are in active restocks, i.e. on restock orders but not yet received (or cancelled).
 
* '''Note''': This is different from qryRstkLines_en_route (deprecated):
 
** Grouped by Item instead of Restock Line
 
** Uses new Restock tables
 
* '''Requires''': rstk_req_item, [[#qryRstks_active]]
 
* '''History''':
 
** '''2008-11-18''' Created for new restock process
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstkItms_active AS
 
SELECT
 
  rrq.ID_Restock,
 
  rrq.ID_Item,
 
  SUM(rrq.QtyOrd) AS QtyOrd
 
FROM
 
      rstk_req_item AS rrq
 
    LEFT JOIN qryRstks_active AS rq ON rrq.ID_Restock=rq.ID
 
WHERE rq.ID IS NOT NULL
 
GROUP BY rrq.ID_Restock, rrq.ID_Item;</mysql>
 
<section end=sql />
 
 
 
====qryRstkItms_en_route====
 
* '''Returns''': Restock items requested, not yet received, but possibly expected because the restock request hasn't been closed or orphaned.
 
* '''Requires''': rstk_rcd_line, rstk_rcd, [[#qryRstkItms_active]]
 
* '''History''':
 
** '''2008-11-19''' Created for new restock process
 
** '''2008-11-24''' QtyRecd falls back on QtyFiled if null
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstkItms_en_route AS
 
SELECT
 
  rqi.ID_Restock,
 
  rqi.ID_Item,
 
  SUM(rqi.QtyOrd) AS QtyOrd,
 
  SUM(IFNULL(rcl.QtyRecd,rcl.QtyFiled)) AS QtyRecd
 
FROM
 
  (rstk_rcd_line AS rcl
 
    LEFT JOIN rstk_rcd AS rc ON rcl.ID_RstkRcd=rc.ID)
 
    LEFT JOIN qryRstkItms_active AS rqi ON (rc.ID_Restock=rqi.ID_Restock) AND (rcl.ID_Item=rqi.ID_Item)
 
GROUP BY rqi.ID_Restock, rqi.ID_Item
 
HAVING IFNULL(QtyOrd,0)-IFNULL(QtyRecd,0);</mysql>
 
<section end=sql />
 
 
 
====v_rstk_lines_wItemInfo====
 
* '''Status''': deprecated (uses old restock tables)
 
<mysql>CREATE OR REPLACE VIEW v_rstk_lines_wItemInfo AS
 
SELECT
 
  ri.*,
 
  i.ID_Title,
 
  IFNULL(r.WhenOrdered,r.WhenCreated) AS WhenDone
 
FROM
 
  (rstk_lines AS ri
 
  LEFT JOIN cat_items AS i ON ri.ID_Item=i.ID)
 
  LEFT JOIN core_restocks AS r ON ri.ID_Restock=r.ID;</mysql>
 
====qryRstkReq_Item_Rcd_status====
 
* '''Returns''': Information about received shipments for a given Item in a given Restock Request
 
* '''History''':
 
** '''2008-11-21''' Created for new restock process
 
** '''2008-11-23''' WhenOrdered -> WhenFirstOrder (MIN), WhenFinalOrder (MAX)
 
* '''Requires''': rstk_rcd_line, rstk_rcd, rstk_req
 
* '''Used by''': [[#qryRstkReq_Item_status]]
 
* '''Notes''':
 
** '''WhenFirstOrder''' is the timestamp of the ''earliest'' open customer order for this item. This is so we know the longest time that anyone has been waiting for this item, and hence how urgent the restock is.
 
** '''WhenFinalOrder''' is the timestamp of the ''latest'' open customer order for this item. This is so we know if there have been further requests for this item, adding further urgency.
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstkReq_Item_Rcd_status AS
 
SELECT
 
  rq.ID AS ID_RstkReq,
 
  MIN(rq.WhenOrdered) AS WhenFirstOrder,
 
  MAX(rq.WhenOrdered) AS WhenFinalOrder,
 
  rcl.ID_Item,
 
  SUM(IFNULL(rcl.QtyFiled,rcl.QtyRecd)) AS QtyRecd
 
FROM
 
      (rstk_rcd_line AS rcl
 
    LEFT JOIN rstk_rcd AS rc ON rcl.ID_RstkRcd=rc.ID)
 
  LEFT JOIN rstk_req AS rq ON rc.ID_Restock=rq.ID
 
GROUP BY rq.ID, rcl.ID_Item;</mysql>
 
<section end=sql />
 
 
 
====qryRstkReq_Item_status====
 
* '''Returns''': Information about items Restock Requested and how many have been Received
 
* '''History''':
 
** '''2008-11-21''' Created for new restock process
 
* '''Requires''': [[#qryRstkReq_Item_Rcd_status]]
 
* '''Notes''':
 
** '''WhenFirstOrder / WhenFinalorder''' are timestamps of ''customer'' orders for this item
 
* '''Used by''': [[#qryRstkReq_Item_status_Req_info]]
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstkReq_Item_status AS
 
SELECT
 
  rci.*,
 
  rqi.QtyOrd,
 
  rqi.Notes
 
FROM
 
    rstk_req_item AS rqi
 
  LEFT JOIN qryRstkReq_Item_Rcd_status AS rci ON (rqi.ID_Restock=rci.ID_RstkReq) AND (rqi.ID_Item=rci.ID_Item)
 
WHERE rci.ID_RstkReq IS NOT NULL
 
ORDER BY rci.ID_RstkReq, rci.ID_Item;</mysql>
 
<section end=sql />
 
====qryRstkReq_Item_status_Req_info====
 
* '''Returns''': Data from [[#qryRstkReq_Item_status]] with some additional info about the request
 
* '''History''':
 
** '''2008-11-23''' Created for new restock process
 
* '''Requires''': [[#qryRstkReq_Item_status]], rstk_req
 
* '''Used by''': sfrmItem_Rstks
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryRstkReq_Item_status_Req_info AS
 
SELECT
 
  rqis.*,
 
  IFNULL(rq.WhenOrdered,rq.WhenCreated) AS WhenMade,
 
  rq.WhenClosed,
 
  rq.WhenKilled
 
FROM qryRstkReq_Item_status AS rqis LEFT JOIN rstk_req AS rq ON rqis.ID_RstkReq=rq.ID
 
ORDER BY WhenMade,ID_RstkReq;</mysql>
 
<section end=sql />
 
 
 
====qryRstkReq_wItem_info====
 
* '''Status''': not being used
 
* '''History''':
 
** '''2008-11-21''' Created for new restock process
 
<mysql>CREATE OR REPLACE VIEW qryRstkReq_wItem_info AS
 
SELECT
 
  rqi.*,
 
  i.ID_Title,
 
  r.WhenCreated,
 
  r.WhenOrdered
 
FROM
 
  (rstk_req_item AS rqi
 
  LEFT JOIN cat_items AS i ON rqi.ID_Itme=i.ID)
 
  LEFT JOIN rstk_req AS rq ON rqi.ID_Restock=rq.ID;</mysql>
 
  
 
===Stock===
 
===Stock===
====v_stk_lines_remaining====
+
====new queries====
View listing stk_items actually in stock, which is actually different things:
+
* [[/qryStk_Bins_w_info]]
* "forSale": items which are visible as "in stock" to customers
+
* [[/qryStk_lines_remaining]]
* "forShip": items which are available for shipping
+
** [[/qryStk_lines_remaining_byBin]]
* items neither for shipping nor for sale (probably some kind of recordkeeping purpose)
+
** [[/qryStk_lines_remaining_forSale]]
<section begin=sql /><mysql>CREATE OR REPLACE VIEW v_stk_lines_remaining AS
+
*** [[/qryStkItms_for_sale]]
  SELECT
 
    st.ID,
 
    st.ID_Bin,
 
    st.ID_Item,
 
    IF(sb.isForSale,st.Qty,0) AS QtyForSale,
 
    IF(sb.isForShip,st.Qty,0) AS QtyForShip,
 
    st.Qty AS QtyExisting,
 
    st.CatNum,
 
    st.WhenAdded,
 
    st.WhenChanged,
 
    st.WhenCounted,
 
    st.Notes,
 
    sb.ID_Place,
 
    sp.Name AS WhName
 
    FROM
 
      (
 
        stk_items AS st
 
        LEFT JOIN stk_bins AS sb
 
          ON sb.ID=st.ID_Bin
 
      )
 
      LEFT JOIN stk_places AS sp
 
        ON sb.ID_Place=sp.ID
 
    WHERE (st.WhenRemoved IS NULL) AND (sb.WhenVoided IS NULL) AND (st.Qty <> 0);</mysql>
 
<section end=sql />
 
====v_stk_items_remaining====
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW v_stk_items_remaining AS
 
  SELECT
 
    ID_Item,
 
    SUM(QtyForSale) AS QtyForSale,
 
    SUM(QtyForShip) AS QtyForShip,
 
    SUM(QtyExisting) AS QtyExisting
 
  FROM v_stk_lines_remaining
 
  GROUP BY ID_Item;</mysql>
 
<section end=sql />
 
====v_stk_titles_remaining====
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW v_stk_titles_remaining AS
 
  SELECT
 
    ID_Title,
 
    SUM(QtyForSale) AS QtyForSale,
 
    SUM(QtyForShip) AS QtyForShip,
 
    SUM(QtyExisting) AS QtyExisting
 
  FROM v_stk_lines_remaining AS s LEFT JOIN cat_items AS i ON s.ID_Item=i.ID
 
  GROUP BY i.ID_Title;</mysql>
 
<section end=sql />
 
 
 
====v_stk_byItemAndBin====
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW v_stk_byItemAndBin AS
 
  SELECT
 
    ID_Item,
 
    ID_Bin,
 
    SUM(QtyForSale) AS QtyForSale,
 
    SUM(QtyForShip) AS QtyForShip,
 
    SUM(QtyExisting) AS QtyExisting
 
  FROM v_stk_lines_remaining
 
  GROUP BY ID_Item, ID_Bin
 
  HAVING SUM(QtyExisting)>0;</mysql>
 
<section end=sql />
 
====v_stk_byItemAndBin_wItemInfo====
 
Is anything actually using this? '''Document!''' (I've tentatively removed the link in Access.)
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW v_stk_byItemAndBin_wItemInfo AS
 
  SELECT * FROM v_stk_byItemAndBin AS s LEFT JOIN qryCat_Items AS i ON s.ID_Item=i.ID;</mysql>
 
<section end=sql />
 
 
 
====v_stk_byItemAndBin_wInfo====
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW v_stk_byItemAndBin_wInfo AS
 
  SELECT
 
    s.*,
 
    i.*,
 
    b.ID_Place,
 
    b.Code AS BinCode,
 
    b.Descr AS BinDescr
 
  FROM
 
  (
 
    v_stk_byItemAndBin AS s LEFT JOIN qryCat_Items AS i ON s.ID_Item=i.ID
 
  ) LEFT JOIN stk_bins AS b ON s.ID_Bin=b.ID;</mysql>
 
<section end=sql />
 
====qryStk_lines_remaining_forSale====
 
* '''Requires''': [[#v_stk_lines_remaining]]
 
* '''Used by''': [[#qryStkItms_for_sale]]
 
* '''Note''': Eventually rename as "qryStkLines_for_sale"
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStk_lines_remaining_forSale AS
 
  SELECT *
 
  FROM v_stk_lines_remaining
 
  WHERE QtyForSale>0;</mysql>
 
<section end=sql />
 
 
 
====qryStkItms_for_sale====
 
* '''Requires''': [[#qryStk_lines_remaining_forSale]]
 
* '''Used by''': [[#qryStkItms_for_sale_wItem_data]]
 
* '''History''':
 
** '''2008-11-17''' Created for revised restocking process
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStkItms_for_sale AS
 
SELECT
 
  ID_Item,
 
  SUM(qsl.QtyForSale) AS QtyForSale,
 
  SUM(qsl.QtyForShip) AS QtyForShip,
 
  SUM(qsl.QtyExisting) AS QtyExisting
 
FROM qryStk_lines_remaining_forSale AS qsl
 
GROUP BY ID_Item;</mysql>
 
<section end=sql />
 
 
 
====qryStkItms_for_sale_wItem_data====
 
* '''Requires''': [[#qryStkItms_for_sale]]
 
* '''Note''': this one may be redundant; the time when we need the item data is *after* all the sources of items-to-restock have been UNIONed together and GROUPed by item
 
* '''History''':
 
** '''2008-11-17''' Created for revised restocking process
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStkItms_for_sale_wItem_data AS
 
SELECT
 
  qsi.*,
 
  i.*
 
FROM qryStkItms_for_sale AS qsi LEFT JOIN cat_items AS i ON qsi.ID_Item=i.ID;</mysql>
 
<section end=sql />
 
 
 
====qryOrdLines_ord_status====
 
* '''Depends on''': [[#qryOrdLines_open]], [[#qryStk_lines_remaining_forSale]], [[#qryRstkLines_en_route]]
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryOrdLines_ord_status AS
 
SELECT
 
  i.ID AS ID_Item,
 
  i.CatNum,
 
  qoi.ID AS ID_OrdLine,
 
  qoi.ID_Order,
 
  qsr.ID AS ID_Stock,
 
  qri.ID AS ID_RstkItem,
 
  IFNULL(qoi.QtyOrd,0) AS QtyOrd,
 
  IFNULL(qoi.QtyDone,0) AS QtyDone,
 
  IFNULL(qri.QtyExp IS NULL,IFNULL(qri.QtyOrd,0)) AS QtyOnRstk,
 
  IFNULL(qsr.QtyForShip,0) AS QtyOnHand,
 
  i.QtyMin_Stk AS QtyMin,
 
  qoi.WhenOldestOrder,
 
  qoi.WhenSoonestDue
 
FROM
 
  (
 
    (cat_items AS i LEFT JOIN qryOrdLines_open AS qoi ON qoi.ID_Item=i.ID)
 
  LEFT JOIN qryStk_lines_remaining_forSale AS qsr ON qsr.ID_Item=i.ID)
 
LEFT JOIN qryRstkLines_en_route AS qri ON qri.ID_Item=i.ID;</mysql>
 
<section end=sql />
 
 
 
====qryOrdLines_to_restock====
 
* '''Depends on''': [[#qryOrdLines_ord_status]]
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryOrdLines_to_restock AS
 
SELECT
 
  ID_Item,
 
  i.ID_OrdLine,
 
  i.ID_Order,
 
  Sum(i.QtyOrd) AS QtyOrd,
 
  Sum(i.QtyDone) AS QtyDone,
 
  Sum(i.QtyOnRstk) AS QtyOnRstk,
 
  Sum(i.QtyOnHand) AS QtyOnHand,
 
  Max(i.QtyMin) AS QtyMin,
 
  Max(IFNULL(i.QtyMin,0))+Sum(IFNULL(i.QtyOrd,0)-IFNULL(i.QtyOnRstk,0)-IFNULL(i.QtyOnHand,0)) AS QtyToGet,
 
  Sum(IFNULL(i.QtyOrd,0)-IFNULL(i.QtyOnRstk,0)-IFNULL(i.QtyOnHand,0)) AS QtyShort,
 
  Min(i.WhenOldestOrder) AS WhenOldestOrder,
 
  Min(i.WhenSoonestDue) AS WhenSoonestDue,
 
  Count(ID_RstkItem) AS RstkLines
 
FROM qryOrdLines_ord_status AS i
 
GROUP BY ID_Item, ID_OrdLine, ID_Order
 
HAVING Max(IFNULL(i.QtyMin,0))+Sum(IFNULL(i.QtyOrd,0)-IFNULL(i.QtyOnRstk,0)-IFNULL(i.QtyOnHand,0))>0;</mysql>
 
<section end=sql />
 
 
 
====qryFrm_OrdLines_to_restock====
 
'''Depends on''': [[#qryOrdLines_to_restock]]
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryFrm_OrdLines_to_restock AS
 
SELECT
 
  qi.*,
 
  ci.CatNum,
 
  ci.isForSale,
 
  ci.ID_Supplier,
 
  ci.PriceSell,
 
  ci.PriceBuy
 
FROM qryOrdLines_to_restock AS qi
 
  LEFT JOIN qryCat_Items AS ci ON ci.ID=qi.ID_Item;</mysql>
 
<section end=sql />
 
 
 
====qryStk_lines_Title_info====
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStk_lines_Title_info AS
 
SELECT
 
  s.*,
 
  i.ID_Title
 
FROM stk_items AS s LEFT JOIN cat_items AS i ON s.ID_Item=i.ID;</mysql>
 
<section end=sql />
 
====qryOrdLines_status_union====
 
* '''Requires''': [[#qryOrdLines_open]], [[#qryRstkLines_en_route]], [[#v_stk_lines_remaining]]
 
* '''Notes''': This is another one of those union queries that is "cancelled" while you're trying to create it... but in this case, it does get created.
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryOrdLines_status_union AS
 
SELECT
 
/* 1 */  CONCAT("oio-",oio.ID) AS Source,
 
/* 2 */  oio.ID AS ID,
 
/* 3 */  oio.ID_Order,
 
/* 4 */  oio.ID_Item,
 
/* 5 */  oio.QtyOrd,
 
/* 6 */  oio.QtyDone,
 
/* 7 */  0 AS QtyOnRstk,
 
/* 8 */  0 AS QtyOnHand,
 
/* 9 */  0 AS QtyMin,
 
/* 10 */ oio.WhenStarted,
 
/* 11 */ oio.WhenNeeded
 
    FROM qryOrdLines_open AS oio
 
UNION
 
SELECT
 
/* 1 */ CONCAT("rie-",rie.ID) AS Source,
 
/* 2 */ rie.ID AS ID,
 
/* 3 */ rie.ID_Order,
 
/* 4 */  rie.ID_Item,
 
/* 5 */  0 AS QtyOrd,
 
/* 6 */  0 AS QtyDone,
 
/* 7 */  IF(rie.QtyExp > rie.QtyOrd,rie.QtyExp,rie.QtyOrd) AS QtyOnRstk,
 
/* 8 */  0 AS QtyOnHand,
 
/* 9 */  0 AS QtyMin,
 
/* 10 */  NULL AS WhenStarted,
 
/* 11 */  NULL AS WhenNeeded
 
  FROM qryRstkLines_en_route AS rie
 
UNION
 
SELECT
 
/* 1 */  CONCAT("srs-",srs.ID) AS Source,
 
/* 2 */  0 AS ID,
 
/* 3 */  NULL AS ID_Order,
 
/* 4 */  srs.ID_Item,
 
/* 5 */  0 AS QtyOrd,
 
/* 6 */  0 AS QtyDone,
 
/* 7 */  0 AS QtyOnRstk,
 
/* 8 */  srs.QtyForShip AS QtyOnHand,
 
/* 9 */  0 AS QtyMin,
 
/* 10 */  NULL AS WhenStarted,
 
/* 11 */  NULL AS WhenNeeded
 
  FROM v_stk_lines_remaining AS srs
 
UNION
 
SELECT
 
/* 1 */  CONCAT("itm-",itm.ID) AS Source,
 
/* 2 */  NULL AS ID,
 
/* 3 */  NULL AS ID_Order,
 
/* 4 */  itm.ID AS ID_Item,
 
/* 5 */  0 AS QtyOrd,
 
/* 6 */  0 AS QtyDone,
 
/* 7 */  0 AS QtyOnRsk,
 
/* 8 */  0 AS QtyOnHand,
 
/* 9 */  QtyMin_Stk AS QtyMin,
 
/* 10 */  NULL AS WhenStarted,
 
/* 11 */  NULL AS WhenNeeded
 
  FROM cat_items AS itm;</mysql>
 
<section end=sql />
 
* '''History''':
 
** '''2008-11-02''' changed When field names (more accurate)
 
 
 
====qryItems_ord_status====
 
* '''Depends on''': [[#qryOrdLines_status_union]]
 
* '''Returns''': Status with regard to ordering, i.e. quantities on hand, quantities needed, etc. Used by the "items needed (for restocking)" form, via [[#qryItems_to_restock]]. This was originally in a different section, but it has to be created after qryOrdLines_status_union.
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryItems_ord_status AS
 
SELECT
 
  i.ID_Item,
 
  SUM(i.QtyOrd) AS QtyOrd,
 
  SUM(i.QtyDone) AS QtyDone,
 
  SUM(i.QtyOnRstk) AS QtyOnRstk,
 
  SUM(i.QtyOnHand) AS QtyOnHand,
 
  MAX(i.QtyMin) AS QtyMin,
 
  SUM(
 
    IFNULL(i.QtyOrd,0)-IFNULL(i.QtyOnRstk,0)-IFNULL(i.QtyOnHand,0)
 
  )+IFNULL(MAX(i.QtyMin),0) AS QtyToGet,
 
  MIN(i.WhenStarted) AS WhenOldestOrder,
 
  MAX(i.WhenNeeded) AS WhenSoonestDue,
 
  COUNT(i.Source) AS RecCount,
 
  CAST(GROUP_CONCAT(DISTINCT i.Source SEPARATOR ' ') AS CHAR) AS Sources
 
FROM qryOrdLines_status_union AS i
 
GROUP BY i.ID_Item
 
HAVING QtyToGet>0;</mysql>
 
<section end=sql />
 
* '''History''':
 
** '''2008-11-02''' mods to accommodate rewritten source data
 
  
====qryItems_to_restock====
+
* [[/qryStk_items_remaining]]
'''Depends on''': [[#qryItems_ord_status]]
+
* [[/qryStk_byItem_byBin]]
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryItems_to_restock AS
 
SELECT
 
  qi.*,
 
  ci.CatNum,
 
  ci.isForSale,
 
  ci.ID_Supplier,
 
  ci.QtyMin_Stk,
 
  ci.QtyMin_Rstk_Title,
 
  ci.PriceSell,
 
  ci.PriceBuy
 
FROM qryItems_ord_status AS qi LEFT JOIN qryCat_Items AS ci ON ci.ID=qi.ID_Item
 
WHERE QtyToGet>0;</mysql>
 
<section end=sql />
 
  
====qryStock_forOpenOrders====
+
* [[/qryStk_lines_Title_info]]
* '''Action''': Show all stock available to fill open orders, along with helpful information about the Bin, Order, and Item
 
* '''Depends on''': [[#v_stk_byItemAndBin_wInfo]], [[#qryOrderLines_notPkgd]], [[#qryCbx_Orders]]
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStock_forOpenOrders AS
 
SELECT
 
  s.ID_Bin,
 
  s.ID_Item,
 
  oi.ID_Order,
 
  oi.QtyOpen,
 
  s.QtyForSale,
 
  s.QtyForShip,
 
  o.WhenNeeded,
 
  o.Descr AS OrdText,
 
  o.ID_Pull,
 
  CONCAT_WS(' ',s.CatNum,s.Descr) AS ItemText,
 
/*  s.BinCode,
 
  s.BinDescr,  are these ever needed? */
 
  CONCAT_WS(' ',s.BinCode,s.BinDescr) AS BinText
 
FROM (v_stk_byItemAndBin_wInfo AS s LEFT JOIN qryOrderLines_notPkgd AS oi ON s.ID_Item=oi.ID_Item) LEFT JOIN qryCbx_Orders AS o ON oi.ID_Order=o.ID
 
WHERE (NOT s.isPulled) AND (o.ID_Pull IS NULL) AND (QtyOpen) AND (QtyForShip);</mysql>
 
<section end=sql />
 
* There's a possibility this query will return wrong information when there are multiple stock lines corresponding to the items needed; further testing would be a good idea.
 
  
====qryStock_byOpt_andType====
+
====old queries====
This is used for displaying the "stock" area. The basic records are items, but they're only ones that are in stock, so "Stock" is shorthand for "items that are in stock" in this case.
+
This was the first batch of queries I created, before I had decided to go with the qry prefix as in Access.
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStock_byOpt_andType AS
+
* [[/v_stk_titles_remaining]]
SELECT
+
* [[/v_stk_byItemAndBin_wItemInfo]]
  i.ID_ItTyp,
 
  i.ID_ItOpt,
 
  SUM(i.qtyInStock) AS qtyInStock,
 
  it.NameSng AS It_NameSng,
 
  it.NamePlr AS It_NamePlr,
 
  io.CatKey AS Io_CatKey,
 
  io.Descr AS Io_Descr,
 
  IFNULL(it.Sort,'ZZZ') AS It_Sort
 
FROM (cat_items AS i LEFT JOIN cat_ioptns AS io ON i.ID_ItOpt=io.ID)
 
  LEFT JOIN cat_ittyps AS it ON i.ID_ItTyp=it.ID
 
GROUP BY i.ID_ItTyp, i.ID_ItOpt, io.Sort
 
HAVING SUM(i.qtyInStock)
 
ORDER BY It_Sort,io.Sort;</mysql>
 
<section end=sql />
 
====qryStock_Titles_most_recent====
 
This defines what the "latest stock" page shows; as given here, it displays the 100 titles most recently added to stock.
 
<section begin=sql /><mysql>CREATE OR REPLACE VIEW qryStock_Titles_most_recent AS
 
SELECT
 
  i.ID_Title,
 
  s.QtyForSale AS QtyAdded,
 
  MAX(s.WhenAdded) AS WhenAdded,
 
  st.QtyForSale AS QtyAvail
 
FROM (v_stk_lines_remaining AS s LEFT JOIN cat_items AS i ON s.ID_Item=i.ID) LEFT JOIN v_stk_titles_remaining AS st ON i.ID_Title=st.ID_Title
 
GROUP BY i.ID_Title
 
HAVING QtyAdded
 
ORDER BY WhenAdded DESC
 
LIMIT 100;</mysql>
 
<section end=sql />
 
  
 
===Caching===
 
===Caching===
 
Caching should only be used for catalog display.
 
Caching should only be used for catalog display.
====v_data_flow====
+
* [[/qryCache_Flow_Procs]]
<section begin=sql /><mysql>  CREATE OR REPLACE VIEW v_data_flow AS
 
    SELECT
 
      df.ID_Srce,
 
      df.ID_Dest,
 
      df.ID_Proc,
 
      dfx.doesClear
 
    FROM data_flow AS df LEFT JOIN data_procs AS dfx ON df.ID_Proc=dfx.ID
 
    ORDER BY dfx.doesClear;</mysql>
 
<section end=sql />
 

Latest revision as of 23:12, 3 March 2016

Navigation

VbzCart: data views

Overview

What MS Access calls "queries" are called "views" in MySQL, i.e. they pull data from existing tables and are themselves usable as data sources in much the same way that tables are (and in which result sets from functions are not).

Some common prefixes:

  • qryCbx_: queries used for filling comboboxes. The unique ID will always be the first field, and the text to display will always be the second field; additional fields may be provided for use in further qryCbx_ queries which show a subset of the results.
  • qryCat_: queries which build catalog numbers or information by joining multiple tables. (In Access, the convention was qryCatNum_.)

by category

Inactive

Catalog

Catalog Items

Catalog Sources

Catalog Topics

Ordering

Carts

Customers

Orders

Packages

Restocks

Shipping

Stock

new queries

old queries

This was the first batch of queries I created, before I had decided to go with the qry prefix as in Access.

Caching

Caching should only be used for catalog display.