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
Related
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.
Basically, I'm trying to retrieve only 1 record from a table based on catalog_no and packing_list_no. However, the table I'm retrieving the information from has additional details that I don't need but makes the 1 record I need into 3 distinct records.
I tried summing and grouping the info, but I'm still getting 3 records instead of 1.
Any ideas of how to solve this issue?
Your GROUP BY groups your result on the columns quantity picked, quantity shipped and weight shipped. A different value in any of those columns will result into a different row.
You can drop the GROUP BY clause all together, if the desirable result is the packing list and catalog no that you have specified. You can use the GROUP BY clause to columns that you do not use sum to group the result set.
SELECT catalog_no, sum(qty_picked), sum(qty_shipped), sum(weight_shipped), packing_list_no, bay_no, carrier_code, tracking_no FROM oeorder_shipping
WHERE packing_list_no='CP12618525' AND catalog_no='437656500'
GROUP BY bay_no, carrier_code, tracking_no;
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
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
I'm to a query multiple times from a single query in BIRT. For example, my DB2 query could be SELECT * FROM GROUPS and my dataset would look like
id | name
1 | group 1
2 | group 2
From that dataset I'd like to run another query for each row. So maybe something like SELECT * FROM ORDERS WHERE group_id = params['id'] where id is id of the current GROUP record.
The actual report would look something like:
Order for Group 1
01/01/2015 Order #321
01/15/2015 Order #948
Orders for Group 2
01/02/2015 Order #123
01/23/2015 Order #456
I'm fairly new to BIRT and have seen examples of using scripts on certain events (beforeOpen, etc), but I wanted to make sure that was the proper way to go for something this rudimentary.
Grouping on Groups in my example and OP's question, read carefully.
From what I understand of your requirements probably the easiest way to get what you want it to group on the table.
Put your fields in the data set and on the table then 'group by' elements of the table.
The report below is grouped by date and UPMC_Assign, then I count the number of INCIDENT_ID (ticket owner is criteria that is not displayed)
Create your 'Data Set', drop the Data Set on the Layout, a table is auto created.
Add a 'Group' (red circle lower part of second image), in my case I grouped by Date then by Group, in your case you would group by your 'Group'
I added an aggregation from the Palette to get counts. You can delete anything from the table you don't want. In my case i started out with a line for every ticket, but I deleted the entire row, and just show the groups and counts.
See my answer here for suggestions on versioning reports during development.