I have a cube with several dimensions. I need (a measure, calculation, whatever...) to show last value of a column, assuming the table is sorted by another column.
Something like:
SELECT TOP 1 column_1
FROM table_1
WHERE «The user's selected dimentions will work as a filter»
ORDER BY column_2 DESC
You would possibly use something like
WITH Measures.[Top 1] AS
TopCount([Dimension1Name].[Attribute1Name].[Level1Name].Members, 1, [Dimension2Name].[Attribute2Name].[Level2Name].Members)
SELECT Measures.[Top 1] ON COLUMNS
FROM [CubeName]
where the names with 1 refer to your column_1, and those with 2 to your column_2.
Related
There is 1 record having duplicate values except in 1 column having x and y
record status
XXXXXXXXXX A
XXXXXXXXXX B
Need to pull A only and remove the other duplicate B
Select record
case
when status in ("'a', 'b'") then ('a')
from xyz
Let suppose you have data as below where Status is repeating for First column
but you are interesting in the status which is of having lower value as given below:
In this case following SQL may help. Here, we are partitioning on key field and ordering the Status so that we can apply filter on rank to get desired result.
WITH sampleData AS
(SELECT '1234' as Field1, 'A' as STATUS UNION ALL
SELECT '1234', 'C' UNION ALL
SELECT '5678', 'A' UNION ALL
SELECT '5678', 'B' )
select * except(rank) from (
select *, rank() over (partition by Field1 order by STATUS ASC) rank from sampleData)
where rank = 1
order by Field1
Consider below approach
select * from sampledata
qualify 1 = row_number() over win
window win as (partition by field1 order by if(status='A',1,2) )
if applied to sample data in your question - output is
I would like to calculate the mode/median or better, most frequent observation of a categorical variable within my query.
E.g, if the variable has the following string values:
dog, dog, dog, cat, cat and I want to get dog since its 3 vs 2.
Is there any function that does that? I tried APPX_MEDIAN() but it only returns the first 10 characters as median and I do not want that.
Also, I would like to get the most frequent observation with respect to date if there is a tie-break.
Thank you!
the most frequent observation is mode and you can calculate it like this.
Single value mode can be calculated like this on a value column. Get the count and pick up row with max count.
select count(*),value from mytable group by value order by 1 desc limit 1
now, in case you have multiple modes, you need to join back to the main table to find all matches.
select orig.value from
(select count(*) c, value v from mytable) orig
join (select count(*) cmode from mytable group by value order by 1 desc limit 1) cmode
ON orig.c= cmode.cmode
This will get all count of values and then match them based on count. Now, if one value of count matches to max count, you will get 1 row, if you have two value counts matches to max count, you will get 2 rows and so on.
Calculation of median is little tricky - and it will give you middle value. And its not most frequent one.
I have a table with a column UniqueID of type varchar. This column has unique IDs labeled as follows:
DU19F0001
DU19M001
DU19M002
DU19F002
EL19F001
EL19F002
MU19M001
MU19M002
I am trying to select for the last max value based on this mixed string. For instance what is the last value for 'DU' '19' 'F'? The result should then be DU19F002. How do I write a query for selecting the max value based on a mix of strings and integers in a column?
Is this what you want?
select max(uniqueid)
from t
where uniqueid like 'DU19F%';
If you want to do this for the first 5 (or whatever) characters, you can do this:
select
first_part,
max(uniqueid)
from
(
select substring(uniqueid,1,5) as first_part,uniqueid from <your table> ) t
group by first_part
I have a query on grouping which I need to do a quick fix on. I am at present grouping column A and counting the value in column B.
select
Column A,
Count ([Column B])
from table1
Group by Column A
The issue is that column A has some entries which are not standard for example.
ABC 100
ABC~ 3
BCA 120
BCA* 4
I need to blast the data to fix long term, but there are 3m rows, so not a quick job, as I need to create a mapping file to deal with the problem.
I currently get returned duplicate entries which is right in theory, but in practice I would like to group the ABC, by either trimming the column to only 3 characters or doing a right. However I have tried it in the select statement and it just removes the ~ or * entry and sums the standard ABC or BCA.
Have you tried something ???
select LEFT([Column A], 3),
Count ([Column B])
from table1
Group by LEFT([Column A], 3)
I have a table that looks like so:
Is there any way I can display it like this in SSRS?
1 2 3 4
1 2.091 0.918 0.9 1.718
2 0.647 0.964 0.6 2.264
3 0.804 0.789 0.6 1.9
I tried using a matrix but it stops after showing the first column and doesn't expand to show the rest.
Attempt:
Results:
Another attempt:
Results:
to get something similar to the result you're looking for you need to write a query like:
select id
,row_number() over(partition by SubscaleNumber order by SubscaleNumber ) SubscaleNumber
, item
from yourtable
then the matrix should work fine.
You need to set the Row and Column grouping to get this matrix-style output.
When you click in your table you should see the brackets for the Row and Column groups. If you don't have a column group, add one.
Go into the properties of each group and make sure they are grouped by the column you want.
Start from scratch with a table or matrix.
Add a Row group on id
Add a Column Group on Item.