Problems when using RANKX on PowerPivot - mdx

I'm trying to use the RANKX funcion to Rank some sales, my table looks like this
ProductID | ProductTotal |
I want to have a third column called ProductRank that will rank the The products depending on the ProductTotal, this is what I have
=RANKX(ALL('Query'[ProductID]),'Query'[ProductTotal])
However, this is nor working :(
any help with this?
Thanks

If [ProductTotal] is a column then the following should work
=RANKX(ALL('Query'[ProductID]),CALCULATE(SUM('Query'[ProductTotal])))

Related

Convert column values into columns in oracle

I have data in sql table which looks something like this.
WORKFLOW_ID|SLOT_NAME |VALUE |PROCESS_ID|
-----------|--------------|-------------------|----------|
47|pm_id |6355 | 212331|
47|entry_id |7722 | 212331|
47|tecn_no |T0212331 | 212331|
47|issue_date |2020-10-24 | 212331|
47|entry_datetime|2020-10-20 15:09:22| 212331|
I want to convert SLOT_NAME column values into COLUMNS NAMES and VALUE column into the COLUMN VALUES. So the data will look something like this.
WORKFLOW_ID|SLOT_NAME |pm_id |entry_id| tecn_no| issue_date| entry_datetime| PROCESS_ID|
-----------|--------------|-------------------|--------|------------|-----------------|------------------------|--------------|
47|pm_id |6355 | 7722| T0212331| 2020-10-24| 2020-10-20 15:09:22| 212331|
Keeping in mind there might be more SLOT NAMES or rows.
You might want to explore the dynamic pivot function written by Anton from AMIS. The source can be found here
https://technology.amis.nl/wp-content/uploads/images/antonsPivoting.zip
and some samples of its use here
https://technology.amis.nl/it/dynamic-sql-pivoting-stealing-antons-thunder/
Internally it is executing a standard query against your data, and then fetching the results in order to build an ANYDATA table in order to give you the dynamic list of columns back.
If you're on a more recent release, you could look at polymorphic table functions to do it, which is covered here
https://blog.sqlora.com/en/dynamic-pivot-with-polymorphic-table-function/
and if you are really on a recent release, ie, 21c you could use some SQL macros to do it
https://blog.sqlora.com/en/dynamic-pivot-with-sql-macros-in-oracle-20c/

Access 2016 SQL: Find minimum absolute difference between two columns of different tables

I haven't been able to figure out exactly how to put together this SQL string. I'd really appreciate it if someone could help me out. I am using Access 2016, so please only provide answers that will work with Access. I have two queries that both have different fields except for one in common. I need to find the minimum absolute difference between the two similar columns. Then, I need to be able to pull the data from that corresponding record. For instance,
qry1.Col1 | qry1.Col2
-----------|-----------
10245.123 | Have
302044.31 | A
qry2.Col1 | qry2.Col2
----------------------
23451.321 | Great
345622.34 | Day
Find minimum absolute difference in a third query, qry3. For instance, Min(Abs(qry1!Col1 - qry2!Col1) I imagine it would produce one of these tables for each value in qry1.Col1. For the value 10245.123,
qry3.Col1
----------
13206.198
335377.217
Since 13206.198 is the minimum absolute difference, I want to pull the record corresponding to that from qry2 and associate it with the data from qry1 (I'm assuming this uses a JOIN). Resulting in a fourth query like this,
qry4.Col1 (qry1.Col1) | qry4.Col2 (qry1.Col2) | qry4.Col3 (qry2.Col2)
----------------------------------------------------------------------
10245.123 | Have | Great
302044.31 | A | Day
If this is all doable in one SQL string, that would be great. If a couple of steps are required, that's okay as well. I just would like to avoid having to time consumingly do this using loops and RecordSet.Findfirst in VBA.
You can use a correlated subquery:
select q1.*,
(select top 1 q2.col2
from qry2 as q2
order by abs(q2.col1 - q1.col1), q2.col2
) as qry2_col2
from qry1 as q1;

RDLC grouping rows

I'm trying to do a rdlc table/matrix gruoping by Id.
I have a DataSet which is returning:
Id | name | price | quantity | subtotal | comment
The comment column is optional,also it can have multiple comments per Id so it could return duplicate rows but with different comment column.
Then I want to display in a Table or matrix the data like this
Id | name | quantity | price | subtotal
comment
I mean adding additional rows per comment.
Is it possible to display rows in that format?
If so, What is the best way to achieve it?
This can easily be achieved by using RowGroups. For your example you'd want a stepped report.
Here is an excellent tutorial from MSDN that should be of use to you:
Create a Stepped Report (Report Builder and SSRS)

Calculate data in a second column using data from the first one

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.

How can i do this query in sql? count number of category

my table is:
name | category |
name01 category01
name02 category01
name03 category02
name04 category02
name05 category02
name06 category02
name07 category02
.....
I would like to count for each category the number of name
the output i woulk like is something like this (by the example above):
category | num
category01 2
category02 5
...
thanks to all that would to help me...
SELECT category, COUNT(*) FROM table GROUP BY category
Although count could be a bit slow on very big tables, so if you run a very big service or operate on a lot of data you might consider cache the result or denormalize the table. (However, these techniques assume a really big table)
this is very basic, but using a GROUP BY statement should give the desired result. i.e:
SELECT category, COUNT(*) FROM table GROUP BY category
Always use COUNT(1) for faster execution