i use SSAS And Sql Server 2008 R2.
i writed this query in SSAS
SELECT
Measures.[Internet Sales Amount] ON COLUMNS
, [Measures].[Internet Freight Cost] ON COLUMNS
FROM [Adventure Works]
WHERE
(
[Date].[Calendar].[Calendar Quarter]. & [2003] & [2],
[Product].[Product Line].[Mountain],
[Customer].[Country].[Australia]
)
I was got this error
Executing the query ...
An axis number cannot be repeated in a query.
Execution complete
How i can select two columns in MDX Query?
You don't need to specify the axis for each measure:
SELECT
{
[Measures].[Internet Sales Amount],
[Measures].[Internet Freight Cost]
}ON COLUMNS
FROM [Adventure Works]
WHERE
(
[Date].[Calendar].[Calendar Quarter]. & [2003] & [2],
[Product].[Product Line].[Mountain],
[Customer].[Country].[Australia]
)
Related
I have this following query in TSQL :
SELECT
CustomerName,
SumOfSales
FROM (
SELECT
Customers.CustomerName,
SUM ( Sales.SalesAmount ) AS SumOfSales
FROM
Sales
INNER JOIN Customers
ON Sales.CustomerKey = Customers.CustomerKey
GROUP BY
Customers.CustomerName
) AS SubQuery
WHERE
SubQuery.SumOfSales > 100
I am working on Power BI and querying the table from SQL Server.
I want to create the same dax query to retrieve customers and total sales for only the customers who bought morethan 100 euros. How to do the INNER JOIN in Power BI ?
DAX uses an automatic LEFT OUTER JOIN in the query whenever you use columns related to the primary table :
EVALUATE
FILTER (
SUMMARIZE (
YourTable,
YourTable[CustomerName],
"TotalOver100", SUM ( Sales[SalesAmount] )
),
[TotalOver100] > 100
Database is HP Vertica 7 or PostgreSQL 9.
create table test (
id int,
card_id int,
tran_dt date,
amount int
);
insert into test values (1, 1, '2017-07-06', 10);
insert into test values (2, 1, '2017-06-01', 20);
insert into test values (3, 1, '2017-05-01', 30);
insert into test values (4, 1, '2017-04-01', 40);
insert into test values (5, 2, '2017-07-04', 10);
Of the payment cards used in the last 1 day, what is the maximum amount charged on that card in the last 90 days.
select t.card_id, max(t2.amount) max
from test t
join test t2 on t2.card_id=t.card_id and t2.tran_dt>='2017-04-06'
where t.tran_dt>='2017-07-06'
group by t.card_id
order by t.card_id;
Results are correct
card_id max
------- ---
1 30
I want to rewrite the query into sql window functions.
select card_id, max(amount) over(partition by card_id order by tran_dt range between '60 days' preceding and current row) max
from test
where card_id in (select card_id from test where tran_dt>='2017-07-06')
order by card_id;
But result set does not match, how can this be done?
Test data here:
http://sqlfiddle.com/#!17/db317/1
I can't try PostgreSQL, but in Vertica, you can apply the ANSI standard OLAP window function.
But you'll need to nest two queries: The window function only returns sensible results if it has all rows that need to be evaluated in the result set.
But you only want the row from '2017-07-06' to be displayed.
So you'll have to filter for that date in an outer query:
WITH olap_output AS (
SELECT
card_id
, tran_dt
, MAX(amount) OVER (
PARTITION BY card_id
ORDER BY tran_dt
RANGE BETWEEN '90 DAYS' PRECEDING AND CURRENT ROW
) AS the_max
FROM test
)
SELECT
card_id
, the_max
FROM olap_output
WHERE tran_dt='2017-07-06'
;
card_id|the_max
1| 30
As far as I know, PostgreSQL Window function doesn't support bounded range preceding thus range between '90 days' preceding won't work. It does support bounded rows preceding such as rows between 90 preceding, but then you would need to assemble a time-series query similar to the following for the Window function to operate on the time-based rows:
SELECT c.card_id, t.amount, g.d as d_series
FROM generate_series(
'2017-04-06'::timestamp, '2017-07-06'::timestamp, '1 day'::interval
) g(d)
CROSS JOIN ( SELECT distinct card_id from test ) c
LEFT JOIN test t ON t.card_id = c.card_id and t.tran_dt = g.d
ORDER BY c.card_id, d_series
For what you need (based on your question description), I would stick to using group by.
Version: SQL Server 2014
Objective: Create a complete time series with existing date range records.
Initial Data Setup:
IF OBJECT_ID('tempdb..#DataSet') IS NOT NULL
DROP TABLE #DataSet;
CREATE TABLE #DataSet (
RowID INT
,StartDt DATETIME
,EndDt DATETIME
,Col1 FLOAT);
INSERT INTO #DataSet (
RowID
,StartDt
,EndDt
,Col1)
VALUES
(1234,'1/1/2016','12/31/2999',100)
,(1234,'7/23/2016','7/27/2016',90)
,(1234,'7/26/2016','7/31/2016',80)
,(1234,'10/1/2016','12/31/2999',75);
Desired Results:
RowID, StartDt, EndDt, Col1
1234, '01/01/2016', '07/22/2016', 100
1234, '07/23/2016', '07/26/2016', 90
1234, '07/26/2016', '07/31/2016', 80
1234, '08/01/2016', '09/30/2016', 100
1234, '10/01/2016', '12/31/2999', 75
Not an easy task I will admit, If anyone has a suggestion on how to tackle this utilizing SQL alone (Microsoft 2014 TSQL) I would greatly appreciate it. Please keep in mind it is SQL and we want to try to avoid cursors at all costs based on performance for large data sets.
Thanks in Advance.
Also as an FYI I was able to achieve half of this by utilizing a LEAD windows function to set the End Date of the current record to the Startdate-1 of the next. The other half of filling gaps back in from previous records still eludes me.
Updated for the 9/31 to 9/30 date.
The following query does essentially what you are asking. You can tweak it to fit your requirements. Note that when checking the results of my query, your desired results contain 09/31/2016 which is not a valid date.
WITH
RankedData AS
(
SELECT RowID, StartDt, EndDt, Col1,
DATEADD(day, -1, StartDt) AS PrevEndDt,
RANK() OVER(ORDER BY StartDt, EndDt, RowID) AS rank_no
FROM #DataSet
),
HasGapsData AS
(
SELECT a.RowID, a.StartDt,
CASE WHEN b.PrevEndDt <= a.EndDt THEN b.PrevEndDt ELSE a.EndDt END AS EndDt,
a.Col1, a.rank_no
FROM RankedData a
LEFT JOIN RankedData b ON a.rank_no = b.rank_no - 1
)
SELECT RowID, StartDt, EndDt, Col1
FROM HasGapsData
UNION ALL
SELECT a.RowID,
DATEADD(day, 1, a.EndDt) AS StartDt,
DATEADD(day, -1, b.StartDt) AS EndDt,
a.Col1
FROM HasGapsData a
INNER JOIN HasGapsData b ON a.rank_no = b.rank_no - 1
WHERE DATEDIFF(day, a.EndDt, b.StartDt) > 1
ORDER BY StartDt, EndDt;
I prepared the following report. It displays three columns (AccruedInterest, Tip & CardRevenue) by month for a given year. Now I want to "Rotate" the result so that StartDate value turn into 12 columns.
I have this:
I need this:
I have tried pivoting the table but I need multiple columns to be aggregated as you see.
You have to unpivot your data and the pivot.
Note: I put in my own values in your table as to make each unique so you know the data is correct.
--Create YourTable
SELECT * INTO YourTable
FROM
(
SELECT CAST('2015-01-01' AS DATE) StartDate,
607.834 AS AccruedInterest,
1 AS Tip,
3 AS CardRevenue
UNION ALL
SELECT CAST('2015-02-01' AS DATE) StartDate,
643.298 AS AccruedInterest,
16.8325 AS Tip,
5 AS CardRevenue
) A;
GO
--This pivots your data
SELECT *
FROM
(
--This unpivots your data using cross apply
SELECT col,val,StartDate
FROM YourTable
CROSS APPLY
(
SELECT 'AccruedInterest', CAST(AccruedInterest AS VARCHAR(100))
UNION ALL
SELECT 'Tip', CAST(Tip AS VARCHAR(100))
UNION ALL
SELECT 'CardRevenue', CAST(CardRevenue AS VARCHAR(100))
) A(col,val)
) B
PIVOT
(
MAX(val) FOR startdate IN([2015-01-01],[2015-02-01])
) pvt
Results:
col 2015-01-01 2015-02-01
AccruedInterest 607.834 643.298
CardRevenue 3 5
Tip 1.0000 16.8325
First, my knowledge of MDX is very limited
Basically, I have 2 queries; one with HQ sales per departments and one with stock room sales per departments. I would like to return the results of the department sales of the stock room followed by the department sales of the HQ in one go.
Thanks in advance.
In SQL, the query should be something like this:
SELECT StockRoom.Code, StockRoom.Sales, StockRoom.Department, StockRoom.HQ
, (SELECT SUM(HQ.Sales)
FROM StockRoom HQ
WHERE HQ.Department = StockRoom.Department
AND HQ.HQ = StockRoom.HQ) as HQSales
FROM StockRoom StockRoom
WHERE Week = 12
AND Year = 2011
AND Code = 'C001'
EDIT:
I'm working on a OLAP Cube using SQL Server Reporting Services (Visual Studio 2k8R2). Here is my current MDX query:
SELECT NON EMPTY { [Measures].[CATTC], [Measures].[CATTC N-1], [Measures].[PROG] }
ON COLUMNS
, NON EMPTY { ([Stock room].[Stock room class Enseigne].[Stock room class Enseigne].ALLMEMBERS * [Stock room].[Stock room geographical dynamic hierarchy].[Stock room].ALLMEMBERS * [Product].[Product Hierarchy].[Product main class level 3].ALLMEMBERS ) }
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM ( SELECT ( STRTOSET(#TimecalendarTimecalendarweekofyear, CONSTRAINED) )
ON COLUMNS FROM ( SELECT ( STRTOSET(#TimecalendarTimecalendaryear, CONSTRAINED) )
ON COLUMNS FROM ( SELECT ( STRTOSET(#StockroomStockroomcode, CONSTRAINED) )
ON COLUMNS FROM [CustomNextTest])))
WHERE ( IIF( STRTOSET(#StockroomStockroomcode, CONSTRAINED).Count = 1, STRTOSET(#StockroomStockroomcode, CONSTRAINED), [Stock room].[Stock room code].currentmember )
, IIF( STRTOSET(#TimecalendarTimecalendaryear, CONSTRAINED).Count = 1, STRTOSET(#TimecalendarTimecalendaryear, CONSTRAINED), [Time calendar].[Time calendar year].currentmember )
, IIF( STRTOSET(#TimecalendarTimecalendarweekofyear, CONSTRAINED).Count = 1, STRTOSET(#TimecalendarTimecalendarweekofyear, CONSTRAINED), [Time calendar].[Time calendar week of year].currentmember ) )
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS