我从未使用过sybase,直到今天。我在Access中有一个使用First()的查询,并且有多个iif语句。我希望最好并粘贴我的查询,我在First()部分遇到错误。我从查询中删除了First,并且我在iif语句中遇到错误。
这是我的问题:
SELECT Client.ClientId, First(Trans.SysId) AS FirstOfSysId, Client.Name1,
Relation.Name1, Debtor.Name1, Account.AcctId, First(Trans.PostDate) AS FirstOfPostDate,
Sum(IIf([TransType]="uc",[Amount],IIf([TransType]="CA",[Amount],IIf([TransType]="RAC",
[Amount],IIf([TransType]="UCBR",[Amount],[amount]*-1))))) AS TransAmount,
Trans.CheckId,Relation.RelId
FROM (((Trans INNER JOIN Account ON Trans.SysAcctId = Account.SysId)
INNER JOIN Debtor ON Account.SysDtrId = Debtor.SysId)
INNER JOIN Relation ON Account.SysRelId = Relation.SysId) INNER JOIN Client ON
Relation.SysClientId = Client.SysId
WHERE (((Trans.TransType)="CA" Or (Trans.TransType)="NC" Or (Trans.TransType)="UC" Or
(Trans.TransType)="AC"
Or (Trans.TransType)="UCB" Or (Trans.TransType)="RAC" Or (Trans.TransType)="UCB" Or
(Trans.TransType)="UCBR"))
GROUP BY Client.ClientId, Client.Name1, Relation.Name1, Debtor.Name1, Account.AcctId,
Trans.CheckId, Relation.RelId
HAVING (((Client.ClientId)=[Forms]![frmCredit]![Combo112])
AND ((Sum(IIf([TransType]="uc",[Amount],IIf([TransType]="CA",[Amount],
IIf([TransType]="RAC", [Amount],IIf([TransType]="UCBR",[Amount],[amount]*-1))))))>0
Or (Sum(IIf([TransType]="uc",[Amount],IIf([TransType]="CA",[Amount],
IIf([TransType]="RAC", [Amount],IIf([TransType]="UCBR",[Amount],[amount]*-1))))))<0)
AND ((Relation.RelId) Not Like "281099"))
ORDER BY Relation.Name1, Account.AcctId, Trans.CheckId
[编辑] http://sqlfiddle.com/#!2/4b98e0/1 [/编辑]
添加了更具可读性的查询我无法找到转换第一个函数的文档,但是我应该将它转换为Case when语句吗?我看了一篇文章here,但我不确定它是否最有效,因为它是从1999年开始......
我得到的错误是Syntax error near 'FIRST' on line 1.
根据文章here我正在使用正确的语法。
在正确的语法中使用First的任何帮助,如果我应该使用Case而不是iif,我们非常感谢。
答案 0 :(得分:1)
基于我认为查询的内容,我认为您可以重写它,如下所示。我无权访问任何Sybase服务器来验证查询,因此我在MS SQL上尝试了它,但我注释了一些可能不需要Sybase的/ * * /。
这可能是完全错误的,或者可能是正确的。如果错了,请在评论中告诉我,我会删除我的答案。
SELECT
Client.ClientId,
FIRST_VALUE(Trans.SysId) /* OVER (ORDER BY Trans.Sysid) */ AS FirstOfSysId,
Client.Name1,
Relation.Name1,
Debtor.Name1,
Account.AcctId,
FIRST_VALUE(Trans.PostDate) /* OVER (ORDER BY Trans.postdate) */ AS FirstOfPostDate,
SUM(CASE
WHEN [TransType] IN ('uc', 'CA', 'RAC', 'UCBR') THEN AMOUNT
ELSE AMOUNT * -1
END
) AS TransAmount,
Trans.CheckId,
Relation.RelId
FROM Trans
INNER JOIN Account ON Trans.SysAcctId = Account.SysId
INNER JOIN Debtor ON Account.SysDtrId = Debtor.SysId
INNER JOIN Relation ON Account.SysRelId = Relation.SysId
INNER JOIN Client ON Relation.SysClientId = Client.SysId
WHERE Trans.TransType IN ('CA','NC','UC','AC','UCB','RAC','UCB','UCBR')
GROUP BY
Client.ClientId, Client.Name1,
Relation.Name1, Debtor.Name1,
Account.AcctId, Trans.CheckId,
Relation.RelId /* , trans.sysid, trans.postdate */
HAVING
1=1 --AND (((Client.ClientId)=[Forms]![frmCredit]![Combo112])
AND SUM(
CASE WHEN [TransType] IN ('uc', 'CA', 'RAC', 'UCBR') THEN AMOUNT
ELSE AMOUNT * -1 END
) <> 0
AND Relation.RelId <> '281099'
ORDER BY Relation.Name1, Account.AcctId, Trans.CheckId
我的理由是
Sum(IIf([TransType]="uc",[Amount],IIf([TransType]="CA",[Amount],IIf([TransType]="RAC",
[Amount],IIf([TransType]="UCBR",[Amount],[amount]*-1))))) AS TransAmount,
如果amount
是TransType
其他'uc', 'CA', 'RAC', 'UCBR'
中的任何一个,则只需Amount*-1
,这应与以下内容相同:
SUM(CASE WHEN [TransType] IN ('uc', 'CA', 'RAC', 'UCBR') THEN AMOUNT
ELSE AMOUNT*-1 END) AS TransAmount,
并在having
条款中:
((Sum(IIf([TransType]="uc",[Amount],IIf([TransType]="CA",[Amount],
IIf([TransType]="RAC", [Amount],IIf([TransType]="UCBR",[Amount],[amount]*-1))))))>0
Or (Sum(IIf([TransType]="uc",[Amount],IIf([TransType]="CA",[Amount],
IIf([TransType]="RAC", [Amount],IIf([TransType]="UCBR",[Amount],[amount]*-1))))))<0)
只检查表达式(sum(amount)
)是> 0
还是< 0
,这应该等同于:
SUM(CASE WHEN [TransType] IN ('uc', 'CA', 'RAC', 'UCBR') THEN AMOUNT
ELSE AMOUNT*-1 END) <> 0