How can I get the MDX query below to display the date next to the customer? Currently it only displays the customer, or whichever dimension I make first in the nonempty call. I'd like both to show in the results.
SELECT {[Measures].[Count1], [Measures].[Count2],
[Measures].[Count3]} ON COLUMNS,
nonempty({[Customers]}, {[DateRange]}) ON Rows
FROM Cube
I tried crossjoin but that return an out of memory error.
Thanks!
I am getting this:
Customer | Count1 | Count2 | Count3 |
I would like to see this:
Customer | Date | Count1 | Count2 | Count3 |
Generally a measure goes as the second argument of NONEMPTY
SELECT {[Measures].[Count1], [Measures].[Count2],
[Measures].[Count3]} ON COLUMNS,
nonempty(({[Customers]}, {[DateRange]}),{[Measures].[Count1], [Measures].[Count2],
[Measures].[Count3]}) ON Rows
FROM Cube
I put all three measures as part of the set for the NonEmpty function. This would return data where any row has at least one of the three counts that isn't null. It only filters out rows that are null for all three counts.
Related
I've been struggling with this for a few hours.
I am trying to build a simple table that will have the following:
Result of a search containing an id and a date
Count of a subquery using the id of the search in 1
I have two working queries that achieve what I want individually, but when I try to use a join I lose the data of the query for #2.
Query #1: index= source="" "" AND parentId=1574 | fields childId, date
-> returns 333,
Query #2: index= source="" "" AND childId=333 | chart count as childCount | fields childCount
-> returns the count, 4 for example
When I try to join the two using something like this, I lose the count:
index=<redacted> source="<redacted" "<some query text>" AND
parentId=1574
| fields childId, date
| join type=left childId [
search index=<redacted> source="<redacted>" "<some query text>" AND
childId=333 | chart count as childCount | fields childCount
]
| table artifactId, childCount, date
I've also tried an outer join, append, etc. to no avail. The count could be 0 as well.
Any help would be appreciated,
Thanks!
The data from the second search is lost because the search does not return the 'childId' field expected by the join. It returns only the 'childCount' field specified in the fields command. Try fields childId childCount.
I have a table like this:
I want to perform count on different set of columns (all subsets where there is at least one element from X and one element from Y). How can I do that in Postgres?
For example, I may have {x1,x2,y3}, {x4,y1,y2,y3},etc. I want to count number of "id"s having 1 in each set. So for the first set:
SELECT COUNT(id) FROM table WHERE x1=1 AND x2=1 AND x3=1;
and for the second set does the same:
SELECT COUNT(id) FROM table WHERE x4=1 AND y1=1 AND y2=1 AND y3=1;
Is it possible to write a loop that goes over all these sets and query the table accordingly? The array will have more than 10000 sets, so it cannot be done manually.
You should be able convert the table columns to an array using ARRAY[col1, col2,...], then use the array_positions function, setting the second parameter to be the value you're checking for. So, given your example above, this query:
SELECT id, array_positions(array[x1,x2,x3,x4,y1,y2,y3,y4], 1)
FROM tbl
ORDER BY id;
Will yield this result:
+----+-------------------+
| id | array_positions |
+----+-------------------+
| a | {1,4,5} |
| b | {1,2,4,7} |
| c | {1,2,3,4,6,7,8} |
+----+-------------------+
Here's a SQL Fiddle.
I already have the code to display the highest aggregate value for a ID.
select max(fk3_job_role_id),max(sum(no_of_placements))
from fact_accounts
group by fk3_job_role_id
the result looks like:
[max(fk3_job_role_id)] | [max(sum(no_of_placements))]
-----------------------|-----------------------------
5 | 25
However, i want to display the job_role_desc instead of fk3_job_role_id represented by the same id.
The table for it looks like:
[job_role_id] | [job_role_desc]
--------------------------------
1 | job1
2 | job2
3 | job3
4 | job4
5 | job5
select job_role_desc,T.total_sum from fact_accounts where job_role_id in (select max(fk3_job_role_id),max(sum(no_of_placements)) as total_sum from fact_accounts group by fk3_job_role_id) T
You need to query for the job description by using a subquery. The above query first fetches the data according to the query inside the brackets( also known popularly as a subquery ). The result returned from this query is used to compare with all the other id's in the main table by a simple "in" clause.
Edit
If you also need the sum of placements you can get it by using a reference to the table created during the execution of the subquery
I need to create a SQL query which calculates some data.
For instance, I have such SQL query:
SELECT SUM(AMOUNT) FROM FIRMS WHERE FIRM_ID IN(....) GROUP BY FIRM;
which produces such data:
28,740,573
30,849,923
25,665,724
43,223,313
34,334,534
35,102,286
38,556,820
19,384,871
Now, in a second column I need to show relation between one entry and sum of all entries. Like that:
28,740,573 | 0.1123
30,849,923 | 0.1206
25,665,724 | 0.1003
43,223,313 | 0.1689
34,334,534 | 0.1342
35,102,286 | 0.1372
38,556,820 | 0.1507
19,384,871 | 0.0758
For instance, sum of all entries from first column above is gonna be 255,858,044 and the value in a first entry, second cell is gonna be 28,740,573 / 255,858,044 = 0.1123. And same for each entry in a result.
How can I do that?
UPD: Thanks #a_horse_with_no_name, I forgot to DBMS. It's Oracle.
Most databases now support the ANSI standard window functions. So, you can do:
SELECT SUM(AMOUNT),
SUM(AMOUNT) / SUM(SUM(AMOUNT)) OVER () as ratio
FROM FIRMS
WHERE FIRM_ID IN (....)
GROUP BY FIRM;
Note: Some databases do integer division. So, if AMOUNT is an integer, then you need to convert to a non-integer number in these databases. One easy method is to multiple by 1.0.
For example...........
Database Table:
BatchID BatchName Chemical Value
--------------------------------------------------------
BI-1 BN-1 CH-1 1
BI-2 BN-2 CH-2 2
--------------------------------------------------------
I need to display following cube
BI-1 BI-2
BN-1 BN-2
-----------------------------------------
CH-1 1 null
------------------------------------------
CH-2 null 2
------------------------------------------
Here BI-1,BN-1 are two rows in a single columns i need to display chemical value as row of that.
What is query MDX query for this.
Could Please help me to solve this problem.
Thank You.
Create a cube with BatchID, Batchname and Chemical as dimensions and Value as measure.
Then use the following MDX code:
SELECT
Crossjoin(Crossjoin([BatchID].Members, [Batchname].Members), { [Measures].[Value] }) ON COLUMNS,
[Chemical].Members ON ROWS
FROM [Mycube]