Issue to build sql query to filter data from one table - sql

I am new to SQL and I have the following table defined in Oracle:
Basically I need to create a store procedure that does the following things:
Display the code,amount, currency and occurrence fields.
However the identical code that have the same currency add the amount and show them in one row and their occurrence, for instance from that data of the above table the result should be:
code: TS-4.1 ,Amount: 150 ,Currency : USD, Occurrence: 2
code: TS-4.1 ,Amount: 25 ,Currency : EU , Occurrence: 1
code: TS-2.1 ,Amount: 225 ,Currency : USD, Occurrence: 1
...other result
Any idea how this can be achieved please?

SELECT CODE, SUM(AMOUNT), CURRENCY, COUNT(*) as Occurrence
FROM <your_table>
GROUP BY CODE, CURRENCY
You can use a GROUP BY.
This would group your entries per code and amount and will display the number of rows that have the same code, amount and currency.

Related

Single Article - Multiple Invoice

I have a need to create a stock report, where I have article number in one stock table and Invoice number under which they were sold in another table.
Stock Table:
Select * from StockTable
result:
artno opening_Stock stock_received
30271472 1 50
Invoice Table:
Select * from InvoiceTable
result:
itemno invoicenumber QTYSold invoicedate
30271472 Inv_123 10 2018-10-06T00:00:00
30271472 Inv_234 20 2018-10-06T00:00:00
30271472 Inv_345 10 2018-10-06T00:00:00
30271472 Inv_567 10 2018-10-06T00:00:00
The Problem is that in Stock Table StockReceived is 50. Now this 50 Quantity can be sold to difference customers in multiple invoices.
My Objective is to show data in most presentation way and then query for that: a) Some option I can think of is to show all invoice numbers in comma saperated, Using XML Path or COALESCE..
b) Second option is to join two table and for each invoice number generate a new row, but in this case Opening_Stoc and Stock Received value will also repeat for each row.
c) Third is to generate dynamic columns for each invoice. Not even sure how to achive this..
Really confused, can somebody help me suggested the best possible way to present this to business and query to achive the same
Regards
Vipin

SQL : how to distinguish between different rows with same value in some field and have a separate function applied to another field

I have a query output showing a list of orders. Some orders might occupy more then one record in the query output if those orders consist of sub-orders.Each sub-order occupies a separate line in the output. There is the OrderID column which has the same value for all sub-orders in the output:
OrderID Sub-Order Price
1 1 100
1 2 50
2 1 30
3 1 50
I need to add a column "Discount" to the output and fill it by following rules:
If certain order has one sub-order - the discount is 10% of the Price
If certain order has more than one sub-order, the discount is 20% on all sub-orders'
My query is a UNION of two SELECTs.
I use mssql with ms sql studio
Use CASE and COUNT window function
SELECT OrderID, Sub-Order, Price,
CASE WHEN (count(*) OVER (PARTITION BY OrderID)) > 1
THEN Price * 0.8
ELSE Price * 0.9
END
FROM ( table or <query> )

Get the sum of a column in oracle reports

I am trying to get the sum of a column using oracle reports, but with a condition. For example I have three columns, a store column, a fruit column and a cost column. I want to get the sum cost of all the "bananas", or whichever fruit you pick, bought in a particular store.
Ex:
store1------------banana----------------$5.00
store1------------apple-----------------$2.00
store1------------banana----------------$3.50
store 1 bananas = $8.50 <- this is what I want
store 1 sum = $10.50
store2------------apples----------------$1.50
etc
I've tried making a formula in the data model, but then I'd have to supply it with the store name.
You can use the SUM function, wich is a ANSI SQL function. You also need to use group by:
select store_name, fruit_name, sum(cost)as Total_Cost
from your_table
group by store_name, fruit_name

Counting Results in SQL

I'm having trouble using COUNT in SQL...The following query returns two rows, but then returns the raps column as 137. So I believe it's counting the total number of operation_id columns in the dataset instead of from the results.
Is there any way to make it count only the columns from the results, so that raps returns as 1 in each of the columns? I would then use PHP to add them together.
//Query
SELECT DISTINCT hrap_id,
operation_id,
COUNT (operation_id) AS raps,
operation_type
FROM view_rappels
WHERE year = '2013' AND crew_id = '4'
GROUP BY hrap_id, operation_type, operation_id
//Results
10.00 702020000.00 137.00 operational
1.00 702020000.00 137.00 operational
You need to put DISTINCT inside of the count function like so
COUNT(DISTINCT operation_id) AS raps

Adding a percent column to MS Access Query

I'm trying to add a column which calculates percentages of different products in MS Access Query. Basically, this is the structure of the query that I'm trying to reach:
Product |
Total |
Percentage
Prod1 |
15 |
21.13%
Prod2 |
23 |
32.39%
Prod3 |
33 |
46.48%
Product |
71 |
100%
The formula for finding the percent I use is: ([Total Q of a Product]/[Totals of all Products])*100, but when I try to use the expression builder (since my SQL skills are basic) in MS Access to calculate it..
= [CountOfProcuts] / Sum([CountOfProducts])
..I receive an error message "Cannot have aggregate function in GROUP BY clause.. (and the expression goes here)". I also tried the option with two queries: one that calculates only the totals and another that use the first one to calculate the percentages, but the result was the same.
I'll be grateful if someone can help me with this.
You can get all but the last row of your desired output with this query.
SELECT
y.Product,
y.Total,
Format((y.Total/sub.SumOfTotal),'#.##%') AS Percentage
FROM
YourTable AS y,
(
SELECT Sum(Total) AS SumOfTotal
FROM YourTable
) AS sub;
Since that query does not include a JOIN or WHERE condition, it returns a cross join between the table and the single row of the subquery.
If you need the last row from your question example, you can UNION the query with another which returns the fabricated row you want. In this example, I used a custom Dual table which is designed to always contain one and only one row. But you could substitute another table or query which returns a single row.
SELECT
y.Product,
y.Total,
Format((y.Total/sub.SumOfTotal),'#.##%') AS Percentage
FROM
YourTable AS y,
(
SELECT Sum(Total) AS SumOfTotal
FROM YourTable
) AS sub
UNION ALL
SELECT
'Product',
DSum('Total', 'YourTable'),
'100%'
FROM Dual;