缩写查询

时间:2018-09-07 01:13:11

标签: sql ms-access

我的确有一个要在Access中运行的下一个查询,它很好,但是如您所见,前三个SELECT中有一个相似之处。我想知道是否有可能对此进行优化,因为我将添加大量的 c

Original Table

SELECT id, IIF(c1>=0, c1, ) AS positive, IIF(c1<0, c1, ) AS negative  FROM tblcostos 

UNION ALL

SELECT id, IIF(c2>=0, c2, ) AS positive, IIF(c2<0, c2, ) AS negative  FROM tblcostos 

UNION ALL 

SELECT id, IIF(c3>=0, c3, ) AS positive, IIF(c3<0, c3, ) AS negative  FROM tblcostos

UNION ALL 

SELECT 'sum  positivo + negative' AS id, SUM(c1 + c2 + c3) AS positive, "" AS negative FROM tblcostos

ORDER BY id;

Query Table

我要求决赛桌像这样显示,因为我最终将得到具有该格式的.txt

1 个答案:

答案 0 :(得分:-2)

我不知道您要归档什么,但似乎您想按ID对表进行排序,然后将它们分为正数和负数,然后将它们加总。

'Order after ID
SELECT ID FROM tblcosts ORDER BY ID DESC 'or mabye ASC

'Positive and Negative columns
SELECT ID, IIF( ID > 0, ID, Null) AS Positive, IIF( ID < 0, ID, Null) AS Negative FROM tblcosts ORDERBY ID DESC

'Sum
SELECT SUM(ID) AS SummeID FROM tblcosts
相关问题