How to use an sum() function without group by? - sql

I just have to omit those records whose sum of sales in all 53 weeks is 0 and would need the output without group by

You cannnot really get that in one query.
To get all years without any sum of sales, you have to sum the sales.
That is:
Firstly:
select YEAR(date) from YourTable group by YEAR(date) having sum(sales) > 0
Then:
select * from YourTable where Year in (<firstquery>) as aliasname
order by <anydatecolumn>
If you are using mssql you can do that in one query using the OVER clause and partitioning

Related

How to create query for search total sales previous year

I have table named Sales.OrderValues that contain of 2 column, namely orderyear and val (total sales per day).
This is the record snippet (I cant show all of record because there are 830 rows)
I want to show the result like this
But, my output is different with my expected output.
As you can see, the expected output of prevtotalsales in 2008 is 618085.30. But, my output is 825169.29 (which is 208083.99 + 617085.30).
Below is my query
SELECT
YEAR(D1.orderdate) AS orderyear,
SUM(D1.val) AS curtotalsales,
(
SELECT
SUM(D2.val)
FROM
Sales.OrderValues D2
WHERE
YEAR(D1.orderdate) > YEAR(D2.orderdate)
)
AS prevtotalsales
FROM
Sales.OrderValues D1
GROUP BY
YEAR(D1.orderdate);
How to show the SUM of totalsales at the previous year without adding the next year's totalsales?
Basically, you want an equality condition in the WHERE clause of the subquery. This:
WHERE YEAR(D1.orderdate) > YEAR(D2.orderdate)
Should be:
WHERE YEAR(D1.orderdate) = YEAR(D2.orderdate) + 1
But it is much simpler and more efficient to just use lag():
SELECT
YEAR(orderdate) AS orderyear,
SUM(val) AS curtotalsales,
LAG(SUM(val)) OVER(ORDER BY YEAR(orderdate)) AS prevtotalsales
FROM Sales.OrderValues
GROUP BY YEAR(orderdate)
ORDER BY orderyear
You need to first SUM the values per year, and then use a cumulative SUM:
WITH Totals AS(
SELECT YEAR(OV.orderdate) AS OrderYear
SUM(OV.Val) AS YearSum
FROM Sales.OrderValues OV
GROUP BY YEAR(OV.orderdate))
SELECT OrderYear,
YearSum,
SUM(YearSum) OVER (ORDER BY OrderYear ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS PreviousCumulative
FROM Totals;

How do I count the values in a column by each month?

In my data frame, I am trying to count number of values of a column per month, given by the time column in SQL. I want the output to have a count of the number of values in a column for each month. I know I can use the where function to count for one month and I could do this for all 12 months, but was wondering if there was a more efficient way.
Here's the inefficient example:
SELECT
count(column1) AS Total
FROM DataFrame
WHERE MONTH(date) == 1
GROUP by column1 ORDER by count(column1) DESC LIMIT 10
I am trying to count distinct values of a column per month, given by the time column in SQL. I
You seem to want GROUP BY:
SELECT MONTH(date) as mon, approx_count_distinct(column1, 0.1) AS Total
FROM DataFrame
GROUP by MONTH(date)
ORDER by Total DESC
LIMIT 10;
Note that using MONTH() without YEAR() or a filter on the date is highly suspicious.

How do you use having for multiple conditions?

In the following codes, how do you exclude members's spending that's larger than $500 for each year (instead of total spending for all years)?
select
Year
,month
,memberkey
,sum(spending) as spending
from table1
group by
1,2,3
A HAVING clause won't work here since you really want to aggregate at the YEAR level to determine which records should be included. Traditionally you would do this with a correlated subquery, but in Teradata you can make use of the QUALIFY clause:
SELECT "Year"
,"Month"
,MemberKey
,spending
from table1
QUALIFY sum(spending) OVER (PARTITION BY "Year", MemberID) < 500

How to group by year with the year showing only once

I have tried using the following query
select distinct Year (SaleDate) AS SaleYear,Max(SalePrice)
from Sale
group by SaleDate
The years 2010 and 2014 are showing twice,even though i used distinct and group by. the amounts in Maxprice are different as well. am i doing something wrong here?
You need to repeat year() in the group by:
select Year(SaleDate) AS SaleYear, Max(SalePrice)
from Sale
group by year(SaleDate);
SELECT DISTINCT with GROUP BY is almost never correct. All that your query does is aggregate by SaleDate and in the result set extract the year. That is why you see duplicates.

Getting the sum of values in same column in SQL QUERY

My database is the list of product a company sold in a particular date and hour. What I need to do is to get the amount of products sold in a particular date and hour. I am quite new to SQL so please help.
This is my sql query:
SELECT SELL_DATE, SELL_HOUR, SUM(PRICE), FROM dbo.SOLD_ITEMS WHERE SELL_DATE > '2014-04-20' AND SELL_DATE < '2014-04-26' AND SELL_HOUR = '9'
SELECT SELL_DATE, SELL_HOUR, SUM(PRICE), COUNT(*)
FROM dbo.SOLD_ITEMS
GROUP BY SELL_DATE, SELL_HOUR;
Functions like SUM() are known as aggregate functions because they combine data from a column in some way. When you use an aggregate function, you must also supply information about which values you are aggregating.
You do this with GROUP BY:
SELECT SELL_DATE, SELL_HOUR, SUM(PRICE)
FROM dbo.SOLD_ITEMS
WHERE SELL_DATE > '2014-04-20'
AND SELL_DATE < '2014-04-26'
AND SELL_HOUR = '9'
GROUP BY SELL_DATE, SELL_HOUR