Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone

I am busy converting access queries for sql server but i dont understand why this one wont work..
SQL
SELECT
	(T1.Debit) As Debit,
	(T1.Credit) As Credit,
	(T1.Debit - T1.Credit) As Balance,
	T1.CustCode
FROM
	(
SELECT
	SUM(AverageDebit) As Debit,
	SUM(AverageCredit) As Credit,
	CustCode
FROM
	(
SELECT
	(IIF(TransactionType IN (0,
	6,
	9,
	10,
	11,
	14,
	16,
	17,
	18,
	19,
	21),
	ABS(Price1) ,
	0)) AS AverageDebit,
	(IIF(TransactionType IN (1,
	2,
	3,
	7,
	15,
	22),
	ABS(Price1),
	0)) AS AverageCredit,
	CustCode,
	TransactionType
FROM
	TransactionHeader
WHERE
	CustCode = 'AA0001'
GROUP BY
	CustCode,
	TransactionType,
	Price1,
	ID)
GROUP BY
	CustCode) As T1



Any suggestions what is wrong with this query ?

Thanks

What I have tried:

Removing Group by,

tried a few other modifications ,

I tried the following and it works , dont know if its the same as the access one tho

SQL
SELECT
	(T1.Debit) As Debit,
	(T1.Credit) As Credit,
	(T1.Debit - T1.Credit) As Balance,
	T1.CustCode
FROM
	(
SELECT
	SUM(AverageDebit) As Debit,
	SUM(AverageCredit) As Credit,
	CustCode
FROM
	(
SELECT
	(IIF(TransactionType IN (0,
	6,
	9,
	10,
	11,
	14,
	16,
	17,
	18,
	19,
	21),
	ABS(Price1) ,
	0)) AS AverageDebit,
	(IIF(TransactionType IN (1,
	2,
	3,
	7,
	15,
	22),
	ABS(Price1),
	0)) AS AverageCredit,
	CustCode,
	TransactionType
FROM
	TransactionHeader
WHERE
	CustCode = 'AA0001'
GROUP BY
	CustCode,TransactionType,Price1
	)AS TS2 GROUP BY TS2.CustCode ) As T1
Posted
Updated 14-Feb-18 10:03am
v2
Comments
David_Wimbley 14-Feb-18 11:26am    
What is the exact error you get? Is it something like (this is sql server) Column '' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Also, if this is a one time query or if you are able to, it looks like you've got 2 deep sub table queries which also may be part of the problem (you didn't post schema and I didn't attempt to recreate) so if you are able, you might consider using a temp table to load data into and massage it there rather than create a huge query. That may make it easier to work with but I don't know anything about your project.

1 solution

Well... I'm not sure you can use IIF together with IN.

You have to use SWITCH[^]
SQL
ABS(Switch([TransactionType]=0,[Price1],[TransactionType]=6, [Price1], ...and so on...))

or IIF[^], but in different way:
SQL
ABS(IIF([TransactionType]=0 OR [TransactionType]=6 OR ..., [Price1], 0))


Finally, i'll simplify your query this way:
SQL
SELECT Src.CustCode, Src.Debit, Src.Credit, Src.Debit - Src.Credit AS Balance
FROM (
    SELECT CustCode, SUM(ABS(IIF([TransactionType]=0 OR [TransactionType]=6 OR ..., [Price1], 0))) AS Debit, 
        SUM(ABS(IIF([TransactionType]=1 OR [TransactionType]=2 OR ..., [Price1], 0))) AS Credit
    FROM TransactionHeader
    WHERE CustCode = "AA0001"
    GROUP BY CustCode
) AS Src;


Try!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900