如何根据每日股票报告为股票突变编写SQL

时间:2018-05-15 13:01:40

标签: sql sql-server

我正在为我的数据库(Microsoft SQL服务器)提供每日库存报告,我想用SQL报告每日传出的产品/数量。每个收到的货物都会获得一个入站ID,这些也是每个产品都知道的。 对于外向数量,我指的是两件事:

  • 如果相同inboundID的1个产品的数量在第二天减少
  • 如果已知的inboundID(之前见过)从库存报告中消失,则此inboundID的最后一个已知库存报告出库数量

我可以使用SQL查询获得基于此的出站报告吗?

此处样本库存报告:

+-------------+------------------------------+-----------+------------+-----------+
| ArticleCode |         Description          | InboundID | TotalStock |   Date    |
+-------------+------------------------------+-----------+------------+-----------+
| BAM131-L    | Jacket in piqu? Georgia L    |  53800222 |        550 | yesterday |
| BAM131-XXXL | Jacket in piqu? Georgia XXXL |  53800220 |      18000 | yesterday |
| BAM133-XXXL | Shirt piqu? Dave XXXL        |  53800220 |      18000 | yesterday |
| BAM133-XL   | Shirt piqu? Dave XL          |  53800222 |          1 | yesterday |
| BAM133-XXL  | Shirt piqu? Dave XXL         |  53800215 |       1800 | yesterday |
| BAM133-S    | Shirt piqu? Dave S           |  53800215 |        787 | yesterday |
| BAM133-M    | Shirt piqu? Dave M           |  53800215 |         59 | yesterday |
| BAM133-L    | Shirt piqu? Dave L           |  53800218 |         50 | yesterday |
| BAM134-XXL  | Shirt Piqu? Lop XXL          |  53800215 |        700 | yesterday |
| BAM134-L    | Shirt Piqu? Lop L            |  53800218 |        300 | yesterday |
| BAM134-S    | Shirt Piqu? Lop S            |  53800218 |        750 | yesterday |
| BAM134-XL   | Shirt Piqu? Lop XL           |  53800218 |          5 | yesterday |
| BAM134-XS   | Shirt Piqu? Lop XS           |  53800215 |         89 | yesterday |
| BAM135-XL   | Coat Leather Jumper XL       |  53800218 |          1 | yesterday |
| BAM135-L    | Coat Leather Jumper L        |  53800300 |          2 | yesterday |
| BAM135-XXXL | Coat Leather Jumper XXXL     |  53800215 |         60 | yesterday |
| BAM135-XXL  | Coat Leather Jumper XXL      |  53800218 |        297 | yesterday |
+-------------+------------------------------+-----------+------------+-----------+

+-------------+------------------------------+-----------+------------+-----------+
| ArticleCode |         Description          | InboundID | TotalStock |   Date    |
+-------------+------------------------------+-----------+------------+-----------+
| BAM131-L    | Jacket in piqu? Georgia L    |  53800222 |        500 | today|
| BAM131-XXXL | Jacket in piqu? Georgia XXXL |  53800220 |      18000 | today|
| BAM133-XXXL | Shirt piqu? Dave XXXL        |  53800220 |      18000 | today|
| BAM133-XL   | Shirt piqu? Dave XL          |  53800222 |          1 | today|
| BAM133-XXL  | Shirt piqu? Dave XXL         |  53800215 |       1800 | today|
| BAM133-S    | Shirt piqu? Dave S           |  53800215 |        787 | today|
| BAM133-M    | Shirt piqu? Dave M           |  53800215 |         59 | today|
| BAM133-L    | Shirt piqu? Dave L           |  53800218 |         50 | today|
| BAM134-XXL  | Shirt Piqu? Lop XXL          |  53800215 |        700 | today|
| BAM134-L    | Shirt Piqu? Lop L            |  53800218 |        300 | today|
| BAM134-S    | Shirt Piqu? Lop S            |  53800218 |        750 | today|
| BAM134-XL   | Shirt Piqu? Lop XL           |  53800218 |          5 | today|
    | BAM134-XS   | Shirt Piqu? Lop XS           |  53800215 |         89 | today|
    | BAM135-XL   | Coat Leather Jumper XL       |  53800218 |          1 | today|
 today|
    | BAM135-XXXL | Coat Leather Jumper XXXL     |  53800215 |         60 | today|
    | BAM135-XXL  | Coat Leather Jumper XXL      |  53800218 |        297 | today|
    +-------------+------------------------------+-----------+------------+-----------+

期望的结果:

        +-------------+------------------------------+-----------+------------+-----------+
        | ArticleCode |         Description          | InboundID | Gone       |   Date    |
        +-------------+------------------------------+-----------+------------+-----------+
        | BAM131-L    | Jacket in piqu? Georgia L    |  53800222 |          50|      today|
        | BAM131-XXXL | Coat Leather Jumper L        |  53800300 |           2|      today|
        +-------------+------------------------------+-----------+------------+-----------+

1 个答案:

答案 0 :(得分:0)

Select Y.ArticleCode, Y.Description, Y.InboundId, (Y.TotalStock - ISNULL(T.TotalStock,0)) Gone, T.Date
From Yesterday Y
Left Join Today T
On Y.ArticleCode = T.ArticleCode
And Y.InboundID = T.InboundID