Hello I am trying to find min date value between multiple columns in ssrs.
For example If date value in column A is less then in column B and column B is not nothing and column A is less then column C and column C is not nothing then chose field A
If Not Isnothing(A) and Not Isnothing(B) and A<B and A<C then
A
else if Not Isnothing(B) and Not Isnothing(C) and b<A and B<C
B
...
end if
I will be very grateful if you help me.
I answered a question recently that had a similar answer here..
How to set dynamic axis unit in SSRS Reports??
However, if possible I would so this in your dataset query it will be much easier as you can simply do..
SELECT MIN(myField) FROM
(SELECT A as myField FROM myTable
UNION
SELECT B FROM myTable
UNION
SELECT C FROM myTable
) x
Related
I want to write a SSRS expression that will allow me to grab the value from Column B base on the max value from ColumnA.
For Example, I have the following values
ColumnA
ColumnB
1
Test
2
Tester
3
Testing
=FIRST(
iif(
Fields!ColumnA.Value= MAX(Fields!ColumnA.Value,"test"),
Fields!ColumnB.Value,0
),"test"
)
The reason I am doing this is because I am trying to combine to datasets in one table. Certain fields in the table just needs to select top N values from another dataset.
I think that easiest way is to add something like row_number into your sql query (row_number() over (order by ColumnA desc) rn and then you can have condition iif(fields!rn.value = 1, fields!ColumnB.value,0)
I am fairly inexperienced with SQL, and I have a table that looks like this (simplified version):
ID | Dataset | date | value
I'm trying to divide each value by a baseline, which would be the first entry in the database for that particular dataset.
For example, for dataset1, if the value at 05/05/2018 is 28, and the first value in the database is 4 at 01/01/2018, then I want the result to be 28/4.
thing is that not every dataset was added to the database at the same time, so they have different dates for their baseline. So if dataset1 has its first entry at 01/01/2018, dataset2 might have its first entry at 02/02/2018.
How would I go about this query? I tried a simple div, but it seems like I can only divide by a single number, and not a value-by-value table.
I tried something like this:
SELECT DATE(date) as time, value, dataset / (
SELECT min(DATE(date)) as time, value, dataset FROM table GROUP BY dataset)
FROM table GROUP BY time, dataset
but I think SQL expects the denominator to be a single value in this case, not a table.
Check this out:
SELECT T1.Date
,T1.Value/T2.Value Value
FROM TempTable T1
INNER JOIN TempTable T2
ON T2.Id
=
(SELECT T2.Id
FROM TempTable T2
WHERE T1.Dataset=T2.Dataset
ORDER BY T2.Date
LIMIT 1
)
"I think SQL expects the denominator to be a single value in this case, not a table." You are on the right track, so the sub select needs to return a single value, below is an example:
SELECT
value, (select value from table b where a.dataset = b.dataset and b.dt = (select min(dt) from table c where b.dataset = c.dataset))
FROM
table a
In MySQL 8+, you would simply do:
select t.*,
t.value / first_value(t.value) over (partition by t.dataset order by t.date) as ratio
from t;
Say, I have a table
A B C D E F
1 2 4 3 6 5
4 2 3 1 6 5
4 5 3 6 1 2
How can one get an output based on rearranging based on its data. For example,
ABDCFE
DBCAFE
EFCABD
is it possible?
EDIT:
The question seems to be asking: How can I get the list of column names in order by value?
I got it. You want to sort the values in each row and show the names of the columns in order.
Let me assume that you have a row id, so you can identify each row. Then:
select id, group_concat(which order by val) as ordered_column_names
from ((select id, a as val, 'A' as which from t) union all
(select id, b, 'B' as which from t) union all
(select id, c, 'C' as which from t) union all
(select id, d, 'D' as which from t) union all
(select id, e, 'E' as which from t) union all
(select id, f, 'F' as which from t)
) t
group by id
order by id;
SQL is fundamentally not the tool to do the operation you describe, because it violates the concept of a relation. I don't mean the common use of "relation" meaning a relationship, I mean the mathematical definition of relation.
There is no order of columns in a relation. The columns are a set, which is by definition unordered. Columns are identified by their name, not their position left-to-right.
All the entries in rows under each respective named column must be part of the same data domain. If you mix them around on a row-by-row basis, you're violating this.
I guess all your columns A through F are actually using values in the same data domain, or else reordering them wouldn't make any sense. If this is true, then you're violating First Normal Form by defining a table with repeating groups of columns. You should instead have all six columns be in one column of a second table. Then it becomes very easy to sort them by value.
Basically, what you're trying to do is better solved by formatting the data results in some application code.
There is a way to do it ,get coulmn name by column ordinal order and print it.
For each value in coulmn iterate this and get the column name for the ordinal specified in cloumn data. Here ordinal position is value in each coulmn data. Iterate for each row and each column and your problem is solved.
select column_name
from information_schema.columns
where table_name = 'my_table_name' and ordinal_position = 2;
It appears that you are asking for output where each row in the output is just a specification of the order of the data values in the columns.
Then, if the values are always integers between 1 and 5, you can do it by outputting a character value of 'A' where the data value is 1, a 'B' where the data value is 2, etc. This SQL will do that.
Select Char(A+64)A, Char(B+64) B,
Char(C+64) C, Char(D+64) D,
Char(E+64) E, Char(F+64) F
From table
if the want the column sort order in one output column, you could also do this:
Select Char(A+64) + Char(B+64) +
Char(C+64) + Char(D+64) +
Char(E+64) + Char(F+64) SortOrder
From table
I want to add Multiple AND conditions in SQL query as Follows...
SELECT CONSUMER_NUMBER
FROM CONSUMER_INFO
WHERE YEAR=2014
AND CONT='USA'
AND ROWNUM=1;
But it does not work...How can I achieve this?
I assume YEAR is an Integer number, best practice would be to have a column type as data, and get the year using the YEAR function in SQL.
SELECT CONSUMER_NUMBER
FROM CONSUMER_INFO
WHERE YEAR(Data) =2014
AND CONT = 'USA'
AND ROWNUM = 1 ;
Something like this, but anyway I don't think this is the problem with your query, can you prinscreen the error?
PS : Also if your YEAR column is varchar or anything like that don't forget you have to use '' ;
This and conditions are right. Do you have column ROWNUM on your table. And what is datatype of YEAR column. You can show me your query errors?
I have following table structure (Table Name is Questions)
c1 c2 c3 Selection
X Y Z 2
A B C 3
Here c1,c2,c3 and Selection are column names. I want to retrieve value of c1 or c2 or c3 on the basis of value of column Selection.
Eg. If Selection value is 2 then I want corresponding value of c2 column i.e. Y.
If Selection value is 3 then it should select value of c3 column which is C here.
Please help me in forming Select Sql query here. I tried myself but was not able to find correct solution.
Thanks in advance
SELECT CASE Selection
WHEN 1 THEN c1
WHEN 2 THEN c2
ELSE c3
END val
FROM tableName
you have to use CASE's or IF's
select if(selection=2 ,b,if(selection=3,c,a)) from Table1 ;
see here for example ... link
This is very simple just run a simple query and it will return all the columns then you should use the appropriate column.
SELECT * FROM mytable WHERE selection = 'yourvalue'