Repeating values in SSRS Cascading parameter - sql-server-2008

I have 4 parameters in my report and my requirement is user should be able to select all values from the parameter list.
So I have created cascading parameters but one of it shows multiple values corresponding to other parameters
eg:
A
A
A
B
B
B
B
C
C
Ideal: A
B
C
I tried by unchecking allow multiple values in Parameter properties.
Param3-> taking values from Dataset3(Col3)
Main Dataset:
SELECT Col1, Col2, Start_Date, End_Date, Col3
FROM Table
Start_Date IS NULL OR
Start_Date >= #StartDate)
AND (End_Date <= #EndDate)
AND (Col3 IN (#Param3))
Dataset 1:
SELECT DISTINCT Col1
FROM Table
Dataset 2:
SELECT DISTINCT Col2
FROM Table
WHERE (Col1IN (#Param1))
ORDER BY Col2
Dataset 3:
SELECT DISTINCT Col1, Col2, Col3
FROM Table
WHERE
(Col1 IN (#Param1))
AND (Col2 IN (#Param2))
Any inputs/ideas/suggestions if I can get only Distinct values in my parameter list instead of repeating values?

Your problem seems Dataset 3 is returning repeated values for Col3since DISTINCT clause is applied across every column you select in the query.
This is a valid return of your dataset 3
Col1 Col2 Col3
A A E
B D E
C C E
Note every row is different but Col3 has repeated values.
To get different values in your parameter you can create an additional dataset to populate Parameter3.
SELECT DISTINCT Col3
FROM Table
WHERE
(Col1 IN (#Param1))
AND (Col2 IN (#Param2))
Let me know if this helps you.

Related

MySQL: How to compare multiple field value of a row and get matched or lowest one?

I have a MySQL table (below):
I can fetch the min or max of the column values simply but my problem is:
If any 3 or more fields value matched then fetch that value (if 3 fields values matched more than one value then the lowest of those values), for row #1 2100 will be the output and for row#3 55 will be the output.
If no field value matched with a minimum of 3 fields the lowest value of the entire row will be fetched, for row# 2 the output will be 1900.
I can use IF (e.g. select if(col1 = col2, col1, col2) from table) in select but I didn't find a solution for this situation. Can anyone help to write a MySQL query for this?
Thanks in advance!
You can use this query to get the results you want. It uses two nested subqueries: the first unpivots the data into a single column; the second then counts how many of each column value occur for a given ID value. The outer query then tests the maximum of the counts; if it is >= 3 then the minimum value which has a count of 3 or more is chosen, otherwise the minimum column value is chosen as the minimum column:
SELECT ID,
CASE WHEN MAX(cnt) >= 3 THEN MIN(CASE WHEN cnt >= 3 THEN col END)
ELSE MIN(col)
END AS min_col
FROM (
SELECT ID, col, COUNT(col) AS cnt
FROM (SELECT ID, col1 AS col FROM data
UNION ALL
SELECT ID, col2 FROM data
UNION ALL
SELECT ID, col3 FROM data
UNION ALL
SELECT ID, col4 FROM data
UNION ALL
SELECT ID, col5 FROM data
UNION ALL
SELECT ID, col6 FROM data
UNION ALL
SELECT ID, col7 FROM data
) d
GROUP BY ID, col
) dd
GROUP BY ID
Output (for your sample data):
ID min_col
1 2100
2 1900
3 55
Demo on SQLFiddle

SQL to calculate sum of column values and repeat this value over all rows [duplicate]

This question already has answers here:
MySQL combining COUNT, MAX and SUM
(3 answers)
Closed 5 years ago.
Let's say I have a table like this:
COL1 COL2
ABC 1
DEF 2
GHI 3
And I want to have the sum of COL2 in a new column COL3:
COL1 COL2 COL3
ABC 1 6
DEF 2 6
GHI 3 6
So taking a simple sum won't work because it will return only 1 value, whereas I want the value to be repeated over all rows.
Try this:
SELECT *
,(SELECT SUM(Col2) FROM Your_Table) Col3
FROM Your_Table
Try this in SQL Server:
SELECT *
,SUM(Col2) OVER(ORDER BY(SELECT NULL)) Col3
FROM Your_Table
Since you didn't specify a dbms, this should work in most (all?) dbms's (tested in sql server):
DECLARE #T TABLE (Col1 VARCHAR(3), Col2 INT)
INSERT #T (Col1, Col2)
VALUES ( 'ABC' , 1 )
, ('DEF', 2)
, ('GHI', 3)
SELECT *
FROM #T
CROSS JOIN (
SELECT SUM(T.Col2) Col3
FROM #T AS T
) X
Though you didn't mention the Server Tag for SQL, I resolved it using MYSQL Server. Try this in MYSQL Server:
Select t.Col1, t.Col2, t1.Col3 from tbl as t join (Select Sum(Col2) as Col3 from tbl) as t1
See the demo here
In order to add the two columns you have to perform the joins query. Base on your operation rather you have to perform inner join or outer join .For more description you can see the respective databases

How to merge two tables without id and with different number of columns in MySQL

I have two tables with different columns. Tables doesn't have id column. They have the same number of rows. I want to merge them in new table. I've tried to do this like this:
CREATE TABLE test_3_cut_dest as
SELECT * FROM test_3_cut_
UNION
SELECT * FROM test_3_cut
but got error:
each UNION query must have the same number of columns
I want to know how achieve merging of two tables with different number of columns without specifying list of columns?
I don't think it works with select * when it comes to not knowing how many columns each table has. The best way to do it is like this:
SELECT A, B, C, D, E, F, G, H FROM test_3_cut_
UNION
SELECT A, B, NULL AS C, NULL AS D, NULL AS E, NULL AS ... FROM test_3_cut
I've done it like this considering test_3_cut has fewer columns than test_3_cut_
Union requires that all participating selects yield matching record sets, meaning same number of columns and compatible data types for each column.
As for your question, you can use placeholders wherever there is a mismatch. For instance:
SELECT Col1, Col2, 'DUMMYVALUE'
FROM Table1
UNION ALL
SELECT Col1, Col2, Col3
FROM Table2 ;
Now, if table1 has 3 textual columns and table2 has 6 textual columns, you can go with:
SELECT * , 'DUMMYVALUE1','DUMMYVALUE2','DUMMYVALUE3'
FROM Table1
UNION ALL
SELECT *
FROM Table2 ;
Union works like this:
Select column1 from table1
union
select column1 from table2
Number of columns must be same in both Select Queries.
You have to select from the two tables the same set of columns. If one of the tables has more columns, you can either select only the columns they share or select a fake value for the missing column from the table that has less.
Example
Table1
col1 | col2 | col3
Table2
col1 | col2
You can do this
select col1, col2 from Table1
union all
select col1, col2 from Table2
or this
select col1, col2, col3 from Table1
union all
select col1, col2, '' from Table2

Suming values according to different criteria

I want to sum value in the same column but depending on certain creteria in different columns i.e.
value column1 column2 column3
11 a x m
45 b y n
50 b z p
12 c x p
So e.g. I want the total sum for 'Value' when
column1 = b & column2 = z & column n
I used the following syntax:
sum(case when column1 = b & column2 = z & column n then value end) total
which worked but I am dealing with lot of columns, so is there an easier way to do this.
I was thinking loops, but can't make sense of loops in SQL.
SELECT SUM(value) FROM table GROUP BY col1, col2, col3
and if needed for a specific set you can use
SELECT SUM(value) FROM table GROUP BY col1, col2, col3 HAVING col1='a' AND col2='n' AND col3='c'

SQL - order by statement

I've got tables like:
table A
id name email
1 test1 ex#ex.com
2 test2 ex#ex.com
3 test3 ex#ex.com
4 test4 ....
5 test5 ....
table B
id catA catB year member
1 false true 2011 2
2 true false 2011 3
3 fals true 2010 5
And i want to get every row in table A and sort it by following:
FIRST, get user 2 (current year, based on table B)
SECOND, get user 3 (current year, based on table B)
after that get users that is in table B
after that get all other users.
I know that i can have specific sql for getting the first two users, and the just the
rest. But shouldnt i be able to get them all with a nice ORDER by statement? Like limiting the first order statement to just affect the first row...
Something like this?
select A.id, A.name, A.email, B.catA, B.catB, B.year
from A
join B on A.id = B.member
ORDER BY B.year DESC, (B.member IS NOT NULL) DESC
First sort all results by the year field in table B, which gets you 2011, 2010, etc... Any members who are NOT listed in table B will have a null year and sort to the bottom of the list. Next sort by B.member not being null - mysql will coerce this boolean result into an integer 1 or 0, which can be sorted, So sort descending to make all the 1's (not null B.members) sort first.
Based on your sorting rules:
after that get users that is in table B
after that get all other users.
I assume that there are some users in table A that do not exist in B, so you'd want to use a LEFT JOIN.
SELECT a.id, a.name, a.email
FROM a
LEFT JOIN b
ON a.id = b.member
ORDER BY ISNULL(b.year), b.year DESC, a.name
You can always 'order by', however grotesquely complicated your select is, buy putting the data-gathering elements into a derived table, selecting from it, and ordering by the results. Something like...
SELECT col1,
col2,
col3
FROM
(SELECT 1 as col1,
col2,
' ' AS col3
FROM blah...blah...blah
UNION
SELECT 2 AS col1
col2,
col3
FROM blah...blah...blah
UNION
and so on) myderivedtable
ORDER BY col1,
col2,
col3
You just have to make sure all the columns you are selecting in each of the queries come out the same, or cope with a NULL value otherwise.