How to aggregate on a crossed join set with MDX - aggregate

I have a tuple set that I want to AGGREGATE .
For example:
I have this query:
Select {[Sales Territory].[Sales Territory].DEFAULTMEMBER.children} on 0,
{[Sales Reason].[Sales Reason Type].DEFAULTMEMBER.children} on 1
From [Adventure Works]
When I want to add a TOTAL for one of the axis I do this:
WITH MEMBER [Sales Territory].[Sales Territory].[Total X] as 'AGGREGATE(AXIS(0))'
Select {[Sales Territory].[Sales Territory].[Total X],[Sales Territory].[Sales Territory].DEFAULTMEMBER.children} on 0
,{[Sales Reason].[Sales Reason Type].DEFAULTMEMBER.children} on 1 From [Adventure Works]
Now, for now it's all good.
But when I'm trying to aggregate a cross join set it's failed.
Please take this query as an example:
Select {[Sales Territory].[Sales Territory].DEFAULTMEMBER.children} on 0
,{[Sales Reason].[Sales Reason Type].DEFAULTMEMBER.children}*{[Sales Channel].[Sales Channel].DEFAULTMEMBER.children} on 1
From [Adventure Works]
Please notice that I did a cross join between [Sales Reason].[Sales Reason Type] to [Sales Channel].[Sales Channel]
Is there a way to do a total on axis when there are more than one member?
Thanks!
P.S: I'm a newbie with MDX queries

You are doing things more complex than necessary. Your second query should be simplified to:
Select [Sales Territory].[Sales Territory].Members on 0,
[Sales Reason].[Sales Reason Type].DEFAULTMEMBER.children on 1
From [Adventure Works]
Please note that the All member is already a member of (nearly) all hierarchies in MDX. Thus, to include the All members for the third query, you could either write
Select {[Sales Territory].[Sales Territory].Members}
on 0,
{[Sales Reason].[Sales Reason Type].Members}
*
{[Sales Channel].[Sales Channel].Members}
on 1
From [Adventure Works]
or, if you really wanted to have the Total of the Axis only without the combinations of the Totals from one of the hierarchies with the other members of the other one, union two sets - one with the tuple containing of the All members, and one with the cross join of the other two hierarchies. I am using the short form of Union here - which is the + operator for sets:
Select {[Sales Territory].[Sales Territory].Members}
on 0,
{ ([Sales Reason].[Sales Reason Type].All, [Sales Channel].[Sales Channel].All) }
+
(
{[Sales Reason].[Sales Reason Type].[Sales Reason Type].Members}
*
{[Sales Channel].[Sales Channel].[Sales Reason Type].Members}
)
on 1
From [Adventure Works]

Related

How to get unique date from multiple dates based on condition in PostgreSQL?

How do I get unique dates from the table "timelog" for the dates where isParent is not 1.
In the table, 2022-01-10 should not come as the result as this date has isParent as 1.
So far, I have written query like this -
SELECT DISTINCT session::date
FROM timelog
WHERE id IN (SELECT id FROM timelog WHERE isParent = 0)
Obviously, this is not working as intended. What changes do I need to make in this query to make it work?
You can try to use condition aggregate function in HAVING, let your condition which didn't any isParent = 1 date on CASE WHEN
SELECT session::date
FROM timelog
GROUP BY session::date
HAVING COUNT(CASE WHEN isParent = 1 THEN 1 END) = 0
sqlfiddle
SELECT session::date
FROM timelog
GROUP BY session::date
HAVING COUNT(*) filter(where isParent = 1) = 0
SELECT session::date
FROM timelog
GROUP BY session::date
HAVING NOT ARRAY_AGG(isParent) && ARRAY[1]; -- NOT in array containing 1
Something along the lines of
SELECT DISTINCT session::date FROM timelog WHERE isParent = 0
should work. All you are looking for is unique dates where the parent is 0 Correct? No aggregation needs to be applied ?

select rows with 'where' that not match all condition

I need to create a statement that select all row that mach 3 conditions or more.
This statement will be used by a high-performance JS function.
A record have some informations: foo, bar, baz, qux and fum.
I thought of 2 ways to do that and I chose the one that seems to me the best but maybe there is better ?
The first one (the best I think)
The db run a statement where the where clause match 3 conditions or more
I mean that for example the DB must returns all the rows where 3 fields or more match.
Below there is an example of the query, is there a way do it ? I think this one is pretty ugly... And if I need to add some informations...
If more than 1 record the JS script determine the good one (iterate over the results)
Query example :
select * from MyTable
where (foo='foo1' and bar='bar1' and baz='baz1')
or (foo='foo1' and bar='bar1' and qux='qux1')
or (foo='foo1' and bar='bar1' and fum='fum1')
or (bar='bar1' and baz='baz1' and qux='qux1')
or (bar='bar1' and baz='baz1' and fum='fum1')
[other or for 3 conditions match ...]
or (foo='foo1' and bar='bar1' and baz='baz1' and qux='qux1')
or (foo='foo1' and bar='bar1' and baz='baz1' and fum='fum1')
or (foo='foo1' and bar='bar1' and qux='qux1' and fum='fum1')
[other or for 4 conditions match ...]
or (foo='foo1' and bar='bar1' and baz='baz1' and qux='qux1' and fum='fum1') /* 5 conditions match */
The other one (surely the worst I think) :
DB return all rows that match at one condition
then the JS script determine the good one by iterating over all the results
Query example :
select * from MyTable
where foo='foo1' or bar='baz1' or baz='baz1' or qux='qux1'
Do you agree that the first one gives the best performance? If yes is there a better query ?
demo:db<>fiddle
SELECT
t.*
FROM mytable t
WHERE
(foo IS NOT DISTINCT FROM 'foo1')::int +
(bar IS NOT DISTINCT FROM 'bar1')::int +
(baz IS NOT DISTINCT FROM 'baz1')::int +
(qux IS NOT DISTINCT FROM 'qux1')::int +
(fum IS NOT DISTINCT FROM 'fum1')::int >= 3
IS NOT DISTINCT FROM checks for equality and considers NULL
You can check below query
select * from
(
select a.*,
case when foo='foo1' then 1 else 0 end +
case when bar='baz1' then 1 else 0 end +
case when baz='baz1' then 1 else 0 end +
case when qux='qux1' then 1 else 0 end +
case when fum='fum1' then 1 else 0 end as total
from MyTable a
where foo='foo1' or bar='baz1' or baz='baz1' or qux='qux1' or fum = 'fum1'
) as a
where total>=3;

An axis number cannot be repeated in a query

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]
)

Simple SELECT, but adding JOIN returns too many rows

The query below returns 9,817 records. Now, I want to SELECT one more field from another table. See the 2 lines that are commented out, where I've simply selected this additional field and added a JOIN statement to bind this new columns. With these lines added, the query now returns 649,200 records and I can't figure out why! I guess something is wrong with my WHERE criteria in conjunction with the JOIN statement. Please help, thanks.
SELECT DISTINCT dbo.IMPORT_DOCUMENTS.ITEMID, BEGDOC, BATCHID
--, dbo.CATEGORY_COLLECTION_CATEGORY_RESULTS.CATEGORY_ID
FROM IMPORT_DOCUMENTS
--JOIN dbo.CATEGORY_COLLECTION_CATEGORY_RESULTS ON
dbo.CATEGORY_COLLECTION_CATEGORY_RESULTS.ITEMID = dbo.IMPORT_DOCUMENTS.ITEMID
WHERE (BATCHID LIKE 'IC0%' OR BATCHID LIKE 'LP0%')
AND dbo.IMPORT_DOCUMENTS.ITEMID IN
(SELECT dbo.CATEGORY_COLLECTION_CATEGORY_RESULTS.ITEMID FROM
CATEGORY_COLLECTION_CATEGORY_RESULTS
WHERE SCORE >= .7 AND SCORE <= .75 AND CATEGORY_ID IN(
SELECT CATEGORY_ID FROM CATEGORY_COLLECTION_CATS WHERE COLLECTION_ID IN (11,16))
AND Sample_Id > 0)
AND dbo.IMPORT_DOCUMENTS.ITEMID NOT IN
(SELECT ASSIGNMENT_FOLDER_DOCUMENTS.Item_Id FROM ASSIGNMENT_FOLDER_DOCUMENTS)
One possible reason is because one of your tables contains data at lower level, lower than your join key. For example, there may be multiple records per item id. The same item id is repeated X number of times. I would fix the query like the below. Without data knowledge, Try running the below modified query.... If output is not what you're looking for, convert it into SELECT Within a Select...
Hope this helps....
Try this SQL: SELECT DISTINCT a.ITEMID, a.BEGDOC, a.BATCHID, b.CATEGORY_ID FROM IMPORT_DOCUMENTS a JOIN (SELECT DISTINCT ITEMID FROM CATEGORY_COLLECTION_CATEGORY_RESULTS WHERE SCORE >= .7 AND SCORE <= .75 AND CATEGORY_ID IN (SELECT DISTINCT CATEGORY_ID FROM CATEGORY_COLLECTION_CATS WHERE COLLECTION_ID IN (11,16)) AND Sample_Id > 0) B ON a.ITEMID =b.ITEMID WHERE a.(a.BATCHID LIKE 'IC0%' OR a.BATCHID LIKE 'LP0%') AND a.ITEMID NOT IN (SELECT DIDTINCT Item_Id FROM ASSIGNMENT_FOLDER_DOCUMENTS)

Update Query will not run

When I run the query (1st Code) below I get 1.37 million random Departure Dates based on the current Arrival Date in the database, this is good news. However when I try to update the database with the 2nd Code query I get an error message(See below) and I don't know why. Can you help?
Msg 116, Level 16, State 1, Line 5 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
1st Code
SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay
ORDER BY ArrivalDate
2nd Code
USE Occupancy
Update Bookings
Set DepartureDate = (SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5
* LengthOfStay.LengthofStay, ArrivalDate))
FROM LengthOfStay, Bookings
You have several problems:
LengthOfStay, Bookings is a CROSS JOIN (Cartesian product): is this intended
You have 2 columns from the sub query but are trying to update only one
Assuming your CROSS JOIN is intended, you don't need the subquery
UPDATE
B
SET
DepartureDate = DATEADD(day,
1 + RAND(checksum(NEWID()))*1.5 * L.LengthofStay,
B.ArrivalDate)
FROM
LengthOfStay L, Bookings B
It seems you are selecting 2 columns to update 1 column(DepartureDate)