Create dynamic column(s) which can move based on the Parameter Selection in SSRS - ssrs-2008

In my SSRS report layout, there is a parameter #GroupBy with four values: A, B, C and D.
In the table, there are four columns: Column AA, Column BB, Column CC and Column DD. I want to make this four columns dynamic. For examples,
when I select B in #GroupBy, the table shall be rearranged to this seq: BB, AA, CC, DD.
when I select C in #GroupBy, the table shall be rearranged to this seq: CC, DD, AA, BB. (special condition: CC must always be next to DD)
when I select D in #GroupBy, the table shall be rearranged to this seq: DD, CC, AA, BB. (special condition: CC must always be next to DD)
when I select A in #GroupBy, the table shall be rearranged to this seq: AA, BB, CC, DD. (back to the original seq)
Can someone help me?

First I would reorganise the dataset (hopefully it is SQL) so that there are 4 rows for each current row, with a GroupBy column containing A, B, C or D. In SQL this would need 3 UNION ALL clauses.
Then in the Tablix I would create a Column group on the GroupBy column.
Finally in the Sort for that Column Group, I would add an expression to test if the dataset value matches the parameter.

Related

JPA equivalent of PostgreSQL with null comparison

I have a requirement something like there are two db tables A and B.
Table A and table B both have column 'merit' of type integer. The requirement is to find the entries from table A those have merit that matches with the least merit number in table B.
If merit in B is NULL for all the entries, the query should result all the entries from table A.
If merit in B has valid numbers, the query should result the entries of A those match the least merit number in table B.
Sample data is like this::
TABLE A TABLE B
COL1 COL2 MERIT COL1 COL2 MERIT
a ab 1 c ac 1
b bc 2 d ad 3
From above data the least merit in B is 1 so, only the matching entry should result from Table A.
If merit column in B is null for all the entries ie. B has no valid number for merit, two entries from A should result.
So, I came up with the below sql query::
select A.* from A where A.merit IS NOT DISTINCT FROM (
select min(B.merit) from B where B.merit IS NOT NULL);
I am unable to write the JPA equivalent of this sql because of "IS NOT DISTINCT FROM".
The below queries are not working.
select a from A a where a.merit in (select min(b.merit) from B b where b.merit is not null)
select a from A a where a.merit = (select min(b.merit) from B b where b.merit is not null)
My environment is POSTGRESQL, HIBERNATE in QUARKUS.
Thanks for any suggestions.

IN with more than one column is possible?

I have this SQL
Select A, B, D from T
Where not exists (select F, S from Z)
Can I do the same thing using NOT IN for more than one column ?
Some databases (eg postgresql) have support for row values, where you can do something like ROW(x, y) NOT IN (select f, s from z), Firebird unfortunately does not have row values, so you cannot have more than one column in an IN (or NOT IN).
However you can usually emulate it with a correlated subquery in the exists, eg:
SELECT A, B, D
FROM T
WHERE NOT EXISTS (
SELECT 1
FROM Z
WHERE Z.F = T.X AND Z.S = T.Y
)
Note that EXISTS doesn't care about the selected values, but just that one or more rows were produced, so the two columns you use in your select within the EXISTS are not relevant (it could just as well have been 1 as I used above).

Add a new column dynamically in resultset

I have a query as below;
Existing Query - select A,B,C from table1.
Table2 has columns X,Y
The new query should have a new column(D) in the result-set. the value of D will be calculated based on column X.
D's calculation should be D = (C * X), Here to decide the row of column X from table2 -Y can be used in where condition. Y & A are not same but similar
I did not understand what did you mean by "Y & A are not same but similar". I assume Y and A can be used as joining keys. If so, the anwer would be:
SELECT T1.A,T1.B,T1.C,T1.C*T2.X AS D
FROM Table1 T1
JOIN Table2 T2 ON T1.A=T2.Y
I hope this helps!

Get field value from a record that causes an aggregate condition to be true

I have a table x which have the fields a, b, c, and d. I want to do a SELECT statement which is GROUPED BY a HAVING a_particular_value = ANY(array_agg(b)) and retrieves a, MIN(d), and c <- from which row is chosen by a_particular_value = ANY(array_agg(b)).
It's a bit confusing.
Lemme try to explain. a_particular_value = ANY(array_agg(b)) will choose some or one record from all records that is grouped by a. I want to retrieve the value of c from the record that causes the condition to be true. While NOT filter out other records because I still need those for the other aggregate function, MIN(d).
The query that I've tried to make:
SELECT a, MIN(d) FROM x
GROUP BY a
HAVING 1 = ANY(array_agg(b))
The only thing that's left to do is put c in the SELECT clause. How do I do this?
with agg as (
select a, min(d) as d
from x
group by a
having 1 = any(array_agg(b))
)
select distinct on (a, c)
a, c, d
from
x
inner join
agg using (a, d)
order by a, c
If min(d) is not unique within the a group then it is possible to exist more than one corresponding c. The above will return the smallest c. If you want the biggest do in instead
order by a, c desc
c can have various values in this scenario, so your only option is to group by c as well.
SELECT a, c FROM x
GROUP BY a, c
HAVING 1 = ANY(array_agg(b))
If you want to eliminate rows with b not satisfying condition before applying GROUP BY then use WHERE as documentation for HAVING says http://www.postgresql.org/docs/9.2/static/sql-select.html#SQL-HAVING

Browse two tables using a cursor

In my procedure I have two tables with the same data. I go through my first table through a cursor. Which compares with the second table that I find much the same data. What if, for example in my table_1 I have ten in my data and I have 12 data table2 how to detect missing data in my two table_1 which is traversed by the cursor?
Thx.
Sounds very much like you'd be better off using the MINUS operator.
SELECT a, b, c
FROM table1
MINUS
SELECT a, b, c
FROM table2
This will show you all results that exist in table1 which are not present in table2. In order to show discrepancies both ways, you could do something like this:
SELECT z.*, 'In table1, not in table2' problem_description
FROM (
SELECT a, b, c
FROM table1
MINUS
SELECT a, b, c
FROM table2
) z
UNION ALL
SELECT z.*, 'In table2, not in table1' problem_description
FROM (
SELECT a, b, c
FROM table2
MINUS
SELECT a, b, c
FROM table1
) z
SQL Fiddle for this answer