Getting the sum of values in same column in SQL QUERY - sql

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

Related

Using Subquery in Sequence function PrestoSQL

Use case -
I am trying to find weekly frequency of a customer from a dataset. Now, not all customers have "events" happening in all of the weeks, and I would need to fill them in with zero values for the "count" column.
I was trying to do this using the sequence function of PrestoSQL. However, this would need me to get the value of max week from the customer's orders itself ( I don't want to hardcode this since the result would be going into a BI tool and I dont want to update this manually every week )
with all_orders_2020 as (select customer, cast(date_parse(orderdate, '%Y-%m-%d') as date) as order_date
from orders
where orderdate > '2020-01-01' and customer in (select customer from some_customers)),
orders_with_week_number as (select *, week(order_date) as week_number from all_orders_2020),
weekly_count as (select customer, week_number, count(*) as ride_count from orders_with_week_number
where customer = {{some_customer}} group by customer, week_number)
SELECT
week_number
FROM
(VALUES
(SEQUENCE(1,(select max(week_number) from weekly_count)))
) AS t1(week_array)
CROSS JOIN
UNNEST(week_array) AS t2(week_number)
Presto complaints about this saying -
Unexpected subquery expression in logical plan: (SELECT "max"(week_number)
FROM
weekly_count
)
Any clues how this can be done ?
Had a similar use case and followed the example from here: https://docs.aws.amazon.com/athena/latest/ug/flattening-arrays.html
Bring the SEQUENCE out and define the subquery using a WITH clause:
WITH dataset AS (
SELECT SEQUENCE(1, (SELECT MAX(week_number) FROM weekly_count)) AS week_array
)
SELECT week_number FROM dataset
CROSS JOIN UNNEST(week_array) as t(week_number)

i am trying to use the avg() function in a subquery after using a count in the inner query but i cannot seem to get it work in SQL

my table name is CustomerDetails and it has the following columns:
customer_id, login_id, session_id, login_date
i am trying to write a query that calculates the average number of customers login in per day.
i tried this:
select avg(session_id)
from CustomerDetails
where exists (select count(session_id) from CustomerDetails as 'no_of_entries')
.
but then i realized it was going straight to the column and just calculating the average of that column but that's not what i want to do. can someone help me?
thanks
The first thing you need to do is get logins per day:
SELECT login_date, COUNT(*) AS loginsPerDay
FROM CustomerDetails
GROUP BY login_date
Then you can use that to get average logins per day:
SELECT AVG(loginsPerDay)
FROM (
SELECT login_date, COUNT(*) AS loginsPerDay
FROM CustomerDetails
GROUP BY login_date
)
If your login_date is a DATE type you're all set. If it has a time component then you'll need to truncate it to date only:
SELECT AVG(loginsPerDay)
FROM (
SELECT CAST(login_date AS DATE), COUNT(*)
FROM CustomerDetails
GROUP BY CAST(login_date AS DATE)
)
i am trying to write a query that calculates the average number of customers login in per day.
Count the number of customers. Divide by the number of days. I think that is:
select count(*) * 1.0 / count(distinct cast(login_date as date))
from customerdetails;
I understand that you want do count the number of visitors per day, not the number of visits. So if a customer logged twice on the same day, you want to count him only once.
If so, you can use distinct and two levels of aggregation, like so:
select avg(cnt_visitors) avg_cnt_vistors_per_day
from (
select count(distinct customer_id) cnt_visitors
from customer_details
group by cast(login_date as date)
) t
The inner query computes the count of distinct customers for each day, he outer query gives you the overall average.

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

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

Compute count and running total for date field in SQL

Here is my dilemma. I have a field in the SQL database, called booking_date. The date is in a format like this
2014-10-13 12:05:58.533
I would like to be able to compute a count of bookings for each date (not date time) as well as a running total.
So my report would look something like so
My SQL code is like so
SELECT
dbo.book.create_time,
replace(convert(nvarchar, dbo.book.create_time, 106),' ', '/') as bookingcreation,
count(*) as Book_Count
FROM
....tables here
However, my count calculation is counting the date based of this type of date format > 2014-10-13 12:05:58.533 which is not computing correctly.
So instead, I'm getting this:
Also, I am not sure how to compute the running total. But I first need to get the count correctly.
Any help is greatly appreciated.
You seem to be using SQL Server. To get the count by day:
SELECT cast(dbo.book.create_time as date) as create_date
count(*) as Book_Count
FROM ...tables here
GROUP BY cast(dbo.book.create_time as date)
ORDER BY create_date;
You can get the cumulative sum in SQL Server 2012+ using the cumulative sum function:
SELECT cast(dbo.book.create_time as date) as create_date
count(*) as Book_Count,
sum(count(*)) over (order by cast(dbo.book.create_time as date) ) as Running_Count
FROM ...tables here
GROUP BY cast(dbo.book.create_time as date)
ORDER BY create_date;
In earlier versions, you can do something similar with a correlated subquery or cross apply.
EDIT:
In SQL Server 2008, you can do:
WITH t as (
SELECT cast(dbo.book.create_time as date) as create_date
count(*) as Book_Count
FROM ...tables here
GROUP BY cast(dbo.book.create_time as date)
)
SELECT t.create_date, t.Book_Count,
(SELECT SUM(Book_Count)
FROM t t2
WHERE t2.create_date <= t.create_date
) as Running_Count
FROM t
ORDER BY create_date;
Try to use trunc(book.create_time) in your query instead of the conversion you're doing

SQL - select max of datetime by day

Using the below example, I want to get a count of records grouped by Person and Product, for each day.
The example on the left is the data in the SQL Server 2008 database. The data on the right is my desired query result.
I could just substring off the time portion of the date values, then just do a Group By... but ideally the Max(date) would contain the full timestamp...
Is this what you want?
select t.person, t.product, count(*) as cnt, max(t.date) as date
from table t
group by t.person, t.product, cast(t.date as date);