Manually pass multiple integer values to a SSRS sureport - tsql

I'm trying to pass multiple integer values to a subreport, in order to use the following SQL request :
SELECT *
FROM Table as T
WHERE Code IN (#Code)
I want to pass 3 integer values to #Code: 1,2 and 3
I tried to use various combination of Split() and Join(), but none worked.

You don't need to do anything. If your parameter is set to be multi-value and takes it's values from a query or list of integers then SSRS will automatically inject a comma separated list of values into your main dataset query.
In your case if values 1, 2 & 3 were selected and you main dataset looked like your example
SELECT *
FROM Table as T
WHERE Code IN (#Code)
then what actually gets passed to the server would be this..
SELECT *
FROM Table as T
WHERE Code IN (1,2,3)
There is no need to do JOINS or SPLITS and no need to change dataset parameters. It will just work.

When I use a multi-value parameter in SSRS I have to use a table-valued function in the query. Essentially, it turns your parameter into a table that you can INNER JOIN on. For example:
SELECT *
FROM Table as T
INNER JOIN tablevaluefuntion(#Code,',') as P--the ',' is the delimiter from your list
WHERE t.code = p.value

Related

Snowflake invalid identifier when performin a join

I have been trying to do an outer join across two different tables in two different schemas. I am trying to filter out before from the table variants the character that are smaller than 4 and bigger than 5 digits. The join was not working with a simply where clause in the end, hence this decision.
The problem is if I do not put the quotes, Snowflake will say that I put invalid identifiers. However, when I run this with the quotes, it works but I get as values in the fields of the column raw.stitch_heroku.spree_variants.SKU only named as the column name, all across the table!
SELECT
analytics.dbt_lcasucci.product_category.product_description,
'raw.stitch_heroku.spree_variants.SKU'
FROM analytics.dbt_lcasucci.product_category
LEFT JOIN (
SELECT * FROM raw.stitch_heroku.spree_variants
WHERE LENGTH('raw.stitch_heroku.spree_variants.SKU')<=5
and LENGTH('raw.stitch_heroku.spree_variants.SKU')>=4
) ON 'analytics.dbt_lcasucci.product_category.product_id'
= 'raw.stitch_heroku.spree_variants.SKU'
Is there a way to work this around? I am confused and have not found this issue on forums yet!
thx in advance
firstly single quote define a string literal 'this is text' where as double quotes are table/column names "this_is_a_table_name"
add alias's to the tables makes the SQL more readable, and the duplicate length command can be reduced with a between, thus this should work better:
SELECT pc.product_description,
sp.SKU
FROM analytics.dbt_lcasucci.product_category AS PC
LEFT JOIN (
SELECT SKU
FROM raw.stitch_heroku.spree_variants
WHERE LENGTH(SKU) BETWEEN 4 AND 5
) AS sp
ON pc.product_id = sp.SKU;
So I reduced the sub-selects results as you only used sku from sp but given you are comparing product_id to sku as your example exists you don't need to join to sp.
the invalid indentifiers implies to me something is named incorrectly, the first step there is to check the tables exist and the columns are named as you expect and the type of the columns are the same for the JOIN x ON y clause via:
describe table analytics.dbt_lcasucci.product_category;
describe table raw.stitch_heroku.spree_variants;

Postgresql parse string values to integers

Is there a way in Postgres I can parse string values to integers? Basically I'm trying to query one table (let's call it table_one) using values from another table (table_two) in a character varying column.
Say SELECT char_column FROM table_two results in "2,4,6,8", I'd like to use this result in a second query as;
SELECT column FROM table_one WHERE some_id IN (2,4,6,8)
How can I get the string "2,4,6,8" to values 2,4,6,8 so as to be able to use it in the second query?
I've tried casting and to_number functions to no success.
SELECT column
FROM table
WHERE other_column = ANY(string_to_array('2,4,6,8', ',')::INT[])
Please try this:
SELECT column FROM table WHERE other_column IN (
SELECT NULLIF(i,'')::int
FROM regexp_split_to_tables('2,4,6,8',',') t(i)
)
Explanation:
The part regexp_split_to_tables('2,4,6,8',',') will split the string into a table. Then you cast it into integer.
Hopefully it will help you.

SSRS Parameter to select a column name on value which to filter the dataset

I need to allow my user to select a column name and its value from a parameter (or two params) to filter the result.
I have a text parameter with a few column names that are listed in my dataset
Column1, Column2, Column3. Each of those columns has only two values 1 and 0.
I would love some help in getting an idea how to filter my dataset based on the column name listed in the parameter and a selected value (1 or 0)
I assume it has to be related to a dynamic sql but, not sure how to incorporate that in either the WHERE clause or the actual dataset filter.
Thanks for any points guys!! :)
In the tablix you can use the filter section to set the criteria
col1 = param1.This will only select the rows that match the value of the parameter.
https://www.mssqltips.com/sqlservertip/2597/dataset-and-tablix-filtering-in-sql-server-reporting-services/

How to return a comma separated string using Crystal SQL Expression

I want to display a string on each row (Details section) in my Crystal Report. The contents of this string will be retrieved with the help of a SQL Expression.
The SQL I have is follows: However if multiple rows are returned, I am not sure how to convert that into a Comma Separated String. I have an Oracle 11g database.
(select distinct NAME from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"')
The TEST Table looks like this:
My report will be filtered for all samples with a specific test (e.g. Calcium). For those samples on the report, My SQL Expression should retrieve all "Other" Tests on the sample. See output example.
You can accomplish this with a wm_concat. WM_CONCAT takes a bunch of rows in a group and outputs a comma separated varchar.
Using the substr function you can separate the first result with the last.
Please note that I am dirty coding this (without a compiler to check my syntax) so things may not be 100% correct.
select sample_number
, substr(wm_concat(name),1,instr(wm_concat(name),",")-1) as NAME
, substr(wm_concat(name),instr(wm_concat(name),","),length(wm_concat(name)-instr(wm_concat(name),",")+1) as OTHER_TEST_NAMES
from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"'
and rownum < 2
group by sample_number
However, if it is not necessary to separate the name and the other test names, it actually is much simpler.
select sample_number
, wm_concat(name) as NAMES
from TEST
where SAMPLE_NUMBER = "TEST"."SAMPLE_NUMBER"
and X_BENCH <> '"TEST"."X_BENCH"'
and rownum < 2
group by sample_number
Also please try to organize your lines to make it easier to read.
You can use LISTAGG for Converting Rows to Comma-Separated String in Oracle.
Example:
SELECT user_id
, LISTAGG(expertise, ',')
WITHIN GROUP (ORDER BY expertise)
AS expertise
FROM TEMP_TABLE
GROUP BY user_id;

Mentioning column names in select for derived tables

i have doubt,
select *
from
(
select *
(
select User_Id,User_Name,Password
from <table> T
where IsActive = 1
) k
) m
in this case, is it required to mention column names in other 2 select statements,
mentioning columns is always better than keeping *
but,what is the use actually in above top 2 selects as we are getting selected columns from derived tables..
There's no need to mention each column instead of doing SELECT * FROM. However, if you don't need all columns you can optimize by selecting only the columns you need: SELECT a, b, c FROM.
There's no value added or optimization in having two nested SELECT * without any sort of calculation. Here's an article on Transact-SQL Derived Tables where I recommend you check out the Advantages of SQL derived tables section. There's a good example in there.