Average Monthly Balance of Accounts - average

I have table containing daily balances of all accounts of different branches with following fields.
Branch_Code, Account_Num, Date, Day_End_Balance
The table contains data from 01-01-2015 to 31-12-2015 on daily basis for each accounts. I want to get average monthly balance from January to December for each account of Branch_Code "123".
What should be the SQL Query for teradata.
Regards
KAM

Select AVG(balance) from table where branch_code="123" group by Account_Num
I guess.. this should work.. :)

Related

How to calculate YTD hours on a Crystal Report?

My company has a payroll program that prints payroll checks and pay stubs using Crystal Reports. The report selects payroll data from a table that has these columns: Hours, PeriodAmount, and YTDAmount. The hours are based on payroll period ending date. The report selects records based on a single pay period end date. So, hours would be selected for a single pay period only. However, we would like to print YTD hours on the pay stub. In other words, hours accumulated from beginning of year to current pay period. Since YTD hours is not a column in the table, would we need to include a sub report to accumulate hours? I don't think running totals would work since we are selecting a single period only. Any thoughts? Thanks.
Yes, a subreport is one option.
Another option is to select all pay periods in the current year and compute the total for the current period using a conditional total. For example, create af ormula that returns the hours if the record belongs to the current period and zero otherwise. Then, SUM that formula per employee. That SUM would give you the hours in the current period.

Calculated Field to Count While Between Dates

I am creating a Tableau visualization for floor stock in our plant. We have a column for incoming date, quantity, and outgoing date. I am trying to create a visualization that sums the quantity but only while between the 2 columns.
So for example, if we have 9 parts in stock that arrived on 9/1 and is scheduled to ship out on 9/14, I would like this visualization to include these 9 parts in the sum only while it is in our stock between those 2 dates. Here is an example of some of the data I am working with.
4/20/2018 006 5/30/2018
4/20/2018 017 5/30/2018
4/20/2018 008 5/30/2018
6/29/2018 161 9/7/2018
Create a new calculation:
if [ArrivalDate]>="2018-09-01" and [ArrivalDate]<"2018-09-15"
and [Shipdate]<'2018-09-15"
then [MEASUREofStock] else 0 end
Here is a solution using UNIONs written before Tableau added support for Unions (so it required custom SQL)
Volume of an Incident Queue at a Point in Time
For several years now, Tableau has supported Union directly, so now it is possible to get the same effect without writing custom SQL, but the concept is the same.
The main thing to understand is that you need a data row per event (per arrival or per departure) and a single date column, not two. That will let you calculate the net change in quantity per day, and you can then use a running total if you want to see the absolute quantity at the close of each day
There is no simple way to display the total quantity between the two dates without changing the input table structure. If you want to show all dates and the "eligible" quantity in each day, you should
Create a calendar table that has all dates start from 1990-01-01 to 2029-12-31. (You can limit the dates to be displayed in dashboard later by applying date filter, but here you want to be safe and include all dates that may exist in your stock table) Here is how to create the date table quickly.
Left join the date table to stock table and calculate the eligible quantity in each day.
SELECT
a.date,
SUM(CASE WHEN b.quantity IS NULL THEN 0 ELSE b.quantity END) AS quantity
FROM date a
LEFT JOIN
stock b on a.date BETWEEN b.Incoming_Date AND b.Outgoing_Date
GROUP BY a.date
Import the output table to Tableau, and simply add dates and quantity to the chart.

Year over year monthly sales

I am using SQL Server 2008 R2. Here is the query I have that returns monthly sales totals by zip code, per store.
select
left(a.Zip, 5) as ZipCode,
s.Store,
datename(month,s.MovementDate) as TheMonth,
datepart(year,s.MovementDate) as TheYear,
datepart(mm,s.MovementDate) as MonthNum,
sum(s.Dollars) as Sales,
count(*) as [TxnCount],
count(distinct s.AccountNumber) as NumOfAccounts
from
dbo.DailySales s
inner join
dbo.Accounts a on a.AccountNumber = s.AccountNumber
where
s.SaleType = 3
and s.MovementDate > '1/1/2016'
and isnull(a.Zip, '') <> ''
group by
left(a.Zip, 5),
s.Store,
datename(month, s.MovementDate),
datepart(year, s.MovementDate),
datepart(mm, s.MovementDate)
Now I'd like to add columns that compare sales, TxnCount, and NumOfAccounts to the same month the previous year for each zip code and store. I also would like each zip code/store combo to have a record for every month in the range; so zeros if null.
I do have a calendar table that I tried to use to get all months, but I ran into problems because of my "where" statements.
I know that both of these issues (comparing to previous year and including all dates in a date range) have been asked and answered before, and I've gotten them to work before myself, but this particular one has me running in circles. Any help would be appreciated.
I hope this is clear enough.
Thanks,
Tim
Treat the Query you have above as a data source. Run it as a CTE for the period you want to report, plus the period - 12 months (to get the historic data). (SalesPerMonth)
Then do a query that gets all the months you need from your calendar table as another CTE. This is the reporting months, not the previous year. (MonthsToReport)
Get a list of every valid zip code / Store combo - probably a select distinct from the SalesPerMonth CTE this would give you only combos that have at least one sale in the period (or historical period - you probably also want ones that sold last year, but not this year). Another CTE - StoreZip
Finally, your main query cross joins the StoreZip results with the MonthsToReport - this gives you the one row per StoreZip/Month combos you are looking for. Left join twice to the SalesPerMonth data, once for the month, once for the 1 year previous data. Use ISNULL to change any null records (no data) to zero.
Instead of CTEs, you could also do it as separate queries, storing the results in Temp tables instead. This may work better for large amounts of data.

How to set the SQL Query for a Report?

Crystal Reports 2011.
Database is MS Access 2003
I have the following tables:
Calendar
has Date entries for the current and next Year, for every Day of the Year, plus some Status Fields marking certain days as "Special" (Joining this table so I have a record for Days with no activity.
Staff table
StaffNo
Name
.
.
.
DayResults
Date
StaffNo
Status
.
.
.
The DayResults table has one entry per Day and Staff.
Entries are only made when the staff gets an entry by the Program for Status or other events. Staff that does not log in the system has no entry for this Day.
So, in case of John not showing up on July 2nd, i have no entry for him for this Day. But I need an entry for my report!
I need to create a Report that fetches Data from the DayResults Table and make calculations on the Parameters here, in order to calculate a Daily as ell as Period Bonus.
The rules for this Bonus require that a Day without activity (i.e. No Show) results in a negative bonus amount.
Therefore I need to have a select statement which creates an entry FOR EACH DAY FOR EACH STAFF.
This should look like this:
Date StaffNo Name Status
2016/07/01 1 Jim 1
2016/07/01 2 John 2
2016/07/02 1 Jim 2
2016/07/02 2 John NULL
(John did not show up on 2016/07/02 ...)
SELECT Calendar.Date, Staff.StaffNo, Staff.NickName, DayResults.Status
FROM Staff LEFT JOIN (Calendar RIGHT JOIN DayResults ON Calendar.Date = DayResults.Date) ON Staff.StaffNo = DayResults.StaffNo;
Unfortunately, no entry here for John on July 2nd?
Any idea how to proceed?
Manfred

PostgreSQL 8.2 extract week number from a a date field

This might be a simple one but I haven't got a solution yet. I have a create_date field which is a date type, and a revenue number. I want to see weekly break down of revenue.
I can get the numbers easily in tableau because of built in functionality but doing it in PostgreSQL is where I need some help.
If you want the revenue by week, you'll need to group and aggregate:
select extract (week from create_date) as week, sum(revenue) from table group by week