How to merge two mdx queries when I have used same dimension & Same Measure - union

I have to merge below two mdx queries results into one set.
Query #1:
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS,
{[Product].[Category].&[4],
[Product].[Category].&[1]} ON ROWS
FROM [Adventure Works];
Output is:
Internet Sales Amount
Accessories $700,759.96
Bikes $28,318,144.65
Query #2:
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS,
{[Product].[Category].&[3]} ON ROWS
FROM [Adventure Works]
where [Geography].[State-Province].&[WA]&[US]
Output is:
Internet Sales Amount
Clothing $339,772.61
I want to filter the region only for "clothing" Category but not for the other category.
But I want the results together. How to union these two result sets?
Final output should be:
Internet Sales Amount
Accessories $700,759.96
Bikes $28,318,144.65
Clothing $339,772.61

This seems to work:
WITH
MEMBER [Product].[Category].[Clothing excl WA_US] AS
(
[Product].[Category].&[3]
,[Geography].[State-Province].&[WA]&[US]
)
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,{
[Product].[Category].&[4]
,[Product].[Category].&[1]
,[Product].[Category].[Clothing excl WA_US]
} ON ROWS
FROM [Adventure Works];
Suspect this performs better:
SELECT
{[Measures].[Internet Sales Amount]} ON COLUMNS
,{
[Product].[Category].&[4]
,[Product].[Category].&[1]
,Exists
(
[Product].[Category].&[3]
,[Geography].[State-Province].&[WA]&[US]
)
} ON ROWS
FROM [Adventure Works];

Related

Use pivot to get sum of each column using a join

I have two tables in SQL and I want to first join them and then use a pivot and get sum for each column by date.
For example:
Table Name Sales:
I have another Table called Manufacturer:
I want to join these tables and sum up the totals for Offer_Price, Sale_Price and Discount. I want to get the results date wise for each Manufacturer.
I wrote a query:
*/
Select Manufacturer_Name ,
date,
Offer_Price,
Sale_Price,
Discount
From
(
Select M.name, S.Date, S.Offer_Price, S.Sale_Price , S.Discount
from dbo.sales S
Inner join dbo.Manufacturer M on S.m_id = M.M_id
) as PivotData
Pivot (Sum(Offer_Price) , sum (Sale_Price), sum (Discount)
For date in ([2020-03-22], [2020-03-21], [2020-03-20])) as pivoting /*
But I get an error:
Incorrect syntax near ','.
This error is for the row where I am adding up the columns.
I am trying to learn pivot but I am stuck here. Can someone help?
Please note that I made up the data above, these are not the actual columns and tables but they are similar.
The results I want are like:
For Manufacturer Vivo and date 03/22/2020 it should add up and show offer price as 20,000, sale_Price as 19900, discount as 100 in a single row.

how to get data from two tables of sqlite and sort data

I've two tables
income
expense
the problem is I want to query all the data from both tables
SELECT income.date AS IN_DATE, expense.date AS EX_DATE FROM income, expense
I get weird result data is double times from db as you can see
you can try this out HERE
how can I get distinct results not double and at last wanna ask don't have idea of getting data from both tables and sort by date descending.
My guess is that you want union all:
select 'income' as which, id, title, date
from income
union all
select 'expense' as which, id, title, date
from expense;
This will give you a result set containing the rows from the two tables, with an identifier of which table each row comes from.
You can order by date and do other manipulations if you use a subquery:
select ie.*
from (select 'income' as which, id, title, date
from income
union all
select 'expense' as which, id, title, date
from expense
) ie
order by date desc;
Your simple SELECT does a cross product with the two columns (IN_DATE, EX_DATE). Hence, you get every possible combination of the values from both columns. INNER JOIN income ON expense.id=income.id or WHERE income.id == expense.id should do the trick.
You need to match the same ids, else SQL will just output any possible combination.
SELECT income.date AS IN_DATE, expense.date AS EX_DATE FROM income, expense WHERE income.id LIKE expense.id

How do I perform MDX member subtraction across another hierarchy?

I have a cube with customers and products. Each customer holds a varying combination of products, each in different amounts.
I would like to calculate the difference (subtraction), between one customer and another, of amounts held in each product.
I am using SQL Server Analysis Services 2014. An example from AdventureWorksDW2014 would be:
select
{[Customer].[Country].&[Australia],[Customer].[Country].&[Canada]} on columns,
non empty ([Product].[Category].members, [Measures].[Internet Sales Amount]) on rows
from
[Adventure Works]
This generates the following output:
Australia Canada
All Products $9,061,000.58 $1,977,844.86
Accessories $138,690.63 $103,377.85
Bikes $8,852,050.00 $1,821,302.39
Clothing $70,259.95 $53,164.62
However what I would like to obtain is
Australia Canada (Australia - Canada)
All Products $9,061,000.58 $1,977,844.86 $7,083,155.72
Accessories $138,690.63 $103,377.85 $35,312.78
Bikes $8,852,050.00 $1,821,302.39 $7,030,747.61
Clothing $70,259.95 $53,164.62 $17,095.33
Ideally this could be performed not just in MDX, but allowing the user to select any two arbitrary customers for comparison.
This is my first MDX question, so please let me know if it should be framed differently.
Here is the MDX query for the desired output:
WITH MEMBER Australia AS
SUM({([Measures].[Internet Sales Amount], [Customer].[Country].&[Australia])}), FORMAT_STRING = "#,#.##"
MEMBER Canada AS
SUM({([Measures].[Internet Sales Amount], [Customer].[Country].&[Canada])}), FORMAT_STRING = "#,#.##"
MEMBER [Australia - Canada] AS
[Australia] - [Canada]
SELECT
{[Australia], [Canada], [Australia - Canada]} ON COLUMNS,
NON EMPTY {[Product].[Category].MEMBERS} ON ROWS
FROM [Adventure Works]

SQL Server Group data by week but show the start of the week

I have a query to select customer data and I want to keep an evolution of the number of customers. In week 1 I have 2 new customers so the number is 2. In week 2 I receive 3 new customers, so the number of customers is 5.
I have following query to do this
SELECT LAST_UPDATED_WEEK, SUM( NUM_CUSTOMERS ) OVER ( ORDER BY LAST_UPDATED_WEEK ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS "Number of customers"
FROM (
SELECT DATEADD(dd,DATEDIFF(dd,0,REGISTRATION_DATE),0) AS LAST_UPDATED_WEEK,
COUNT(DISTINCT CUSTOMER_ID) AS NUM_CUSTOMERS
FROM CUSTOMERS_TABLE
GROUP BY DATEADD(dd,DATEDIFF(dd,0,REGISTRATION_DATE),0)) AS T
But when I run this query, it doesn't group my data by week. I read about a DATEPART function, but that returns an integer, but I need to have the actual date.
Can someone help me?
Just replace dd in DATEADD and DATEDIFF functions with WEEK

[Measures].[First4Days] for each month ON ROWS

How do I replace the following placeholder with a measure that returns the sum of the [Measures].[Internet Sales Amount] for the first 4 days of each month that are on the rows?
WITH
MEMBER [Measures].[First4Days] AS
SUM(1) //<<<<<<placeholder
SELECT
{
[Date].[Calendar].[Month].&[2005]&[6]
:
[Date].[Calendar].[Month].&[2008]&[7]
} ON ROWS
,{
[Measures].[Internet Sales Amount]
,[Measures].[First4Days]
} ON COLUMNS
FROM [Adventure Works];
Your placeholder should be
Sum(Head([Date].[Calendar].CurrentMember.Children, 4), [Measures].[Internet Sales Amount])
You just take the children of the current month - as coming from the context of the cell, and from this, the first four, and then sum it across Internet Sales Amount.
Please note that stating rows before columns is not standard MDX, even if Analysis Services accepts it. According to the standard, axes must stated be in ascending order, and rows are Axis(1), while columns are Axis(0).

Resources