sum from one table between dates of second table - sum

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

Related

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.

Woocommerce aggregate report by each person type sold - filtered by date range for bookable product

Does any functionality or SQL query exist to build a report on bookable products sold by the individual person type filtered by a date range? I'd like to see a count of each person type sold in each day across a month.
If not where are the individual sold person types stored in the woocommerce SQL tables?

How to identify churn accounts in teradata?

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.

MDX : combining data from different tables

How can I combine data coming from different tables.
Let's assume I have 2 tables:
First with sales:
id shop
id product
date
amount
Second with stocks:
actually, with the same structure
id shop
id product
date
amount
I need to analyze for how many days' stock there is in the shop now. For that I need to calculate the average sale per shop per day for last 20 weeks and then divide the remaining stock by the average sales rate.
How can I achieve this?
This is not a actual problem in MDX as you can combine dimension over different fact tables.
You need to create your 3 dimension (using reference table or similar) :
id_shop -> [Shop]
id_product -> [Product]
date -> [Time]
Now we need to add the two tables as 'fact' tables. Recall that Fact Tables are the ones defining measures.
In icCube create a default Cube, e.g. [Cube], and for each table create a 'measure group' (just click the '+' ).
Bind your tables to the dimension, the 'magic'wand will do the work and create a measure for each table (e.g. [Stock] & [Sales] ).
Once the schema is defined and deployed you can use both measures without taking even noticing they are coming from different tables :
[Measures].[Sales] / [Measures].[Stocks]

SQL Database design question

I have a task to design a SQL database that will record values for the company's commodity products (sku numbers). The company has 7600 unique product items that need to be tracked, and each product will have approximately 200 values over the period of a year (one value per product per day, over the period of a year).
My first guess is that the sku numbers go top to bottom (each sku has a row) and each date is a column.
The data will be used to view in chart / graph format and additional calculations will be displayed against those (such as percentage profit margin etc)
My question is:
- is this layout advisable?
- do I have to be cautious of anything, if this type of data goes back about 15 yrs (each table will represent a year)
Any suggestions?
It better to have 3 columns only - instead of many as you are suggesting:
sku date value
-------------------------
1 2011-01-01 10
1 2011-01-02 12
2 2011-01-01 5
This way you can easily add another column if you want to record something else about a given product per date.
I would suggest a table for your products, and a table for the historical values. Maybe create an index for the historical values based on date if you plan to select for specific time periods.
create table products (
id number primary key,
sku number,
name text,
desc text);
create table values (
id number primary key,
product_id number,
timestamp date,
value number,
foreign key fk_prod_price product_id on product.id);
create index idx_price on values.timestamp;
NOTE: not actual sql, you will have to write your own
If you do like #fiver wrote, you don't have to have a table for each year either. Everything in one table. And add indexes on sku/date for faster searching