2

我有两个表 Assets_In 和 Assets_Out。我想知道我的库存资产。

资产_IN

ASSET_TYPE  ASSET_IN_COUNT
Laptops             3
Desktops            2
Desktops            2
Laptops             2
Laptops             2
Keyboards           5
Keyboards           5
Monitors            4
AC Adapters         3
Mouse               10
Monitors            2
Monitors            2
Backpacks           10
Desktops            2
Backpacks           3

Asset_Out

ASSET_TYPE  ISSUED_TO
Desktops    Ram
Monitors    Shyam
Keyboards   Ranjan

我已经写了 2 个查询,但我无法将这两个查询结合起来以实现我的结果。

SELECT ASSET_TYPE Asset, SUM(ASSET_IN_COUNT) Count 
FROM ASSETS_IN GROUP BY ASSET_TYPE

这让我知道我们收到了多少资产

SELECT ASSET_TYPE Asset,COUNT(ASSET_TYPE) 
FROM ASSETS_OUT  GROUP BY ASSET_TYPE

它显示了我们发行了多少资产。

结果应该显示我们有多少资产库存。即是手头资产减去已发行资产。你能帮我解决这个问题吗?

4

2 回答 2

0
Select a.Asset, a.Asset_in, b.Asset_issued, a.Asset_in - b.Asset_issued 
from
    (SELECT ASSET_TYPE Asset, SUM(ASSET_IN_COUNT) Asset_in Count 
     FROM ASSETS_IN 
     GROUP BY ASSET_TYPE) a

join

    (SELECT ASSET_TYPE Asset,COUNT(ASSET_TYPE) Asset_issued 
     FROM ASSETS_OUT 
     GROUP BY ASSET_TYPE) b

on a.Asset = b.Asset
于 2013-07-01T11:37:15.143 回答
0

尝试这个

SELECT ASSET_TYPE AS Asset
, SUM(ASSET_IN_COUNT) - (SELECT COUNT(ASSET_TYPE) FROM ASSETS_OUT 
WHERE ASSET_TYPE=AI.ASSET_TYPE) AS [Count] 
FROM ASSETS_IN AI GROUP BY ASSET_TYPE
于 2013-07-01T11:51:27.417 回答