Inventory margin displayed in the view

Source: Internet
Author: User

Effect:


Create view [DBO]. [v_show_stockinorderitem]

As
With cte_detail
(
-- Number of warehouse receiving records
Select S. productid, S. colorsid, S. sizesid, S. Title, S. colorsname, S. sizesname, S. quantity as Qty
From thebeerhouse. stockinitems s
Left join thebeerhouse. stockin Si ON Si. stockinid = S. stockinid

Where Si. Status = 'audited' -- the flag that has been reviewed,

-- Thebeerhouse. stockin is equivalent to an order.

-- Thebeerhouse. stockinitems is associated with the order table. It details the product, quantity, color, and size of the order.


Union all

-- Warehouse picking quantity
Select O. productid as oproductid, O. colorsid as ocolorsid, O. sizesid as osizesid, O. Title As otitle,
O. colorsname as ocolorsname, O. sizesname as osizesname,-o. quantity as Qty
From thebeerhouse. orderitems o
Left join thebeerhouse. Orders OO on OO. orderid = O. orderid

Where oo. shippingstatus = 'applyshipped '-- a warehouse marker

-- Thebeerhouse. Orders

-- Thebeerhouse. orderitems is associated with the order table. It details the products, quantities, colors, and sizes of the order to be exported.

)

-- Combine the above results

, Cte_qty
(
Select productid, sum (qty) as qty, title, sizesid, sizesname, colorsid, colorsname -- qty indicates the inbound volume-outbound volume, that is, inventory margin
From cte_detail
Where productid = productid and colorsid = colorsid and sizesid = sizesid
Group by productid, title, sizesid, sizesname, colorsid, colorsname
)
Select row_number () over (order by T. productid) as 'data identified'
, Product ID = T. productid
, Row_number () over (order by T. productid) as 'sequence number'
, Product no. = T. productid
, Product name = T. Title
, Color = T. colorsname
, Size = T. sizesname
, Total warehouse receiving amount = (select isnull (sum (S. quantity), 0)
From thebeerhouse. stockinitems s
Left join thebeerhouse. stockin Si ON Si. stockinid = S. stockinid
Where Si. Status = 'audited' and S. productid = T. productid and S. sizesid = T. sizesid and S. colorsid = T. colorsid)
, Total sales = (select isnull (sum (O. quantity), 0)
From thebeerhouse. orderitems o
Left join thebeerhouse. Orders OO on OO. orderid = O. orderid
Where oo. shippingstatus = 'applyshipped 'and O. productid = T. productid and O. sizesid = T. sizesid and O. colorsid = T. colorsid)
, Inventory quantity = isnull (T. qty, 0)
From
Cte_qty as t

Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.