select total sales per day - sql

i have query like this and i wanted to have sum of sales per day
SELECT DATEPART(day,deduction_timestamp) as [day],
[fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp), fare_deduction
with result :
day fare_deduction
--------------------------------
1 10.00
3 15.00
3 2.00
4 10.00
10 20.00
31 12.00
and i wanted the result to be like this..
day fare_deduction
--------------------------------
1 10.00
3 17.00
4 10.00
10 20.00
31 12.00
and take note that not all day have values and it only display the affected
day only. Can help me on these? Thanks!

SELECT DATEPART(day,deduction_timestamp) as [day],
sum(fare_deduction) as fare_deduction
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)

Simply use sum, it will get your required result
SELECT DATEPART(day,deduction_timestamp) as [day],
SUM([fare_deduction]) [fare_deduction]
FROM [dbfastsprocess].[dbo].[vClosingTransitLog]
WHERE bus_id in ('JEAST', 'MKV004', 'NWTN01')
and YEAR(deduction_timestamp) = ISNULL(2016, YEAR(deduction_timestamp))
and MONTH(deduction_timestamp) = ISNULL(10, MONTH(deduction_timestamp))
GROUP BY DATEPART(day,deduction_timestamp)

Related

How to calculate average monthly number of some action in some perdion in Teradata SQL?

I have table in Teradata SQL like below:
ID trans_date
------------------------
123 | 2021-01-01
887 | 2021-01-15
123 | 2021-02-10
45 | 2021-03-11
789 | 2021-10-01
45 | 2021-09-02
And I need to calculate average monthly number of transactions made by customers in a period between 2021-01-01 and 2021-09-01, so client with "ID" = 789 will not be calculated because he made transaction later.
In the first month (01) were 2 transactions
In the second month was 1 transaction
In the third month was 1 transaction
In the nineth month was 1 transactions
So the result should be (2+1+1+1) / 4 = 1.25, isn't is ?
How can I calculate it in Teradata SQL? Of course I showed you sample of my data.
SELECT ID, AVG(txns) FROM
(SELECT ID, TRUNC(trans_date,'MON') as mth, COUNT(*) as txns
FROM mytable
-- WHERE condition matches the question but likely want to
-- use end date 2021-09-30 or use mth instead of trans_date
WHERE trans_date BETWEEN date'2021-01-01' and date'2021-09-01'
GROUP BY id, mth) mth_txn
GROUP BY id;
Your logic translated to SQL:
--(2+1+1+1) / 4
SELECT id, COUNT(*) / COUNT(DISTINCT TRUNC(trans_date,'MON')) AS avg_tx
FROM mytable
WHERE trans_date BETWEEN date'2021-01-01' and date'2021-09-01'
GROUP BY id;
You should compare to Fred's answer to see which is more efficent on your data.

SQL - Repeated sum of the values, based on dates selected

Currently, I need a simple thing:
sale_date
Gross
SUM_GROSS
2018-01-01
1
6
2018-01-02
2
6
2018-01-03
3
6
I know this question already mentioned before, the difference now, is that I need to calculate a sum based on selected dates. (I use BigQuery)
SUM(SALES.GrossValueBaseCurrency) OVER(PARTITION BY ???) AS SUM_GROSS
If I will use
SUM(SALES.GrossValueBaseCurrency) OVER(PARTITION BY SALE.SALE_DATE) AS SUM_GROSS
It will give me what I would like ONLY if I will select specific ONE day.
How can I make it work, so if I will select different dates, SUM_GROSS will repeat the SUM of ALL gross values for a selected period of time?
SAMPLE DATA and Expectations:
Expecting 60 in SUM_GROSS column
Row SALE_DATE GROSS SUM_GROSS
1 25/08/2018 10.00 60
2 04/10/2018 10.00 60
3 04/07/2018 10.00 60
4 01/03/2018 10.00 60
5 10/02/2018 10.00 60
6 10/01/2018 10.00 60
If you will query this table result should be :
SELECT SUM(GROSS) AS GROSS, SUM_GROSS FROM TABLE
WHERE SALE_DATE BETWEEN 01/01/2018 AND 01/04/2018
GROUP BY SUM_GROSS
RESULT:
GROSS SUM_GROSS
30 30
I think you want conversation in partition clause:
SUM(SALES.GrossValueBaseCurrency) OVER (PARTITION BY EXTRACT(YEAR from SALE.SALE_DATE), EXTRACT(MONTH from SALE.SALE_DATE)) AS SUM_GROSS
EDIT :
SELECT . . .,
SUM(SALES.GrossValueBaseCurrency) OVER () AS SUM_GROSS
FROM SALES s
WHERE SALE.SALE_DATE BETWEEN "2018-01-01 AND "2018-02-01"
Is this what you are looking for?
SUM(CASE WHEN sales.sale_date = '2018-01-01'
THEN SALES.GrossValueBaseCurrency
ELSE 0
END) OVER () AS sales_20180101

Calculating Daily Change and New Salespersons

I have a SALES table with Person, Date and Qty:
Person Date Qty
Bob 2016-08-01 5
Bob 2016-08-02 2
Bob 2016-08-03 6
Bob 2016-08-04 4
Jim 2016-08-01 1
Jim 2016-08-02 3
Jim 2016-08-03 2
Jim 2016-08-04 2
Sheila 2016-08-03 9
Sheila 2016-08-04 12
I would like to produce 3 outputs
1) The Daily change in total Qty for the Persons who were selling the prior day:
Date Qty Change Pct Change
2016-08-01 0 0 0.00
2016-08-02 5 -1 -16.66
2016-08-03 8 3 60.00
2016-08-04 18 1 5.88
Note that 8/1/16 is the first day in my dataset, so total = 0 since no SalesPerson was selling the prior day. Also note that Sheila started selling on 8/3, which means here 8/3 sales do not figure into the 8/3 qty or change. However, when determining the 8/4 change, Sheila's 8/3 sales of 9 units are used to determine the correct total change of 1 unite for 8/4.
2) I want to break out the totals for new SalesPersons each day on one line per day. If no new Salespersons are added, then the date would show zeros.
Date New Qty
2016-08-01 6
2016-08-02 0
2016-08-03 9
2016-08-04 0
Since 8/1 was the first day of selling for anyone, both sales for Bob and Jim are included in the New Qty of 6 for 8/1.
3) The final output shows detail for #2, such as the date that a new SalesPerson started selling and the quantity they sold on that day.
Date Person New Qty
2016-08-01 Bob 5
2016-08-01 Jim 1
2016-08-03 Sheila 9
Are these 3 outputs possible in SQL Server?
You can use LAG() if you are using sql server 2012 or above. You got the "change". You can figure out the rest.
Query #1:
SELECT
dt.DateVal,
SUM(CASE WHEN dt.PreviousQuota = 0 THEN 0 ELSE dt.PreviousQuotadd END) Change
FROM
(
SELECT
Id,
Person,
DateVal,
Qty,
LAG(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal) AS PreviousQuota ,
LEAD(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal) AS NextQuota ,
LAG(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal) AS PreviousQuotad ,
(Qty - LAG(Qty, 1,0) OVER ( PARTITION BY Person ORDER BY DateVal)) AS PreviousQuotadd
FROM Table1
) AS dt
Here is the Fiddlle link.
I solved the following queries:
Query #2:
WITH CTE_1 AS
(Select DISTINCT Date FROM [master].[dbo].[Sales] GROUP BY Date)
, CTE_2 AS
(select Person,Date,ROW_NUMBER() OVER(Partition By Person Order By Date) as RowNum, SUM(Qty) as [PersonDailySum]
FROM [master].[dbo].[Sales]
GROUP BY Person,Date)
,CTE_3 AS(
SELECT T0.date, SUm([PersonDailySum]) As [New Qty]
FROM CTE_1 T0
LEFT JOIN CTE_2 T1
ON T0.Date = T1.date AND T1.RowNum = 1
GROUP BY T0.Date)
SELECT Date, ISNULL([New Qty],0) AS [New Qty] FROM CTE_3
Query #3
SELECT Date,Person,SUM([PersonDailySum]) AS [New Qty] FROM (
select Person,Date,ROW_NUMBER() OVER(Partition By Person Order By Date) as RowNum, SUM(Qty) as [PersonDailySum]
FROM [master].[dbo].[Sales]
GROUP BY Person,Date) T0
WHERE T0.RowNum = 1
GROUP BY Date,Person

Joining multiple tables with filter

I got multiple tables and i'm only using 3 for testing. I don't know if this is the right approach.
AccntTbl
id accnt amount date
---------------------------------
1 xxx 10.00 1/1/2016
2 yyy 20.00 1/1/2016
3 zzz 30.00 1/1/2016
SupplyTbl
id accnt supply date
-------------------------------
1 xxx 5.00 1/10/2016
1 xxx 5.00 2/14/2016
IssuedTbl
id accnt issued date
---------------------------------
1 xxx 3.00 1/9/2016
1 xxx 2.00 2/1/2016
and the result will be like this if filter to JANUARY
id accnt amount issued balance
--------------------------------------------
1 xxx 15.00 3.00 12.00
2 yyy 20.00 0.00 20.00
3 zzz 30.00 0.00 30.00
and if FEBRUARY
id accnt amount issued balance
---------------------------------------------
1 xxx 20.00 5.00 15.00
2 yyy 20.00 0.00 0.00
3 zzz 30.00 0.00 0.00
and here's my query that i've come up with
SELECT accnttbl.id,
accnttbl.accnt,
Sum(accnttbl.amount)
+ Sum(SupplyTbl.supply) AS amount,
issuedtbl.issued
FROM accnttbl
LEFT JOIN (SELECT id,
Sum(supply) AS supply,
date
FROM supplytbl
GROUP BY id,
date) AS SupplyTbl
ON accnttbl.id = SupplyTbl.id
AND Month(SupplyTbl.date) BETWEEN 1 AND 2
LEFT JOIN (SELECT id. Sum(issued) AS issued,
date
FROM issuedtbl
GROUP BY id,
date)
ON accnttbl.id = issuedtbl.id
AND Month(issuedtbl.date) BETWEEN 1 AND 2
WHERE Year(accnttbl.date) = 2016
GROUP BY accnttbl.id,
accnttbl.accnt
and i kinda mess up..glad for any help :)
the result be like this..in JANUARY
id accnt amount issued
1 xxx 30.00 10.00
2 yyy 20.00 0.00
and in FEBRUARY
1 xxx 60.00 20.00
2 yyy 20.00 0.00
with the value that i been given..it just..it doubled the amount on its own..the result are wrong..why is that ?
Try as below :
SELECT AccntTbl.id, AccntTbl.accnt
, SUM(AccntTbl.amount) + SUM(SupplyTbl.supply) as amount
, SUM(IssuedTbl.issued) as issued
, (SUM(amount) - SUM(issued)) as balance
FROM AccntTbl LEFT JOIN
(SELECT id, COALESCE(SUM(supply), 0) AS supply, date FROM SupplyTbl
GROUP By id, date) AS SupplyTbl
ON AccntTbl.id = SupplyTbl.id AND (MONTH(SupplyTbl.date) BETWEEN
1 AND 2)
LEFT JOIN
(SELECT id, COALESCE(SUM(issued),0) as issued, date from IssuedTbl
GROUP By id, date )
ON AccntTbl.id = IssuedTbl.id AND (MONTH(IssuedTbl.date) BETWEEN
1 AND 2)
WHERE (YEAR(AccntTbl.date) = 2016)
GROUP By AccntTbl.id, AccntTbl.accnt
;
In sql anything added with NULL is NULL . So in your subquery if you are not using COALESCE, you are adding your value with NULL, hence you are not getting your desired result.

How to sum total amount for every month in a year?

I have a database in SQL Server 2012 and there is a table with dates in D.M.YYYY format like below:
ID | Date(date type) | Amount(Numeric)
1 3.4.2013 16.00
1 12.4.2013 13.00
1 2.5.2013 9.50
1 18.5.2013 10.00
I need to sum the total amount for every month in a given year. For example:
ID | Month | TotalAmount
1 1 0.00
...
1 4 29.00
1 5 19.50
I thought what I needed was to determine the number of days in a month, so I created a function which is described in determine the number of days, and it worked. After that I tried to compare two dates(date type) and got stuck; there are some examples out there, but all of them about datetime.
Is this wrong? How can I accomplish this?
I think you just want an aggregation:
select id, month(date) as "month", sum(amount) as TotalAmount
from t
where year(date) = 2013
group by id, month(date)