Weighted-Avg Access Query - sql

Background: I have the following table with clothing brand names, the count of the brand name sold, the revenue the brand name has brought in, and an avg per unit sold of that brand name.
Quantity Brand Revenue Rev_Per_Brand
1820 CMD $13,519.50 $7.43
791 ATL $8,997.00 $11.37
335 WHBM $4,988.00 $14.89
320 CH $4,593.50 $14.35
233 AT $3,207.50 $13.77
Objective: Calculate the weighted average revenue by quantity sold of brands.
What I Have Now:
SELECT
COUNT(*) AS [Quantity],
Sales.Description AS Brand,
FORMAT(SUM(Sales.Amt),"Currency") AS Revenue,
Format(SUM(Sales.Amt)/COUNT(*), "Currency") AS Rev_Per_Brand,
SUM(Sales.Amt)*(COUNT(*)/SUM(COUNT(*))) AS [wAvg_Rev_Per_Brand]
FROM Sales
WHERE Sales.Date > DateAdd("m",-1,NOW())
AND "This query lists brands that have sold in the past 1 month from today's date and returns the revenue received from them" <> ""
GROUP BY Sales.Description
ORDER BY COUNT(*) DESC;
Problem: I am receiving the cannot have aggregate function in expression error, and I am guessing it is probably in the SUM(COUNT(*)) part above.
I am simply trying to do the count of the specific brand over the total count (sum) of all brands sold. Can anyone tell me what I am doing wrong?
Thank you and I really appreciate any help in advance.

You can not double aggregate i.e SUM(COUNT(*)) you've got to have that count in a separate subquery,
change your query to:
SELECT
COUNT(*) AS [Quantity],
Sales.Description AS Brand,
FORMAT(SUM(Sales.Amt),"Currency") AS Revenue,
Format(SUM(Sales.Amt)/COUNT(*), "Currency") AS Rev_Per_Brand,
SUM(Sales.Amt)*(COUNT(*)/(SELECT Count(*) FROM sales)) AS [wAvg_Rev_Per_Brand]
FROM Sales
WHERE Sales.Date > DateAdd("m",-1,NOW())
AND "This query lists brands that have sold in the past 1 month from today's date and returns the revenue received from them" <> ""
GROUP BY Sales.Description
ORDER BY COUNT(*) DESC;

Related

how to calculate percentage of sales and return in sql

i have 10 columns in a table as below:
(transaction id, customer id, transaction date, product subcategory,
product category, quantity, rate, tax, total amount and store type.)
where returns are in negative value of (quantity,rate, tax and total amount) i have to calculate
what is percentage of sales and returns by product sub category?
This sounds like e.g.:
SELECT productsubcategory, SUM(CASE WHEN quantity < 0 THEN 1 ELSE 0)/COUNT(*)
FROM t
GROUP BY productsubcategory
That will give a ratio of the returns to all transactions. If you want it on a per-item basis rather than a per-transaction basis it would be more like:
SELECT productsubcategory, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0)/SUM(ABS(quantity))
FROM t
GROUP BY productsubcategory
But be careful how you treat quantities; i've guess that "sell 20 biscuits, return 15 biscuits" is a total of 35 product movements, 15 of which are returns. I don't know if you want a conclusion of "15/20ths (75%) of biscuits were returned" in which case make the returns a percentage of the sales, not a percentage of the sales+returns

SSRS set year variables per record

I need help to create a code to have it display years specified.
User defines years to report: Year1 Year2 Year3
ProductID Year1 data
Year2 data
Year3 data
where even if year 2 has no data like below
ProductID Year1 data
Year2
Year3 data
My current code will show all data that fit the criteria, but will not show Year2 due to no data.
My current code is
select
sales.ProductID, Sales.Trans_Year, Sum(salesQTY) as QTY
from
(select ProductID, Year(Trans_dt) as Trans_Year, salesQTY
from salestable) sales
group by
sales.ProductID, Sales.Trans_Year
My code is inefficient but I did the select twice so that it would group the year on transdate, if any new to make it better would much appreciated.
Hoping to get help to have the following result.
ProductID Year1 QTY
ProductID Year2
ProductID Year3 QTY
Thank you
You can select the years from your table and cross join them to the products to get a matrix of all years and products. The CROSS JOIN will make a record for each year for each product - regardless if the product was sold in that year.
Then LEFT JOIN your sales total. Using a LEFT JOIN will allow the year and product ID from the SALES_YEAR and SALES subqueries to show even if there's none of a product sold in that year.
SELECT PRODUCTS.ProductID, YEARS.SALES_YEAR, SALES.TOTAL_SALES
FROM (
SELECT ProductID
FROM salestable
GROUP BY ProductID
) AS PRODUCTS
CROSS JOIN (
SELECT YEAR(Trans_dt) AS SALES_YEAR
FROM salestable
WHERE YEAR(Trans_dt) IN #YEARS
) AS YEARS
LEFT JOIN (
SELECT ProductID, Year(Trans_dt) as Trans_Year, SUM(salesQTY) AS TOTAL_SALES
FROM salestable
GROUP BY Year(Trans_dt)
) SALES ON SALES.Trans_Year = YEARS.SALES_YEAR AND SALES.ProductID = PRODUCTS.ProductID
Note that this assumes that you sold something every year selected. If the user enter 1905 for the year and there's no data for 1905, that year wouldn't appear. That shouldn't really be an issue though.

simple sql query highest sales

There are 2 tables - Products and Sales
Products
prod_id
prod_nm
Sales
prod_id
cust_id
sls_dt
sls_amt
Write a query selecting ALL the products. For each product show total of sales amounts in the past 30 days or 0 if not sold in 30 day withoug using subqueries.
Since different RDBMS have different date functions, you can filter by date using the following pseudo code - sls_dt > now() - 30.
Im new to sql and im trying it like this as i found this online.
Select prod_id, prod_nm from(
Select sls_amt
From Sales) as t
Where t.rank = 1
However, this isnt' working. Any help is appreciated
Try below:
select p.prod_id,
p.prod_nm,
sum(s.sls_amt)
from products p
left outer join Sales s on p.prod_id = s.prod_id
and s.sls_dt > now() - 30
group by p.prod_id,
p.prod_nm;

SQL Server 2008 R2- Query to get total sales and qty sold by month

I have 2 tables STOCK and ITEMS,
I am trying to get a report showing how total items purchased (ITEMS table- items.quanto), total sales for items(ITEMS table- I assume (items.quanto*items.it_unlist) with description(STOCK table- stock.desc1) by month (ITEMS table-items.odr_date) with classification (which is a field in the STOCK table- stock.assoc).
The owner wants this info for each month for 2011, 2012, and YTD 2013
so total qty sold for item with total sales of that item by month 2011, 2012, 2013 YTD
This is my query
select items.item, items.quanto, SUM(items.QUANTO*items.it_unlist) as [total cost], stock.desc1, items.it_sdate from items
inner join
stock
on
stock.number = items.item
where IT_SDATE between '2011-01-01 00:00:00.0' and '2011-12-31 00:00:00.0'
group by items.item, items.quanto, stock.desc1, items.it_sdate
order by IT_SDATE
I was hoping to get each item totaled sales and qty by month but this is what I figured out on my own…lol
any help is appreciated.
select
item,
right(convert(varchar(8), it_sdate, 3), 5) theMonth,
sum(quanto) totalQuantity,
sum(quanto * it_unlist) totalSales,
max(stock.desc1) descr,
max(stock.assoc) assoc
from
items inner join stock on
items.item = stock.number
where
it_sdate > '2011-01-01'
group by
item,
right(convert(varchar(8), it_sdate, 3), 5)
order by
item,
theMonth
http://sqlfiddle.com/#!3/246d3/18/0
if the item column of the items table is primary key, then you can’t display the each and every item number when you are trying to display the total sales and qty.
And also, the IT_DATE column consists of only one date per month in the table, then only the below query is possible, if not we need to write other query.
if not, you can select.
select i.quanto, sum(QUANTO * it_unlist) as [total list],
s.desc1, i.it_sdate from items i inner join stock s on (s.number = i.item)
where IT_SDATE > 2011-01-01
group by i.it_sdate order by IT_SDATE;

Share of invoiced in total (sum group by)

I have sales lines some of them are invoiced, some not. I would like to add information of invoiced lines share per each order (salesid). I want this information for each sales line to be the same within one order. Invoiced line has sales status =3. Here's my try:
SELECT
salesid,linenum,itemid,salesstatus,name,lineamount
,(SELECT SUM(CASE WHEN salesstatus=3 THEN lineamount ELSE 0 END) as expr1) invoiced
,(SELECT SUM (lineamount) as expr2 GROUP BY salesid) total
,invoiced/total
FROM T.SALESLINES
GROUP BY
salesid,linenum,itemid,salesstatus,name,lineamount
Grouping by salesid does not work giving error "Each GROUP BY expression must contain at least one column that is not an outer reference."
I guess the code seems logical but does not work. So how should I group by salesid to divide invoiced by total?
try
Select *,invoiced/total as ColName from (
SELECT
salesid,linenum,itemid,salesstatus,name,lineamount
,(SELECT SUM(CASE WHEN salesstatus=3 THEN lineamount ELSE 0 END From T.SALESLINES) as invoiced
,(SELECT SUM (lineamount)From T.SALESLINES GROUP BY salesid) as total
FROM T.SALESLINES) source
GROUP BY
salesid,linenum,itemid,salesstatus,name,lineamount,invoiced,total