SSRS Date parameter ranges - sql-server

I have a new requirement to design report using date range parameters for Start and End dates in my report.
I am using this queries in my report:
Main Dataset:
SELECT Col1, Col2, StartDate, TargetDate, Col3
FROM Table
WHERE (StartDate BETWEEN #StartDateFrom AND #StartDateTo)
AND (TargetDate BETWEEN #TargetDateFrom AND #TargetDateTo)
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))
GROUP BY Col1, Col2, Col3
While running the report I get an error: TargetDate parameter is missing value.
Can someone help?

I don't see where you are "DECLARE"ing your variables #TargetDateTo and #TargetDateFrom? You may want to check your query again as SSRS may not be picking up all your variables.

Related

Snowflake order by

Is there any way I can select from a table without specifying the order by column in the order by clause?
select col1 from table order by col2
This works in TSQL, but doesn't appear to be allowed in Snowflake.
Yes, this is possible:
CREATE OR REPLACE TABLE tab AS
SELECT 1 AS col1, 'B' AS col2 UNION ALL
SELECT 2, 'A';
SELECT col1
FROM tab
ORDER BY col2;
Output:

Group row id for all unique rows in SQL Server

I would like to generate group row id for each unique rows. Any hint or suggestion would be helpful. I am using SQL Server 2017.
You can use the windowed function ROW_NUMBER() as shown below:
Select col1
, col2
, ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col1, col2) [Group ID]
from <YourTableName>
Db<>Fiddle Demo
For your Case ROW NUMBER Function and use PARTITION by Column Names
SELECT *,ROW_NUMBER () OVER (Partition By COL1 ORDER BY COL1) from #T

SQL query optimization for select query over IN query

I have one view, i want to add pagination logic on this view. There are over 1.5 million records. It took longer time to get result if for my where condition that select only specific records mapped with one Id.
I am thinking of getting only those mapped records from main table and then select only those records from view, will this faster?
Select top 10 col1, col2, col3, ROW_NUMBER() OVER (ORDER BY col4 desc) from vMyView where someid=1
Then
Select top 10 col1, col2, col3 from vMyView where col1 in (Select col1 from tMyTable where someid=1)
FYI I am not expert
Assuming typical cardinality, I tend to write it more like this:
select top 10 col1, col2, col3
from vMyView v
inner join tMyTable t ON t.col1 = v.col1
WHERE t.someid = 1
However, if it's possible to match more than one row in tMyTable for each col1 value in vMyView, this could possibly result in duplicating rows from vMyView. If duplicating rows is possible, a solution based on row_number() is typically the fastest option.
i want to add pagination logic on this view
As for paging, you should look into OFFSET/FETCH syntax, rather than TOP n.
SELECT col1, col2, col3
FROM vMyView v
ORDER BY <need an order by clause for paging to work>
OFFSET <pagenumber * pagesize> FETCH NEXT <pagesize> ROWS ONLY

Can we add a new column to the result set with out modifying the select in SQL server

I have a query as:
SELECT T1.col1, T1.col2, T2.col3, T2.col4 from Table1 T1 join Table2 T2
This returns the columns(T1,T2,T3,T4) as expected.
col1 col2 col3 col4
A 11 0.5 B
Now i want to add a column to the result set with out modifying the above Select statement.
I want output result set should have another column(col8) from Table1 but i cannot add this column(col8) to the exisiting Select statement.
col1 col2 col3 col4 col8
A 11 0.5 B 9
I know there is Union in SQL server. But Union requires
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order.
I want to achieve similar to UNION but with out above conditions. I want only column to be added to the result set.
Is this achievable in SQL Server?
Any help?
Thanks in advance.

How can I use the plus operation for a column in SQL Server with the result in the next row?

I have a table in SQL Server that I want to plus amount of a specific column and have the result in next row.
How can I do that?
I want to plus amount of a specific column and have the result in next row.
In case you are looking to insert another from the previous row after adding certain amount, you could use the following:
INSERT INTO MyTable (Col1, Col2, Col3)
SELECT Col1, Col2 + <additional amount>, Col3
FROM MyTable
WHERE
<Criteria to select that row of interest>
In case you are looking to select all the rows in a table and aggregate the amount column and show the result in a separate row, then you could use the following:
SELECT Col1, Col2, Col3 FROM MyTable
UNION
SELECT '', SUM(Col2), '' FROM MyTable

Resources