字段数据相加
insert #aa(item,zt) select TD004,TD008-TD015 from PURTD,#ReTable where TD016 ='N' and TD018 ='Y' and TD004=#ReTable.item
SELECT #ReTable.Lvl,#ReTable.item,INVMC.MC007,#aa.zt,#ReTable.MB004,INVMC.MC002,#ReTable.MB002,#ReTable.MB003 FROM #ReTable,INVMC,PURTD,#aa where INVMC.MC001=#ReTable.item and #ReTable.item=#aa.item
SET NOCOUNT OFF
怎样用Group by语句将select后面的#aa表格下的zt字段中的数据相加
请搞手指点。
[解决办法]
SELECT ReTable.Lvl,
max(#ReTable.item) as Item,
max(INVMC.MC007) as Mc007,
sum(#aa.zt) sum_zt,
max(#ReTable.MB004) as Mb004,
max(INVMC.MC002) as mc002,
max(#ReTable.MB002)as mb002,
max(#ReTable.MB003) as mb003
FROM #ReTable,INVMC,PURTD,#aa
where INVMC.MC001=#ReTable.item and #ReTable.item=#aa.item
group by ReTable.item,INVMC.MC007,#ReTable.MB004,INVMC.MC002,#ReTable.MB002,#ReTable.MB00
[解决办法]
-->假设D: c3 c4-------------------- -------------------- -------------------- --------------------------------------------------0.0.0.0 0.255.255.255 IANA CZ88.NET1.0.0.0 1.255.255.255 IANA CZ88.NET2.0.0.0 2.255.255.255 IANA CZ88.NET3.0.0.0 3.255.255.255 美国 新泽西通用电气公司4.0.0.0 4.9.255.255 美国 CZ88.NET4.10.0.0 4.10.255.255 美国 新泽西州立大学4.11.0.0 4.11.255.255 美国 夏威夷4.12.0.0 4.19.77.255 美国 CZ88.NET4.19.78.0 4.19.78.255 美国 西南政法大学4.19.79.0 4.19.79.63 美国 Armed Forces Radio/Television4.19.79.64 4.21.176.255 美国 CZ88.NET4.21.177.0 4.21.177.255 美国 西南政法大学4.21.178.0 4.36.124.127 美国 CZ88.NET4.36.124.128 4.36.124.255 美国 Technical Resource Connections Inc4.36.125.0 4.36.127.255 美国 坦帕大学4.36.128.0 4.37.215.255 美国 CZ88.NET4.37.216.0 4.37.219.255 美国 德克萨斯女子大学4.37.220.0 4.41.60.255 美国 CZ88.NET4.41.61.0 4.41.61.255 美国 EARTHLINK公司4.41.62.0 4.43.68.255 美国 CZ88.NET4.43.69.0 4.43.69.95 美国 罗斯门特学院4.43.69.96 4.208.48.255 美国 Genuity用户4.208.49.0 4.208.49.255 美国 加洲*/drop table #
[解决办法]
select item,sum(zt) from #aagroup by item--是这个意思吗?
[解决办法]
不就是这样吗?
select item,sum(zt) from #aagroup by item
[解决办法]
select item,sum(zt) from #aagroup by item--这个就是那个意思啊,item相同的汇总zt啊?
[解决办法]
SELECT ReTable.Lvl, #ReTable.item, max(INVMC.MC007) as Mc007, sum(#aa.zt) sum_zt, max(#ReTable.MB004) as Mb004, max(INVMC.MC002) as mc002, max(#ReTable.MB002)as mb002, max(#ReTable.MB003) as mb003 FROM #ReTable,INVMC,PURTD,#aa where INVMC.MC001=#ReTable.item and #ReTable.item=#aa.item group by ReTable.Lvl,ReTable.item
------解决方案--------------------
SELECT max(ReTable.Lvl) as Lvl, #ReTable.item, max(INVMC.MC007) as Mc007, sum(#aa.zt) sum_zt, max(#ReTable.MB004) as Mb004, max(INVMC.MC002) as mc002, max(#ReTable.MB002)as mb002, max(#ReTable.MB003) as mb003 FROM #ReTable,INVMC,PURTD,#aa where INVMC.MC001=#ReTable.item and #ReTable.item=#aa.item group by ReTable.item
[解决办法]
select item,sum(zt) from #aagroup by item--先对#aa进行上面的出来放到另一个临时表,--再和其他关联!!
[解决办法]
--关联也就用了#aa的item和zt字段!select item,sum(zt) zt into #aaafrom #aagroup by item
[解决办法]
insert #aa(item,zt) select TD004,TD008-TD015 from PURTD,#ReTable where TD016='N' and TD018='Y' and TD004=#ReTable.itemselect item,sum(zt) zt into #aaafrom #aagroup by itemSELECT #ReTable.Lvl,#ReTable.item,INVMC.MC007, #aaa.zt,#ReTable.MB004,INVMC.MC002, #ReTable.MB002,#ReTable.MB003 FROM #ReTable,INVMC,PURTD,#aaa where INVMC.MC001=#ReTable.item and #ReTable.item=#aaa.item
[解决办法]
--加个distinct或重新修改你的语句逻辑,--不知道你的表结构及之间的关系!SELECT #ReTable.Lvl,#ReTable.item,INVMC.MC007, #aa.zt,#ReTable.MB004,INVMC.MC002, #ReTable.MB002,#ReTable.MB003 FROM #ReTable,INVMC,PURTD,#aa where INVMC.MC001=#ReTable.item and #ReTable.item=#aa.item
[解决办法]
--你写的这个语句本身就有重复数据的,
--自己看看吧!难怪会有你以下说的问题
--zt的数据有问题
--本来只有几千的数量
--但按照你们的语句写完之后运行出来的zt数据非常大
[解决办法]
--不了解,那就这样:
SELECT distinct #ReTable.Lvl,#ReTable.item,INVMC.MC007,
#aaa.zt,#ReTable.MB004,INVMC.MC002,
#ReTable.MB002,#ReTable.MB003
FROM #ReTable,INVMC,PURTD,#aaa
where INVMC.MC001=#ReTable.item
and #ReTable.item=#aaa.item