How to identify churn accounts in teradata? - sql

So I have created a table that has the following columns from a transaction table with all customer purchase records: 1. Month-Year, 2.Customer ID, 3. Number of Transactions in that month.
I'm trying to create a table that has the output of 1. Month-Year, 2. Number of churned customers in that month defined as customers who have not had a transaction in the last 12 month.
I would appreciate any suggestions here- I can't figure out how to quantify the date range difference. Was thinking about rank ordering and subtracting the rank order dates but that doesn't work in execution.

Related

How to perform aggregate function in SQL based on a condition of the data?

I have a table with fields RegionName, Month and Mo.Rent Price. Each row contains values for a city's average rent price in a particular month. I want to use a SQL Query to pull the cities that have the lowest rent prices for each month. In other words, I don't want to find the city with the min rent overall. I want the min rent from the other rent values in that month. And I want that for each month. Anyone know what query I could use? Thanks I attached the table im working with below

sum from one table between dates of second table

I have a report table that has the following columns-
Item#/Location/Price Hold Date/ Price Execution date
And a sales table with -
Item#/Location/Date/Units
I'm trying to sum the units between the two dates in report table matching them based on item number and location into something like -
Item/Location/Price Hold Date/Price Execution Date/ Total Units Sold

Different detail levels in one table

EDITED
I'm having a problem with a table, where I need to store measures on different detail levels. My default table is:
Id
TotalQuantity
Amount
1
75
1000
Where TotalQuantity is a sum of quantities from every month.
Now I need to add into my default table an information, what quantities I have each month. These monthly quantities should be in one column, so I used UNION.
The problem is that when I will sum up values from these columns in some reports, TotalQuantity AND OTHER VALUES THAT ARE THE SAME FOR BOTH ROWS will be displayed wrong. How can I possibly store all that information?
You need a fact table at the (id, month) grain, like FactMonthlyTotals(id, month, amount). If you have other data that is not for a particular month it would go on a separate fact table, or perhaps a dimension table.

Populate those customers with no transactions at day level with zero(0) (Teradata SQL Assistant)

Probably somebody has created this before, but I need to create a dummy table to be able to union to my other transaction table.
The use case is, I have daily transactional data with Fields like
"customer", "date", "sales"
No I want a dummy data with the same fields populated by zero values at day level,
customer information should read from customer master but the date field should be pre-generated at day level at least for current year and previous 3 years.
The idea is I want to union this dummy table to my original transaction table to be able to produced no null transactions at day level, and those days with no sales should be populated by zero.
This requirement is for BI Analytics projects.
The make it simple I don't want my table to have a missing transactions at day level.
Here is the sample screenshot

Excluding results that are within same month in SQL

I have two tables -- one, a history table that contains a log of some kind of entries, and another (let's call it flags) that contains columns about flags (for a certain account). Both tables contain account IDs.
I want to write a query that only extracts rows from the flag table if the account ID does not already have an entry for that month in the history table (e.g., in the flag table, an entry was entered on April 2, 2019 and in the history table, the account already had an entry recorded on April 1, 2019. The result is, the April 2nd entry should not be pulled up).
I have a query right now that basically looks like this:
SELECT *multiple column names*
FROM flags
WHERE NOT EXISTS (SELECT acc_id FROM history WHERE ...)
This is where I am stuck. With the subquery, I basically want to get the matches where the dates from both tables match (same month and year), and with the WHERE NOT EXISTS, exclude the results from flag that are found in the subquery (essentially I only want results where the date for the entry is not from the same month)
The most important columns are:
the account ID (to correctly associate each log entry to the right account)
date (to only get rows where the month recorded is not already logged in the history table)
I initially used MONTH(), but that only extracts the month of the date. I need it to match both the month and the year because the history table contains a few years of data.
Any help would be greatly appreciated! Thank you in advance!
SELECT *multiple column names*
FROM flags
WHERE NOT EXISTS (
SELECT 1
FROM history
WHERE history.acc_id=flags.acc_id
AND date_trunc('month', history.date) =
date_trunc('month', flags.date)
)
The date_trunc function will work for postgres, which was one of the tags originally. If you aren't using postgres, there may be a similar function in your database, it you could format the dates to just Year-month and compare the resulting strings.