Let's say I have two simple dimensions:
Products - with id and name
Salesmen - with id and name
My fact table is named SALES and contains the ids of the abovementioned.
Let's say product X has been sold by salesmen A, B and C.
Product Y has been sold by salesmen B, C, and D
I want to produce a MDX query which would tell me the names of the salesmen who sold both those products. In this case the result would be B and C
My attempt:
select {null} on 0,
DESCENDANTS (
[Salesmen].[Name].children
) on 1
FROM [Test]
where (
{
(
[Products].[Name].&[X]
)
,
(
[Products].[Name].&[Y]
)
}
)
Please try nesting exist function like this:
SELECT
{} on 0,
EXISTS(
EXISTS(
{[Salesmen].[Name].MEMBERS}, //<<TRY THIS INSTEAD
{[Products].[Name].&[X]}
)
,{[Products].[Name].&[Y]}
)
ON 1
FROM [Test];
Strictly speaking EXISTS requires the name of a measures group as it's third argument like this:
SELECT
{} on 0,
EXISTS(
EXISTS(
{[Salesmen].[Name].MEMBERS} //<<TRY THIS INSTEAD
,{[Products].[Name].&[X]}
,"Reseller Sales" //<<replace with group name from your cube
)
,{[Products].[Name].&[Y]}
,"Reseller Sales" //<<replace with group name from your cube
)
ON 1
FROM [Test];
An alternative approach is to use a member from the hierarchy [Measures] and the functions NonEmpty and Intersect:
SELECT
{} on 0,
INTERSECT(
NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[X],[Measures].[SomeMeasureInYourCube])
)
,NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[Y],[Measures].[SomeMeasureInYourCube])
)
)
ON 1
FROM [Test];
The above may well work with a simple tuple of the members of Products only
SELECT
{} on 0,
INTERSECT(
NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[X])
)
,NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[Y])
)
)
ON 1
FROM [Test];
Related
I have one dataset, and am trying to list all of the combinations of said dataset. However, I am unable to figure out how to include the combinations that are null. For example, Longitudinal? can be no and cohort can be 11-20, however for Region 1, there were no patients of that age in that region. How can I show a 0 for the count?
Here is the code:
SELECT "s_safe_005prod"."ig_eligi_group1"."site_name" AS "Site Name",
"s_safe_005prod"."ig_eligi_group1"."il_eligi_ellong" AS "Longitudinal?",
"s_safe_005prod"."ig_eligi_group1"."il_eligi_elcohort" AS "Cohort",
count(*) AS "count"
FROM "s_safe_005prod"."ig_eligi_group1"
GROUP BY "s_safe_005prod"."ig_eligi_group1"."site_name",
"s_safe_005prod"."ig_eligi_group1"."il_eligi_ellong",
"s_safe_005prod"."ig_eligi_group1"."il_eligi_elcohort"
ORDER BY "s_safe_005prod"."ig_eligi_group1"."site_name",
"s_safe_005prod"."ig_eligi_group1"."il_eligi_ellong" ASC,
"s_safe_005prod"."ig_eligi_group1"."il_eligi_elcohort" ASC
Create a cross join across the unique values from each of the three grouping fields to create a set of all possible combinations. Then left join that to the counts you have originally and coalesce null values to zero.
WITH groups AS
(
SELECT a.site_name, b.longitudinal, c.cohort
FROM (SELECT DISTINCT site_name FROM s_safe_005prod.ig_eligi_group1) a,
(SELECT DISTINCT il_eligi_ellong AS longitudinal FROM s_safe_005prod.ig_eligi_group1) b,
(SELECT DISTINCT il_eligi_elcohort AS cohort FROM s_safe_005prod.ig_eligi_group1) c
),
dat AS
(
SELECT site_name,
il_eligi_ellong AS longitudinal,
il_eligi_elcohort AS cohort,
count(*) AS "count"
FROM s_safe_005prod.ig_eligi_group1
GROUP BY site_name,
il_eligi_ellong,
il_eligi_elcohort
)
SELECT groups.site_name,
groups.longitudinal,
groups.cohort,
COALESCE(dat.[count],0) AS "count"
FROM groups
LEFT JOIN dat ON groups.site_name = dat.site_name
AND groups.longitudinal = dat.longitudinal
AND groups.cohort = dat.cohort;
I am new to DAX.
Let's pretend I have a table that looks like this:
Table A:
status delivered sold
late 10 50
late 20 300
early 5 500
Let's pretend I am using this SQL query:
with cte_1 as (
select
status, count(*) as [row_count]
from [table a]
group by [status]
having count(*) > 1
)
select *
from [table a] as p1
inner join [cte_1] as p2
on p1.[status] = p2.[status]
What would be the dax equivalent of this?
The SQL query return the Table A rows with the status that occurrs at least twice in the table, adding the count of the number of rows with the same status. In Power BI we can write a calculated table that adds the count of the rows of the same status and then filter out those with a count less than 2
Result =
FILTER(
ADDCOLUMNS(
'Table A',
"row_count",
CALCULATE(
COUNTROWS( 'Table A' ),
ALLEXCEPT( 'Table A', 'Table A'[Status] )
)
),
[row_count] > 1
)
I want to list out all cars from Dimension.Car which has a status IsPremium = 1 from cube.
And also, a separate list of all cars from Dimension.Car which has status IsFourWeeler = 1
Dimension has attributes as follows:
1. Car Code
2. Car Name
3. IsPremium
4. IsFourWheeler
If they are different hierachies (either attribute or user hierarchies) in the same dimension then you can use the function EXISTS.
Defined here: https://msdn.microsoft.com/en-us/library/ms144936.aspx?f=255&MSPPError=-2147217396
The example they show is the same as your situation:
SELECT
[Measures].[Internet Sales Amount] ON 0,
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Customer].[State-Province].&[CA]&[US]}
) ON 1
FROM [Adventure Works];
But you have
SELECT
[Measures].[SomeMeasuresInCube] ON 0,
EXISTS(
[Dimension.Car].[Car].MEMBERS
, [[Dimension.Car]].[IsPremium].[1]
) ON 1
FROM [YourCube];
NonEmpty could also be your friend:
WITH SET [SpecialCars] AS
NONEMPTY(
[Dimension.Car].[Car].MEMBERS
,([[Dimension.Car]].[IsFourWheeler].[1])
)
SELECT
[Measures].[SomeMeasuresInCube] ON 0,
[SpecialCars] ON 1
FROM [YourCube];
Employees of the company are divided into categories A, B and C regardless of the division they work in (Finance, HR, Sales...)
How can I write a query (Access 2010) in order to retrieve the number of employees for each category and each division?
The final output will be an excel sheet where the company divisions will be in column A, Category A in column B, category B in column and category C in column D.
I thought an IIF() nested in a COUNT() would do the job but it actually counts the total number of employees instead of giving the breakdown by category.
Any idea?
SELECT
tblAssssDB.[Division:],
COUNT( IIF( [Category] = "A", 1, 0 ) ) AS Count_A,
COUNT( IIF( [Category] = "B", 1, 0 ) ) AS Count_B,
COUNT( IIF( [ET Outcome] = "C", 1, 0 ) ) AS Count_C
FROM
tblAssssDB
GROUP BY
tblAssssDB.[Division:];
My aim is to code a single sql statement and avoid writing sub-queries in order to calculate the values for each division.
Count counts every non-Null value ... so you're counting 1 for each row regardless of the [Category] value.
If you want to stick with Count ...
Count(IIf([Category]="A",1,Null))
Otherwise switch to Sum ...
Sum(IIf([Category]="A",1,0))
Use GROUP BY instead of IIF. Try this:
SELECT [Division:], [Category], Count([Category]) AS Category_Count
FROM tblAssssDB
GROUP BY [Division:], [Category];
Try this Count:
Count(IIf([Field1]="N",1))+Count(IIf([Field2]="N",1)) ...
I grouped my qry and place Expression under this Count field I created. It worked for me
Select count(iif(fieldname='a',1,null)) as asde
from [table name]
where .....
I'm trying to select max(count of rows).
Here is my 2 variants of SELECT
SELECT MAX(COUNT_OF_ENROLEES_BY_SPEC) FROM
(SELECT D.SPECCODE, COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
FROM DECLARER D
GROUP BY D.SPECCODE
);
SELECT S.NAME, MAX(D.ENROLEECODE)
FROM SPECIALIZATION S
CROSS JOIN DECLARER D WHERE S.SPECCODE = D.SPECCODE
GROUP BY S.NAME
HAVING MAX(D.ENROLEECODE) =
( SELECT MAX(COUNT_OF_ENROLEES_BY_SPEC) FROM
( SELECT D.SPECCODE, COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
FROM DECLARER D
GROUP BY D.SPECCODE
)
);
The first one is working OK, but I want to rewrite it using "HAVING" like in my second variant and add there one more column. But now 2nd variant don't output any data in results, just empty columns.
How can I fix it ? Thank YOU!)
This query based on description given in comments and some suggestions, so it may be wrong:
select -- 4. Join selected codes with specializations
S.Name,
selected_codes.spec_code,
selected_codes.count_of_enrolees_by_spec
from
specialization S,
(
select -- 3. Filter records with maximum popularity only
spec_code,
count_of_enrolees_by_spec
from (
select -- 2. Count maximum popularity in separate column
spec_code,
count_of_enrolees_by_spec,
max(count_of_enrolees_by_spec) over (partition by null) max_count
from (
SELECT -- 1. Get list of declarations and count popularity
D.SPECCODE AS SPEC_CODE,
COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
FROM DECLARER D
GROUP BY D.SPECCODE
)
)
where count_of_enrolees_by_spec = max_count
)
selected_codes
where
S.SPECCODE = selected_codes.spec_code
Also query not tested and some syntax errors are possible.