日期:2014-05-18  浏览次数:20475 次

急寻高手给一个sql文的解决的办法
SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 01000000) AND (ProdCode < 02000000)
SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 02000000) AND (ProdCode < 03000000)
SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 03000000) AND (ProdCode < 04000000)


这三个sql文能整合成一个么?

------解决方案--------------------
如果都是连续的话,你拆开select干嘛啊,直接select最大最小之间的不就ok了。
SQL code

SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 01000000) AND (ProdCode < 02000000)
SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 02000000) AND (ProdCode < 03000000)
SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 03000000) AND (ProdCode < 04000000)
改:
SELECT sum(val)FROM dbo.T_Main WHERE (ProdCode >= 01000000) AND (ProdCode < 04000000)

------解决方案--------------------
SQL code

SELECT sum(val)FROM dbo.T_Main
 WHERE( (ProdCode >= 01000000) AND (ProdCode < 02000000)
OR (ProdCode >= 02000000) AND (ProdCode < 03000000)
OR (ProdCode >= 03000000) AND (ProdCode < 04000000))