SQL Server 2008中子查询计算基于多个项目
我有几个股票表,使用数据如下
进馆:
Item_ID Barcode ITEM_CODE Cp_Id whLocId dt_added COLOUR_ID MRN_NO
-----------------------------------------------------------------------------
123456 222222 62626262 1 78 2014-10-31 23:04:02.920 2 99897
123456 222222 62626262 1 78 2014-10-30 23:04:11.340 2 99897
123456 222222 62626262 1 78 2014-07-23 23:04:16.273 2 99897
123456 222222 62626262 1 78 2014-10-21 23:04:19.327 2 99897
123456 222222 1234 1 74 2014-11-01 00:16:42.783 1 99897
123456 222222 1234 1 74 2014-10-24 00:16:53.447 1 99897
123456 222222 1234 1 74 2014-10-23 00:16:55.467 1 99897
123456 222222 1234 1 74 2014-10-23 00:17:00.283 2 99897
123456 222222 1234 1 74 2014-10-23 00:17:02.080 2 99897
123456 222222 1234 1 70 2014-10-23 00:17:10.273 2 99897
123456 222222 12348 1 70 2014-10-23 00:17:14.080 2 99897
123456 222222 12348 1 78 2014-10-24 00:18:37.340 2 99897
123456 222222 62626262 2 78 2014-10-24 00:24:38.977 2 99897
内:
Item_ID Barcode ITEM_CODE Cp_Id whLocId dt_added COLOUR_ID PO_NO
-----------------------------------------------------------------------------
123456 222222 62626262 1 78 2014-10-20 23:04:58.487 2 99897
123456 222222 62626262 1 78 2014-10-30 23:05:04.863 2 99897
123456 222222 12348 1 70 2014-10-26 00:17:39.780 2 99897
123456 222222 12348 1 78 2014-10-26 00:18:19.903 2 99897
BLOCK:
Item_ID Barcode ITEM_CODE Cp_Id whLocId dt_added COLOUR_ID PO_NO
-----------------------------------------------------------------------------
123456 222222 62626262 1 78 2014-10-31 23:03:43.923 2 99897
123456 222222 1234 1 74 2014-11-01 00:16:24.490 1 99897
我试图产生将设在ITEM_CODE一个关闭报告,colour_id采用等
我尝试下面的逻辑,但每个项目的填充相同的输出。
`SELECT
p.ITEM_CODE as ITEM_CODE,
--(select COUNT(*) from inward where cast(dt_added as DATE)<>CAST(GETDATE() as DATE) group by item_code,whlocid)- (select COUNT(*) from outward where cast(dt_added as DATE)<>CAST(GETDATE() as DATE)and item_code='62626262' and colour_ID='2' group by item_code,whlocid)as Opening_Stock,
(select COUNT(*) from inward where cast(dt_added as DATE)=CAST(GETDATE() as DATE)) as DAY_INWARD_Stock,
(select COUNT(*) from inward) as 'TOTAL_INWARD_Stock',
(select COUNT(*) from OUTWARD where cast(dt_added as DATE)=CAST(GETDATE() as DATE)) as DAY_OUTWARD_Stock,
(select COUNT(*) from OUTWARD) as TOTAL_OUTWARD_Stock,
--((select COUNT(*) from inward)-(select COUNT(*) from outward)) as TOTAL_AVAILABLE_STOCK,
(select COUNT(*) from BLOCK where cast(dt_added as DATE)=CAST(GETDATE() as DATE))as DAY_BLOCK_Stock,
(select COUNT(*) from BLOCK) as TOTAL_BLOCK_Stock,
p.cp_id,
p.whLocId,
p.COLOUR_ID
from inward p
left join outward out
on p.item_code=out.item_code
left join block blo
on p.item_code=blo.item_code
group by p.item_code,p.cp_id,p.colour_id,p.whlocid`
但它显示所有项目的相同值。
结果:
ITEM_CODE DAY_INWARD_Stock TOTAL_INWARD_Stock DAY_OUTWARD_Stock TOTAL_OUTWARD_Stock DAY_BLOCK_Stock TOTAL_BLOCK_Stock cp_id whLocId COLOUR_ID
-----------------------------------------------------------------------------
1234 1 13 0 4 1 2 1 74 1
1234 1 13 0 4 1 2 1 70 2
1234 1 13 0 4 1 2 1 74 2
12348 1 13 0 4 1 2 1 70 2
12348 1 13 0 4 1 2 1 78 2
62626262 1 13 0 4 1 2 1 78 2
62626262 1 13 0 4 1 2 2 78 2
这有利于Machha谁是封闭2天希望的问题是明确和格式现在都很好
感谢
**试过,但其根据商品代码隔离但不符合cp_id,colour_id,whlocid **
`SELECT
p.ITEM_CODE as ITEM_CODE,
--(select COUNT(*) from inward where cast(dt_added as DATE)<>CAST(GETDATE() as DATE) group by item_code,whlocid)- (select COUNT(*) from outward where cast(dt_added as DATE)<>CAST(GETDATE() as DATE)and item_code='62626262' and colour_ID='2' group by item_code,whlocid)as Opening_Stock,
(select COUNT(*) from inward where cast(dt_added as DATE)=CAST(GETDATE() as DATE) and item_code=p.item_code) as DAY_INWARD_Stock,
(select COUNT(*) from inward where item_code=p.item_code) as 'TOTAL_INWARD_Stock',
(select COUNT(*) from OUTWARD where cast(dt_added as DATE)=CAST(GETDATE() as DATE)and item_code=p.item_code) as DAY_OUTWARD_Stock,
(select COUNT(*) from OUTWARD where item_code=p.item_code) as TOTAL_OUTWARD_Stock,
--((select COUNT(*) from inward)-(select COUNT(*) from outward)) as TOTAL_AVAILABLE_STOCK,
(select COUNT(*) from BLOCK where cast(dt_added as DATE)=CAST(GETDATE() as DATE)and item_code=p.item_code)as DAY_BLOCK_Stock,
(select COUNT(*) from BLOCK where item_code=p.item_code) as TOTAL_BLOCK_Stock,
p.cp_id,
p.whLocId,
p.COLOUR_ID
from inward p
left join outward out
on p.item_code=out.item_code
left join block blo
on p.item_code=blo.item_code
group by p.item_code,p.cp_id,p.colour_id,p.whlocid`
结果:
ITEM_CODE DAY_INWARD_Stock TOTAL_INWARD_Stock DAY_OUTWARD_Stock TOTAL_OUTWARD_Stock DAY_BLOCK_Stock TOTAL_BLOCK_Stock cp_id whLocId COLOUR_ID
-----------------------------------------------------------------------------
1234 1 6 0 0 1 1 1 74 1
1234 1 6 0 0 1 1 1 70 2
1234 1 6 0 0 1 1 1 74 2
12348 0 2 0 2 0 0 1 70 2
12348 0 2 0 2 0 0 1 78 2
62626262 0 5 0 2 0 1 1 78 2
62626262 0 5 0 2 0 1 2 78 2
`SELECT
p.ITEM_CODE as ITEM_CODE,
(select COUNT(*) from inward where cast(dt_added as DATE)<>CAST(GETDATE() as DATE) and item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid )- (select COUNT(*) from outward where cast(dt_added as DATE)<>CAST(GETDATE() as DATE) and item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid)as Opening_Stock,
(select COUNT(*) from inward where cast(dt_added as DATE)=CAST(GETDATE() as DATE) and item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid) as DAY_INWARD_Stock,
(select COUNT(*) from inward where item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid) as 'TOTAL_INWARD_Stock',
(select COUNT(*) from OUTWARD where cast(dt_added as DATE)=CAST(GETDATE() as DATE)and item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid) as DAY_OUTWARD_Stock,
(select COUNT(*) from OUTWARD where item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid) as TOTAL_OUTWARD_Stock,
((select COUNT(*) from inward where item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid)-(select COUNT(*) from outward where item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid )) as TOTAL_AVAILABLE_STOCK,
(select COUNT(*) from BLOCK where cast(dt_added as DATE)=CAST(GETDATE() as DATE)and item_code=p.item_code and cp_id=p.cp_id and colour_id=p.colour_id and whlocid=p.whlocid)as DAY_BLOCK_Stock,
(select COUNT(*) from BLOCK where item_code=p.item_code) as TOTAL_BLOCK_Stock,
p.cp_id,
p.whLocId,
p.COLOUR_ID
from inward p
left join outward out
on p.item_code=out.item_code
left join block blo
on p.item_code=blo.item_code
group by p.item_code,p.cp_id,p.colour_id,p.whlocid`
看来这一个工作。但任何其他易升以及执行方法将是真正的帮助
感谢 获得以下错误最后一个查询 消息209,级别16,状态1,行23 不明确的列名 'ITEM_CODE'。 消息209,级别16,状态1,行28 不明确的列名称“ITEM_CODE”。 我试图将其绑定到向内和pRecords但其不接受 消息8120,级别16,状态1,行20 ,因为它不是在聚合载列“inward.ITEM_CODE”在选择列表中无效函数或GROUP BY子句。 消息209,级别16,状态1,行28 不明确的列名称“ITEM_CODE”。 请建议。 – 2014-11-01 07:09:59
@PradeepKumar对不起,没有得到更快。无论如何,代码现在应该工作。通过更改子查询中的ITEM_CODE名称来修复它。 – KHeaney 2014-11-03 13:49:18