在日期标准上加入3个表

时间:2015-12-09 09:29:37

标签: sql sql-server

我需要帮助。

Tbl:WarehouseInventory

Date       | DelRec | ProductId | Quantity
2015-09-10 | 110    | 1         | 100
2015-09-12 | 111    | 1         | 100
2015-09-12 | 111    | 2         | 200
2015-09-12 | 111    | 3         | 300

Tbl:取款

Date       | ID     | ProductId | Quantity | CustomerId
2015-09-11 | 1      | 1         | 400      | 2
2015-09-12 | 1      | 1         | 100      | 1
2015-09-12 | 2      | 2         | 200      | 1
2015-09-12 | 3      | 3         | 300      | 1

Tbl:客户

Customer Id     | Name 
1               | Somebody
2               | Someone

输出应该是这样的

DelRec | Date Added | ProductId | Stocked | Withdrawn | Customer 
  110  | 2015-09-10 | 1         | 100     | 0         | NULL
  0    | 2015-09-11 | 1         | 0       | 400       | Someone
  111  | 2015-09-12 | 1         | 100     | 100       | Somebody
  111  | 2015-09-12 | 2         | 200     | 200       | Somebody
  111  | 2015-09-12 | 3         | 300     | 300       | Somebody

这是我到目前为止所提出的,它给了我一个错误的输出

select wi.DateAdded as 'Date Added', max(wi.DeliveryReceipt) as 'Delivery Receipt', wi.ProductId as 'Product',
     max(isnull(wi.Quantity, 0)) as 'Stocked', max(isnull(w.Quantity, 0)) as 'Withdrawn', e.Customers as 'Customer'
     from WarehouseInventory wi 
     cross join Withdrawals w 
     cross join Customer e 
     group by wi.DateAdded, wi.ProductId, e.Customers, wi.DeliveryReceipt, w.ProductId

基本上,我需要在日期和产品上加入两个表,如果其中一个表中有空值,只需将其设为0.感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可以使用FULL OUTER JOIN

SELECT DelRec, 
       COALESCE(wi.[Date], wd.[Date]) AS Date_Added,
       COALESCE(wi.ProductId, wd.ProductId) AS ProductId,
       COALESCE(wi.Quantity, 0) AS Stocked,
       COALESCE(wd.Quantity, 0) AS Withdrawn,
       c.Name AS Customer
FROM WarehouseInventory AS wi
FULL OUTER JOIN Withdrawals AS wd 
ON wi.[Date] = wd.[Date] AND wi.ProductId = wd.ProductId
LEFT JOIN Customers AS c ON c.[Customer Id] = wd.CustomerId 
ORDER BY Date_Added

答案 1 :(得分:1)

您的示例表与查询之间存在一些不一致之处,但这是基本要点:

  • 您希望在产品和日期上全部加入仓库交货(A)和提款(B)表
  • 确保您为日期和产品合并(A,B)
  • 对每个表中的数量求和,然后在每个聚合外部合并以获得零(因为一列可以全为空)。

下面:

project.ext.react = [
        // the name of the generated asset file containing your JS bundle
    bundleAssetName: "index.android.bundle",

    // the entry file for bundle generation
    entryFile: "index.android.js",

    // whether to bundle JS and assets in debug mode
    bundleInDebug: false,

    // whether to bundle JS and assets in release mode
    bundleInRelease: true,

    // the root of your project, i.e. where "package.json" lives
    root: "../../",

    // where to put the JS bundle asset in debug mode
    jsBundleDirDebug: "$buildDir/intermediates/assets/debug",

    // where to put the JS bundle asset in release mode
    jsBundleDirRelease: "$buildDir/intermediates/assets/release",

    // where to put drawable resources / React Native assets, e.g. the ones you use via
    // require('./image.png')), in debug mode
    resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",

    // where to put drawable resources / React Native assets, e.g. the ones you use via
    // require('./image.png')), in release mode
    resourcesDirRelease: "$buildDir/intermediates/res/merged/release",

    // by default the gradle tasks are skipped if none of the JS files or assets change; this means
    // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
    // date; if you have any other folders that you want to ignore for performance reasons (gradle
    // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
    // for example, you might want to remove it from here.
    inputExcludes: ["android/**", "ios/**"]
]