Sum Expression Aggregate Error - sql

I am rewriting this question because Gordon Linoff told me it might come off as rude if I edited my other one -- so this isn't a duplicate, just a correction.
I am trying to write a code that will sum the prices of all the orders that come up when I fill out the query. For example, if I enter the ID range 1-60, I want there to be a sum column created that then sums up all the prices of ID's 1-60.
I thought it would be simple enough to just create a SUM(.....) AS Exp 1, but it tells me that there is a problem with the ID and aggregate function.
I want to be able to see the individual prices, as well as a new column with the sum of all these prices. I plan on adding some more columns of data into the table later on.
My current code looks like this:
SELECT table.ID, table.Price, SUM(table.Price) AS Exp 1
FROM table
WHERE table.ID BETWEEN StartID AND EndID
Thank you for any help

You have a concept error with your aggregate statement. When you run this query, the WHERE clause will evaluate first to exclude all IDs that are not between your user specified start and end points. Then, you missed the GROUP BY clause to tell it what needs to be grouped. Eliminate the table.Price field, otherwise you will be getting unique records for each price which is not what you want.
SELECT t.ID, SUM(t.Price) AS Price_Summary
FROM table t
WHERE t.ID BETWEEN StartID AND EndID
GROUP BY t.ID
Also, aliases will help improve readability.
EDIT
I think this might be what you are trying to get to, but I'm still unclear.
SELECT t.ID, SUM(t.Price) AS Price_Summary,
(SELECT SUM(t2.Price) FROM table t2 WHERE t2.ID BETWEEN StartID AND EndID) AS Total_Price
FROM table t
WHERE t.ID BETWEEN StartID AND EndID
GROUP BY t.ID
This will give you a result set with all of the IDs, a sum of the price of everything with that ID, and then a total sum. So it would look something like this:
ID | Price_Summary | Total_Price
1 10 60
2 30 60
3 20 60

Related

How can I get multiplication between 2 different table columns with group by clause?

I tried this whole day but didn't got the result. Please go through the images on the link below
Here is my table 1 structure
and here is my table 2 structure
I want to multiply product_uom_qty from 1st table with cost in 2nd table and then group them by product_id which is there is both tables.
This is my query.
select sum(sale_order_line.product_uom_qty) * product_price_history.cost
from sale_order_line, product_price_history
group by product_id;
And result I am getting is
ERROR: column reference "product_id" is ambiguous. LINE 1: ...m
sale_order_line, product_price_history group by product_id...
please tell me where I am making mistake.
Presumably, you want something like this:
select product_id, sum(sol.product_uom_qty * pph.cost)
from sale_order_line sol join
product_price_history pph
using (product_id)
group by product_id;
That is, you need to join the tables on some condition.
Without sample data and desired results, it is unclear what you really want. But this at least is a sensible query that removes the syntax errors.

Sum Expression Access Query for Completed Fields

I am trying to write a code that will sum the prices of all the orders that come up when I fill out the query. For example, if I enter the ID range 1-60, I want there to be a sum column created that then sums up all the prices of ID's 1-60.
I thought it would be simple enough to just create a SUM(.....) AS Exp 1, but it tells me that there is a problem with the ID and aggregate function.
My current code looks like this:
SELECT table.ID, table.Price, SUM(table.Price) AS Exp 1
FROM table
WHERE table.ID BETWEEN StartID AND EndID
Thank you for any help
EDIT: I should've specified this earlier, but I want to be able to see the individual prices, as well as a new column with the sum of all these prices. I plan on adding some more columns of data into the table later on.
If you want the sum over all the rows, then only include that in the SELECT:
SELECT SUM(table.Price) AS Exp1
FROM table
WHERE table.ID BETWEEN StartID AND EndID

SQL SUM function with added

total novice here with SQL SUM function question. So, SUM function itself works as I expected it to:
select ID, sum(amount)
from table1
group by ID
There are several records for each ID and my goal is to summarize each ID on one row where the next column would give me the summarized amount of column AMOUNT.
This works fine, however I also need to filter out based on certain criteria in the summarized amount field. I.e. only look for results where the summarized amount is either bigger, smaller or between certain number.
This is the part I'm struggling with, as I can't seem to use column AMOUNT, as this messes up summarizing results.
Column name for summarized results is shown as "00002", however using this in the between or > / < clause does not work either. Tried this:
select ID, sum(amount)
from table1
where 00002 > 1000
group by ID
No error message, just blank result, however plenty of summarized results with values over 1000.
Unfortunately not sure on the engine the database runs on, however it should be some IBM-based product.
The WHERE clause will filter individual rows that don't match the condition before aggregating them.
If you want to do post aggregation filtering you need to use the HAVING Clause.
HAVING will apply the filter to the results after being grouped.
select ID, sum(amount)
from table1
group by ID
having sum(amount) > 1000

Join query in Access 2013

Currently have a single table with large amount of data in access, due to the size I couldn't easily work with it in Excel any more.
I'm partially there on a query to pull data from this table.
7 Column table
One column GL_GL_NUM contains a transaction number. ~ 75% of these numbers are pairs. I'm trying to pull the records (all columns information) for each unique transaction number in this column.
I have put together some code from googling that hypothetically should work but I think I'm missing something on the syntax or simply asking access to do what it cannot.
See below:
SELECT SOURCE_FUND, GLType, Contract, Status, Debit, Credit, GL_GL_NUM
FROM Suspense
JOIN (
SELECT TC_TXN_NUM TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM HAVING COUNT(GL_GL_NUM) > 1 ) SUB ON GL_GL_NUM = GL_NUM
Hey Beth is this the suggested code? It says there is a syntax error in the FROM clause. Thanks.
SELECT * from SuspenseGL
JOIN (
SELECT TC_TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM
HAVING COUNT(GL_GL_NUM) > 1
Do you want detailed results (all rows and columns) or aggregate results, with one row per tx number?
If you want an aggregate result, like the count of distinct transaction numbers, then you need to apply one or more aggregate functions to any other columns you include.
If you run
SELECT TC_TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM
HAVING COUNT(GL_GL_NUM) > 1
you'll get one row for each distinct txn, but if you then join those results back with your original table, you'll have the same number of rows as if you didn't join them with distinct txns at all.
Is there a column you don't want included in your results? If not, then the only query you need to work with is
select * from suspense
Considering your column names, what you may want is:
SELECT SOURCE_FUND, GLType, Contract, Status, sum(Debit) as sum_debit,
sum(Credit) as sum_credit, count(*) as txCount
FROM Suspense
group by
SOURCE_FUND, GLType, Contract, Status
based on your comments, if you can't work with aggregate results, you need to work with them all:
Select * from suspense
What's not working? It doesn't matter if 75% of the txns are duplicates, you need to send out every column in every row.
OK, let's say
Select * from suspense
returns 8 rows, and
select GL_GL_NUM from suspense group by GL_GL_NUM
returns 5 rows, because 3 of them have duplicate GL_GL_NUMs and 2 of them don't.
How many rows do you want in your result set? if you want less than 8 rows back, you need to perform some sort of aggregate function on each column you want returned.
You could do something like the following:
SELECT S.* FROM
SUSPENSE AS S
INNER JOIN (SELECT DISTINCT GL_GL_NUM, MIN(ID) AS ID FROM SUSPENSE
GROUP BY GL_GL_NUM) AS S2
ON S.ID = S2.ID
AND S.GL_GL_NUM = S2.GL_GL_NUM
Which would return a single row for a unique gl_gl_num. However if the other rows have different data it will not be shown. You would have to either aggregate that data up using SUM(Credit), SUM(Debit) and then GROUP BY the gl_gl_num.
I have attached a SQL Fiddle to demonstrate my results and make this clearer.
http://sqlfiddle.com/#!3/8284f/2

Returning the sum of items depending on which type it is

I have one field that I need to sum lets say named items
However that field can be part of group a or b
In the end I need to have all of the items summed for group a and group b
when I say grouped I mean there is a LEFT OUTER JOIN to another table the previous table has a type for the items and the one being joined has a group assigned for this item type
Sorry guys Im a little new to sql I am going to try out what you have given me an get back to you
Ok I feel like we are getting close just not yet allain's I can get them to separate but the issue I need to have both groups to sum on the same row which is difficult because I also have several LEFT OUTER JOIN's involved
Tyler's looks like it might work too so I am trying to hash that out real fast
Alain's seems to be the way to go but I have to tweek it a little more
Maybe I'm not understanding the complexity of what you're asking but... shouldn't this do?
SELECT groupname, SUM(value)
FROM items
WHERE groupname IN ('a', 'b')
GROUP BY groupname
And if you don't care which of a or b the item belongs to then this will do:
SELECT SUM(value)
FROM items
WHERE groupname IN ('a', 'b')
You want something like
SELECT column,SUM( column ) FROM table GROUP BY column
Is that (Tyler's answer) what the question meant, or is it simply this;
SELECT sum(item), groupingField FROM someTable GROUP BY groupingField
or even:
SELECT count(*), item FROM someTable GROUP BY item
which will produce results like this:
sum(item) | groupingField
-------------+-----------------------
71 | A
82 | B
Questioner, perhaps you could clarify which you meant or if I'm oversimplifying?
Try this:
SELECT B.[Group], COUNT(*) AS GroupCount
FROM Table1 A
LEFT JOIN Table2 B ON B.ItemType=A.ItemType
GROUP BY B.[Group]
Please refer this Image (1)
SELECT category_id,SUM(amount) AS count FROM expense GROUP BY category_id;