Get the sum of a column in oracle reports - sql

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

Related

Calculating the mode/median/most frequent observation in categorical variables in SQL impala

I would like to calculate the mode/median or better, most frequent observation of a categorical variable within my query.
E.g, if the variable has the following string values:
dog, dog, dog, cat, cat and I want to get dog since its 3 vs 2.
Is there any function that does that? I tried APPX_MEDIAN() but it only returns the first 10 characters as median and I do not want that.
Also, I would like to get the most frequent observation with respect to date if there is a tie-break.
Thank you!
the most frequent observation is mode and you can calculate it like this.
Single value mode can be calculated like this on a value column. Get the count and pick up row with max count.
select count(*),value from mytable group by value order by 1 desc limit 1
now, in case you have multiple modes, you need to join back to the main table to find all matches.
select orig.value from
(select count(*) c, value v from mytable) orig
join (select count(*) cmode from mytable group by value order by 1 desc limit 1) cmode
ON orig.c= cmode.cmode
This will get all count of values and then match them based on count. Now, if one value of count matches to max count, you will get 1 row, if you have two value counts matches to max count, you will get 2 rows and so on.
Calculation of median is little tricky - and it will give you middle value. And its not most frequent one.

How to Group value stored in Redis using Hash type?

I have used Hash data type to store data as shown in given code snippet:
Field 1
c7b05551-97fa-4492-a949-0e8f3660129f
Value 1
{"Currency":"USD","guid":"c7b05551-97fa-4492-a949-0e8f3660129f","price":"86.143","quantity":"48071"}
Field 2
c9b05851-98fa-4592-a946-0e8f5660129f
Value 2
{"Currency":"USD","guid":"c9b05851-98fa-4592-a946-0e8f5660129f","price":"86.143","quantity":"42"}
I want to group by price, and want sum of quantity.
In mongoose it is possible to do with Aggregation Group and Sum.
Is it possible to do with Redis?
It is impassable. The best way to do this by using sorted set. Make price as scores and store the quantity values in set.By using ZUNIONSTORE you can sum the quantities with same scores(price).

Possible to keep fraction in a query?

I am looking for a way to add up averages in SQL. Here is an example of the data I have:
product avg_price
phone 104.28
car 1000.00
And I'm looking to build something like this:
product avg_price
[all] 544.27
phone 104.28
car 1000.00
The way I'm currently doing it is to store the count and sum in two different columns, such as:
product cnt total
phone 203 20,304.32
car 404 304,323.30
And from that get the average. However, I was wondering if it is possible in SQL to just 'keep the fraction' and be able to add them as needed. For example:
product avg_price
[all] [add the fractions]
phone 20,304.32 / 203
car 304,323.30 / 404
Or do I need to use two columns in order to get an average of multiple aggregated rows?
You don't need 2 columns to get the average, but if you want to display as a fraction then you will need both numbers. They don't need to be in 2 columns though.
select product, sum(total) ||'/'||sum(count)
from table a
join table b on a.product=b.product
union
select product, total ||'/'||count
from table a
join table b on a.product=b.product;

Microsoft Access - Group By Feature

I'm wondering if there is a way to modify the results of one of my queries. Right now, I have a query in which the output shows the number of items in certain type of category by date, by using the Group By feature. So just as an example, if I enter 9/15/13 as the date the output would be as follows:
Apples 1
Bannas 5
Pears 16
Is there a way for it to just show Apples and total all other items into one category so it would output:
Apples 1
All Others 21
Thanks in advance for all of your help!
A bit difficult without table names or column names, but something along the lines of this (you'll have to enter it in SQL view)
Select
IIf([Product] = 'Apples', 'Apples', 'All Others'),
Count(*)
From
[Inventory]
Where
[InventoryDate] = [Enter Date]
Group By
IIf([Product] = 'Apples', 'Apples', 'All Others')
You need to replace Inventory with your table name, and Product with the column that contains the category and InventoryDate with the column that has the date. Count(*) might be sum([Quantity]), it depends on your structure.

ms sql Query for POS item sales

I have a SQL question regarding POS system item sales data. In the database, there is a "Totals" table. In that table, contains various records identified by the "TtlType" field. I'm working with 2 types. Values 11, which is the Item POS Sales, and 12, which is the Item Cost Sales.
When I do my SQL Query, i'm doing something along the lines of
SELECT ItemNumber,
SUM(ItemCount),
SUM(ItemAmount)
FROM Totals
WHERE TtlType = 11
AND Date = 8/1/2012
etc.
I'd like to also include TtlType 12. How can I merge all of the data into one row?
If I change my WHERE to include TtlType 11 & 12, I get 2 seperate rows for each menu item. The first one being TtlType 11 which is the Sell Price, and the next being 12 which is the cost. BOTH TtlType use the SAME field names to record information (ItemCount, ItemAmount)
I hope i explained this well enough. When I use the GROUP BY, I still get the same results.
I'm not sure I understand your data structure fully, or the significance of ItemNumber.
However, you don't need a GROUP BY to do SUM as you already know, but including ItemNumber in your SELECT & GROUP BY will definitely cause you to have duplicate rows as it will be grouped on that column.
You shouldn't have any issues with the two SUM functions with just
SELECT SUM(ItemCount), SUM(ItemAmount) FROM Totals WHERE TtlType IN (11,12)
AND Date = 8/1/2012