I need to calculate the average price per brand of the table below:
SELECT * FROM VENDOR;
Expected result:
I did some tests but they do not calculate and group the AVG per brand,
For example:
CREATE VIEW AVERAGE_PRICE AS
SELECT LOWER (BRAND) as BRAND, AVG (PRICE) as PRICE
FROM VENDOR
GROUP BY BRAND;
Any ideas?
Should be
group by lower(brand)
Related
In a very standard question of selecting all item names and their prices for those prices greater than the average price.
The query using having has the form:
'''SELECT Name, Price FROM items
GROUP BY Name
HAVING Price >
(SELECT AVG(Price) FROM items )
;'''
It seems that only when AVG is used in a SELECT statement then it is returning the column average. Else if I use HAVING Price > AVG(Price) nothing is returned. Why is an additional SELECT statement required, instead of just using AVG on Price?
That's just how GROUP BY works. In the SELECT part of the query, you can only have the columns that are used in the GROUP BY clause, or the aggregates, like MIN, MAX, AVG, SUM, etc. In this case, what's your aggregate of the column Price that you want to output? you should specify that.
Alternatively, you could use a windowed function for calculating the average price for each name unit, and then add the subquery to filter on that:
SELECT *
FROM (SELECT Name, Price, AVG(Price) OVER (PARTITION BY Name) AS Average_Price
FROM items) AS X
WHERE X.Price > X.Average_Price
I think your best option is a simple subquery in WHERE condition
select i.*
from items i
where Price > (
select avg(Price) avgPrice
from items ii
--where ii.Name = i.Name /* add this line if you need average to be calculated per Name */
)
I need to add some values with a condition in GoogleBigQuery
NOTICE: I edited the original question since it was not accurate enough.
Thanks to the two participants who have tried to help me.
I tried to apply the solutions kindly suggested by you but I got the same result from the pct column as a result.
Something like this:
results
Here is the more detailed definition:
TABLE
Columns:
Shop: Shop location
brand: Brands of cars sold at shoplocation
sales: sales of each brand sold at each shop_location
rank: Rank of each brand per shop location (the biggest the greater)
total_sales_shop: SUM of all brand sales per shop location
pct: percentage of sales by brand in relationship with shop location
pct_acc:
What i need to calc is pct_acc which is the cumulative sum of the shops by rank (while it has no relation with brand)
PCT_ACC
My need is to reach something like PCT_ACC, and then save the results in another one like this:endtable
You can use following query to get the required data:
select values, rank,
sum(case when rank<2 then values else 0 end) condition1
from table
group by values, rank
Need to add/remove columns from select and group by as per requirement
To get the cumulative sum you can use following query:
select shop, brand, sales, rank, total_sales_shop, pct ,
sum(pct) over (partition by shop order by rank) as pct_act
from data
And to get the final table you can use combination of case statement and group by
e.g
select shop,
max(case when rank=1 then pct_act end) as rank_1,
max(case when rank=2 then pct_act end) as rank_2,
max(case when rank=3 then pct_act end) as rank_3,
max(case when rank=4 then pct_act end) as rank_4,
max(case when rank=5 then pct_act end) as rank_5
from cumulative_sum
group by shop
If you want only the final sum for the rows with that condition you can try:
SELECT
SUM (IF(Rank < 2, Values, 0) AS condition1
FROM table
If you want to get the rank and the sum only for the rows with that condition you can try doing
SELECT
Rank,
SUM (IF(Rank < 2, Values, 0) AS condition1
FROM table
WHERE
RANK < 2
GROUP BY
Rank
Finally, if you want to get the rank and the sum considering all the rows you can try doing:
SELECT
Rank,
SUM (IF(Rank < 2, Values, 0) AS condition1
FROM table
GROUP BY
Rank
I hope it helps
How can I get the total price of each invoice from a table that has multiple invoices? For example,
you can use sum and group by
eg if you need sum for each inv_number you can use the sum of the product between line_units and line_price eg:
select inv_number, sum(line_units*line_price) as total
from my_table
group by inv_number
and if your my_table is a joi result you can
select t.inv_number, sum(t.line_units*t.line_price) as total
from (
your select ...........
) t
group by t.inv_number
SELECT
CUS_FNAME
,CUS_FNAME
,SUM(LINE_UNITS * LINE_PRICE) TOTALPRICE
FROM
dbo.Invoices
GROUP BY
CUS_FNAME,CUS_LNAME
Pretty simple, just used group by. Hope this helps!
I have a table made up of dates and sales totals for the particular date. I would like to be able to query the table and select the following: max sales, the date associated with the max sale figure, sum of all sales, and the minimum date in the table. One additional complication is that there are duplicate max values. I don't care which max value is chosen but I just want one at random. This is for Oracle.
Here is what I tried. It was using a sub query.
Select sales, date, min(date), sum(sales) from table
Where sales = (select distinct(max(sales)) from table)
select
max(sales),
max(date_) keep (dense_rank first order by sales desc),
sum(sales),
min(date_)
from
table_
See also This SQL Fiddle
I have category, Sub-category and Revenue.
Category Sub-Category Revenue Percentage
---------------------------------------------------------
Books Text Books 5000 (5000/14000)*100
Comics Books 6000
Horror Books 3000
Now my question is, how to achieve like this using SQL?
I need percentage wrt to total sales for that category.
Oracle has RATIO_TO_REPORT analytic function, which computes the ratio of a value to the sum of a set of values.
select category, subcategory, revenue,
round((ratio_to_report(revenue) over (partition by category) * 100),2) as percentage
from mytable;
Output:
CATEGORY SUBCATEGORY REVENUE PERCENTAGE
-----------------------------------------
books text 5000 35.71
books horror 3000 21.43
books comics 6000 42.86
Sample fiddle here
You can use an analytical SUM function:
select
Category,
SubCategory,
Revenue,
Revenue * 100 / sum(Revenue) over (partition by Category) as Percentage
from
yourTable
;
You can use a subselect to get the overall revenue and use it in the calculations.
SELECT category, sub-category, revenue,
((revenue /
SELECT sum(inne.revenue)
FROM table inne
WHERE inne.category = oute.category) * 100) AS percentage
FROM table oute