0

Hi everyone!

I have the following problem I am trying to solve:

I have one table with data and need to access the same column twice, everyone with different criteria and then output the results on one table.

I tried

SELECT e.user, SUM(e.column), SUM(f.column)
FROM table AS e INNER JOIN table AS f
ON e.user = f.user
WHERE  (e.criterion1 and f.criterion2)
GROUP BY e.user

sums are calculated multiple times. What is wrong?


Thank you very much in advance.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by nkarvi
0

Please use code tags when posting queries on daniweb:

[code=sql] ...query here...

[/code]

Next --- I cannot tell what you are trying to do since you have abstracted away the table and column names. You can access the same column twice for multiple calculations so you do not need to join the table to itself unless you're trying to do certain things. Can you please post your table, its' structure, some sample rows, and indicate what your desired output is?

0

Thank you very much for your help.

I googled a bit more on "accessing the same table multiple times" as you wrote and ende up using a case statement in the sum like this:

select sum(case when type = 1 then quantity else 0 end)

where the type is the criterion and the quantity is the ammount I want to add.

It worked ok.

Thanks very much again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.