This MDX request works:
SELECT [Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM [SalesAnalysis]
WHERE [Area].[Shanghai]
This one works too (different WHERE condition):
SELECT [Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM [SalesAnalysis]
WHERE EXCEPT([Product].[All Products].Children, {[Product].[All Products].[#null]})
Question: How do I write a request with both conditions?
i.e. area condition AND product condition
I tried , and AND but no luck so far.
I guess you need to define a set within your slicer:
SELECT [Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM [SalesAnalysis]
WHERE
[Area].[Shanghai]
* EXCEPT([Product].[All Products].Children
, {[Product].[All Products].[#null]})
Pay attention that MDX slicer is not a SQL WHERE statement; you might have a look to MDX sub-select instead.
USE * Instead of AND or ,.
It works
Your welcome
This should produce the results you need: (Sub-select)
SELECT [Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM (SELECT [Area].[Shanghai] on 0 from [SalesAnalysis])
WHERE EXCEPT([Product].[All Products].Children, {[Product].[All Products].[#null]})
Perhaps something like this would work
SELECT [Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM [SalesAnalysis]
WHERE
StripCalculatedMembers(CROSSJOIN(
{[Area].[Shangai]},
EXCEPT([Product].[All Products].Children, {[Product].[All Products].[#null]})))
SELECT
[Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM
[SalesAnalysis]
WHERE
{
[Product].[All Products].Children,
[Product].[All Products].[#null]
}
As mentioned above you can use * instead of AND.
Example:
select {[Measures].Members} ON COLUMNS,
{[Product].Members} ON ROWS
from [data_cube]
where {[Location].[New York] * [Time].[2015]}
In your case it should be something like this:
SELECT [Measures].salescount ON COLUMNS,
[Date].[2010] ON ROWS
FROM [SalesAnalysis]
WHERE EXCEPT([Product].[All Products].Children, {[Product].[All Products].[#null]}) * [Area].[Shanghai]
Related
I am currently trying to select multiple columns including note_id, notes_date_time, comments, DATALENGTH(comments) as 'note_length', from a table labelled job_notes.
However, I am only aiming to display the above when the length of characters within a comment is greater than the average length of the other comments. (The data type for comments is VARCHAR something).
I am also aiming to order by the length of the comments in descending order.
This is my code:
SELECT note_id, notes_date_time, comments, DATALENGTH(comments) AS 'note_length'
FROM job_notes
WHERE DATALENGTH(comments) > AVG(DATALENGTH(comments))
ORDER BY DATALENGTH(comments) DESC;
Upon execution, I am met with the following error message
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Any help would be greatly appreciated. Thanks!
You can try the below -
SELECT note_id, notes_date_time, comments, DATALENGTH(comments) AS 'note_length'
FROM job_notes
WHERE DATALENGTH(comments) > (select AVG(DATALENGTH(comments)) from job_notes)
ORDER BY DATALENGTH(comments) DESC
As the error states, you can't use aggregate functions in the WHERE, and the HAVING won't help you here. Personally, I would suggest using a CTE and a Windowed Aggregate function:
WITH CTE AS
(SELECT note_id,
notes_date_time,
comments,
DATALENGTH(comments) AS Note_Length, --Don't use single quotes for aliases, they are meant for literal strings
AVG(DATALENGTH(comments)) OVER () AS Avg_Note_Length
FROM dbo.job_notes)
SELECT note_id,
notes_date_time,
comments,
Note_Length --Don't forget to divide by 2 if you want characters and this is an nvarchar
FROM CTE
WHERE Note_Length > Avg_Note_Length;
I need to combine rows to merge into one single row of the data for the following scenario. would really appreciate any help here. I'm trying this in MSAccess
I would like result as 1 row which has earliest INSTOREDATE which is 21-Jan-19, total of TOTALBUY(10000+300+4000+2475=16,775). below is the result i am expecting
SELECT DISTINCT SEASON,
PHASE,
STYLEBR,
COLOURCODE,
INSTOREDATE,
TICKETRETAIL,
SUM(TOTALBUY) AS TOTAL
FROM YourTable
or
SELECT SEASON,
PHASE,
STYLEBR,
COLOURCODE,
INSTOREDATE,
TICKETRETAIL,
SUM(TOTALBUY) AS TOTAL
FROM YourTable
GROUP BY SEASON, PHASE, STYLEBR, COLOURCODE, INSTOREDATE, TICKETRETAIL
I have the following query:
select * from events order by Source, DateReceived
This gives me something like this:
I would like to get the results which i marked blue -> When there are two or more equal ErrorNr-Entries behind each other FROM THE SAME SOURCE.
So I have to compare every row with the row before. How can I achieve that?
This is what I want to get:
Apply the row number over partition by option on your table:
SELECT
ROW_NUMBER() OVER(PARTITION BY Source ORDER BY datereceived)
AS Row,
* FROM events
Either you can run a (max) having > 1 option on the result set's row number. Or if you need the details, apply the same query deducting the row nuber with 1.
Then you can make a join on the source and the row numbers and if the error nr is the same then you have a hit.
You can use the partition by as below.
select * from(select
*,row_number()over(partition by source,errornr order by Source, DateReceived) r
from
[yourtable])t
where r>1
You can specify your column names in the outer select.
I'm using the following query:
SELECT Policy_type_ID,Policy_Value Value,CASE Policy_Value WHEN max(Policy_Value) THEN 'Highest' WHEN min(Policy_Value) THEN 'Lowest' END AS Range
FROM Policy_Types
GROUP BY Policy_type_ID,Policy_Value
HAVING ((Policy_Value IN (SELECT max(Policy_Value)
FROM Policy_Types)) OR (Policy_Value IN(SELECT min(Policy_Value)
FROM Policy_Types)));
But the result has only one value 'Highest' in the column 'Range'.Its only regarding the first case,whichever it maybe, and ignoring the rest.
Policy_type_ID Value Range
501180 990000 Highest
690002 10 Highest
690006 10 Highest
690007 10 Highest
I've no idea where I'm going wrong. Its just that CASE statement that is the problem....any help??
The problem is that your MIN and MAX functions are being calculated within the GROUP BY groups, not across the entire table. You need to calculate them in a separate subquery that doesn't have GROUP BY.
SELECT DISTINCT Policy_type_ID, Policy_Value,
CASE Policy_Value
WHEN MaxPolicy THEN 'Highest'
ELSE 'Lowest'
END Range
FROM Policy_Types
JOIN (SELECT MIN(Policy_Value) MinPolicy, MAX(Policy_Value) MaxPolicy) MinMax
HAVING Policy_Value IN (MinPolicy, MaxPolicy)
Suppose I have an MDX query like this:
SELECT Measure1, Measure2, Measure3 ON COLUMNS
[Region].[Region].[Region] ON ROWS
FROM TheCube
If I wanted to exclude rows where ALL THREE measures are empty, I would use SELECT NON EMPTY, which works fast. But I actually need to exclude rows where both Measure1 and Measure2 are empty, even if Measure3 has a value - because in this particular cube Measure3 always has a value, so NON EMPTY has no effect at all.
I could do
SELECT Measure1, Measure2, Measure3 ON COLUMNS
FILTER ([Region].[Region].[Region],
NOT (IsEmpty(Measure1) AND IsEmpty(Measure2)) ON ROWS
FROM TheCube
and it even works, but it takes forever: an order of magnitude longer than the NON EMPTY query above. In fact, even if I filter by an expression that is always true, like FILTER(..., 1=1), it also takes a lot of time.
Is there a more efficient way to filter out rows where both Measure1 and Measure2 are empty?
I think you are looking for the similar function NonEmpty.
http://msdn.microsoft.com/en-us/library/ms145988.aspx
Here is a good explanation between them: http://thatmsftbiguy.com/nonemptymdx/
Just retyping the resulting query in a more readable manner:
SELECT Measure1, Measure2, Measure3 ON COLUMNS
NonEmpty([Region].[Region].[Region],
{ [Measure1], [Measure2] }) ON ROWS
WHERE -- some filter
If you don't use WHERE, you must be very careful to check what exactly your NonEmpty() runs on.