Creating relationship between tables of different aggregation - sql

I have a retail store data with 50000 records. One of the columns in that file is Segment (Home office, Corporate and Consumer). This data i have in one table. In another table i just have two columns - Segment, Forecast Sales. What query needs to be written to get the actual segment sales and Forecast Sales in single table or how should the relationship be created in SQL.
Data can be found here : https://community.tableau.com/docs/DOC-1236

I will assume table name [Orders] as per your excel and [Segment Forecast] for the table with columns [Segment], [Forecast Sales].
select sf.Segment,sum(o.sales) [Actual Sales],sf.[Forecast Sales]
from [Orders] o
inner join [Segment Forecast] sf on o.Segment=sf.Segment
group by sf.Segment,sf.[Forecast Sales]

Related

forcing SSAS respecting relationships between dimensions

with regard to the following question and the given response I have the following question:
I am facing the same issue, but creating a bridge table like mentioned above is no option in my case as I still need the separate dimensions for other operations and different analysis.
For budgeting reasons I have to show all customers even if they have no match with the fact table. Enabling the corresponding option in EXCEL obiously leads to a cross join of the selected dimensions resulting in a complete list of all customers in all countries, although not each customer exists in all countries.
Is there another altrnative to force SSAS respecting relationships defined?
Many thanks in advance for assistance.
Let’s say you have FactSales with CustomerKey, CountryKey, DateKey and SalesAmount. When you build a PivotTable in Excel with Customer and Country on rows and SalesAmount on columns it only shows customers with sales. If you set Excel to show rows with no data it shows all customers in all countries.
Try building a new FactCustomerCountry table with CustomerKey and CountryKey only. Create a measure Customer Country Count.
Then create a new calculated measure:
CREATE MEMBER CURRENTCUBE.[Measures].[Sales Amount with Zeros]
AS
IIF(
IsEmpty([Measures].[Customer Country Count]),
Null,
CoalesceEmpty([Measures].[SalesAmount], 0)
);
Use that measure in your PivotTable instead of SalesAmount. Do not show rows with no data. The measure should return meaningful Customer-Country combinations and should return all customers.
Hi you can solve your issue in MDX. Take a look at the query below, its an example on Adventureworks. This will fetch all the products that were sold and the quaters that they were sold in.Now some products were never sold, I still want to see those product.
select [Measures].[Internet Sales Amount] on columns,
--non empty
{filter(([Product].[Subcategory].[Subcategory],[Date].[Calendar Quarter of Year].[Calendar Quarter of Year]),[Measures].[Internet Sales Amount]>0),
filter(([Product].[Subcategory].[Subcategory],[Date].[Calendar Quarter of Year].defaultmember),[Measures].[Internet Sales Amount]=null)
}
on rows
from
[Adventure Works]

Populating fact table

I've a data warehouse for sales, it has 3 dimensions [product,time,store] and a fact table [sales_fact].
Primary key of 'sales_fact' table is made up of all primary keys of dimensions table, dimension tables are all filled up manually now I want to fill 'sales_fact' table with SUM of prices of products stored in a city for a specific month or 3 month period.
How should I sum up prices from product table which are related to a specific month and add it to fact table?
Considering that sum up prices from product table which are related to a specific month
is a measure, your query can be like below :
SELECT DS.City, DT.[Month], SUM(DP.Price)FROM
SalesFact AS S
LEFT JOIN DimProduct AS DP ON DP.ProductSK=S.ProductSK
LEFT JOIN DimTime AS DT ON DT.DateSK=S.DateSK
LEFT JOIN DimStore AS DS ON DS.StoreSK=S.StoreSK
WHERE [Date condition] --Add your date conditoon
GROUP BY DS.City, DT.[Month]
You can use a view for this measure.

Two Tables in SQL Database

beginner sql coder. I have an Access database with a customers table and orders table. The headers of the columns have a space (i.e: Shipped Date). I need to return Company Name, Contact Name, and Phone of customers who placed orders in March of 1998. Shipped Dates are formatted as 01-Mar-1998.
How do I do this? Access keeps giving me errors
SELECT Orders.*,Customers.CompanyName, Customers.ContactName, Customers.Phone
FROM Customers, Orders
(SELECT *
FROM Orders
JOIN Customers ON Orders.Order ID=Customers.Order ID)
AND Shipped Date BETWEEN #03/01/1998# AND #03/31/1998#;
GROUP BY Customers.CompanyName, Customers.ContactName, Customers.Phone;
EDIT:
New code
SELECT *
FROM Orders INNER JOIN Customers ON Orders.Customer=Customers.[Company Name]
WHERE Orders.[Shipped Date] BETWEEN #01-MAR-1998# AND #31-MAR-1998#;
The code runs, but Access prompts me to enter values for the all of the column names...Please help!
Your Query is incorrectly formed. As we should enclose the coloumns with names containing spaces in []
also tour GROUP BY was not relevant.
Now when we use JOIN we should have the query like :
SELECT * FROM Orders JOIN Customers ON Orders.[Order ID]=Customers.[Order ID]
WHERE Orders.[Shipped Date] BETWEEN #03/01/1998# AND #03/31/1998#;

Summing Sales Data Access

I'm just starting out with SQL, I have been playing around with simple select queries and grouping data, now I want to pull some actually useful data out of our database for analysis. The data is organized as follows:
Access 2010 Database
I didn't set it up, I know it isn't set up as it should be
I can't change data, only poll
-Customers are kept in one table
-Closed orders are kept in another table (each line item is listed with invoice #, date closed, Customer ID as well as other info)
-Archived closed orders table keep sales records that are a year + old (table is laid out exactly the same as Closed order table)
I want to start with a simple query, list all the customers from a certain branch and their past year totals. Here's what I have tried:
SELECT CUSTOMERS.Company, CUSTOMERS.[Ship City], (SELECT SUM (CLOSEDORDERS.Quant*CLOSEDORDERS.SellPrice) FROM CLOSEDORDERS WHERE CUSTOMERS.ID = CLOSEDORDERS.CustID) AS LifeTotal
FROM CUSTOMERS, CLOSEDORDERS
WHERE CUSTOMERS.Branch=33;
When I run the query, it asks me to enter a parameter value for CLOSEDORDERS.Quant. What am I doing wrong?
I think this is what you're looking for with an OUTER JOIN:
SELECT CUSTOMERS.Company,
CUSTOMERS.[Ship City],
SUM(CLOSEDORDERS.Quant*CLOSEDORDERS.SellPrice) AS LifeTotal
FROM CUSTOMERS
LEFT JOIN CLOSEDORDERS ON CUSTOMERS.ID = CLOSEDORDERS.CustID
WHERE CUSTOMERS.Branch=33
GROUP BY CUSTOMERS.Company,
CUSTOMERS.[Ship City]
If you only want to return matching results from both tables, then use a standard INNER JOIN instead of the LEFT JOIN.

SQL query (in ZOHO reports) to join two tables by subtotas :-O

if you are out there, please help me:
I have 2 different tables, with different columns and no primary key:
1 - daily actual sales data
2 - monthly budget sales data
I need:
- one consolidated table with monthly actual sales (counted) and montly budget sales, to compare monthly sales vs budget.
Is it possible?
Of course.
You can aggregate the numbers for every month from the actual sales data table using sum() and group by and can join into this the budget table via the month column.