Split Table data into two table from one data set in ssrs - reporting-services

I need to split the number of rows into two table from one Dataset in
SSRS.
First table should have first 30 records and second table start with
row number 31. Number of records may be increased so this should be
dynamic. Need to do this in SSRS design only not in SP.
I have tried Expression Rownumber(Nothing)/30 in table filter but
cannot use Rownumber function in table filter.
Please suggest.

I don't think you can use any kind of aggregation in a table filter so you would have to look at alternatives.
If you cannot change the stored proc then you could dump the results of the stored proc into a temp table then do additional processing on that. You can do all this in your report's dataset query.
for example
CREATE TABLE #t (myFirstSPColumn int, mySecondSPColumn varchar(10))
INSERT INTO #t
EXEC myStoredProc
SELECT *,
(ROW_NUMBER() OVER(ORDER BY CountryID)-1) / 30 as TableNumber
FROM #t
This will run the stored proc, put the results into a temp table and then add a TableNumber column which you can use directly in your report

Related

Second dataset result not showing on first time execution

I had created a SSRS reports where it generates list of studnts with selected subjects and it marks which was displayed in matrix successfully. After that I need to tshow the overall analysis of each subject in the same report.
So I had created total 2 procedures.
First one for dataset 1 list of students with respective subject wise marks.
As like temp table here I had created a table where I push the analysis of each subject in the new table.
And by using the second procedure for dataset 2 I planned for fetching the data of subject wise analysis.
Every time the second proc executes I'm deleting the data of that table.
My problem is I'm not getting the data from dataset 2 on the firt execution.From the second run I'm able to get the data.
Whenever I had changed the parameters for the first time It's not geting the data.
ALTER Proc [dbo].[SP_Get_IGCSESubjectMarks_GetLastTerm_HTS2] --7,'1,17,8','2537,2555,2558,2568'
(
#ReportId int=7,
#SubjectId varchar(200),
#SectionId varchar(200)
)
AS
BEGIN
-------------------
------------------- Some code
Insert into #temp (Name,Class,SubjectName,Section,enrollNo,TermName,TestName,Marks)
select Name,Class,'Total',Section,enrollNo,TermName,'Percentage',SUM(Marks)*100/sum(maxmarksare) from #temp1
GROUP BY Name,Class,Section,enrollNo,SubjectName,TermName,SubjectOrder
--TmpIgcseData is the new table for which I'm pushing the subject wise analysis This was the table used in 2nd dataset for fetching data.
delete from TmpIgcseData
insert into TmpIgcseData(enrollno,SName,SubjectName,TestName,Marks,OrderNumber) select enrollno,Name,SubjectName,TestName,Marks,SubjectOrder from #temp
select #UID as Id,* from #temp
drop table #temp
drop table #temp1
end
--------------------------------------------------------------
ALTER PROC [dbo].[IgcesResultAnalysis_HTS2]
AS
begin
------------
------------ Some code.
select * from #distsubjects
--Deleting the data from the table
delete from TmpIgcseData
End
This is really bad practice. You are editing data in a permanent table (TmpIgcseData). What happens if two people execute the report? You also cannot rely on the execution order of the datasets in SSRS.
It is much better to pass the required parameters to both procedures and do all the work within each procedure. In other words, do not rely on another proc to prepare the data unless you call that proc from with your main proc and restrict the tables used to temp tables within the scope of the main proc.

Generic stored procedure to lag a table column

I need to calculate returns at different frequencies. In order to do so, I would like to be able to lag the values in a column by k units. While I have found different specific solutions, I have not been able to make a general stored procedure (most likely due to my inexperience with mysql). How could I best do this?
I have a table with multiple columns, amongst which columns containing info on:
ID
Date
Price
The end result should be a table with all the original columns, plus a column containing the lagged values of Price.
To keep the procedure general, I could imagine the procedure would take the table name, necessary column names (e.g. ID, Date, Price), and number of lags k as input, and append a column to the table.
You can do what you want with a correlated subquery. Here is an example:
select t.*,
(select t2.price
from <tablename> t2
where t2.date < t.date
order by date
limit 1 offset 1 -- change the offset for a bigger lag
) as price_lag_1
from <tablename> t;
Your desire to create a generic stored procedure is not very SQL-y. MySQL doesn't support table-valued functions, so you wouldn't be able to use the resulting table as an actual table.
If you want to put this in a stored procedure that is generic, you will need dynamic SQL to construct the SQL statement, using the particular table and columns that you pass in.
Instead, I would suggest that you simply learn how to express what you want as a query. If you have multiple tables with the same structure, then you may want to revisit your data model. Have multiple similar tables is often an example of an entity being inappropriately spread across too many tables.

How to create auxiliary table in concise way?

I want to create temporary table with some dataset to execute more complicated query to mysql DB.
I see two possible ways to do it.
create table and insert every row:
create temporary table dates (
date date not null
);
insert into dates values ('2010-01-01');
insert into dates values ('2010-02-01');
insert into dates values ('2010-03-01');
or create in-place table:
select *
from (
select '2010-01-01' as date
union select '2010-02-01' as date
union select '2010-03-01' as date
) as dates;
Is there other more concise way to do such table?
Some like to create permanent aux tables like sequence of numbers or dates and then just reference them in their queries. Other DB systems allow recursive CTEs which are more concise.
You could write a stored procedure that can loop and create your temporary table more concisely.
Although a bit silly of a suggestion, your provided SQL does not need the "AS Date" except on the first select of the derived table and you should use UNION ALL instead of UNION.

How can I dynamically set the table name using SELECT INTO?

DROP TABLE Backup_LOAD_EMPLOYEE
SELECT * INTO dbo.Backup_LOAD_Employee FROM LOAD_Employee WHERE 1=1
TRUNCATE TABLE LOAD_Employee
I am bulk inserting employee data from external source . In my sp each time after import , I will truncate the load_employee table. Before truncate I would like to take a table backup,previous day data should truncate .
how to give auto increment table name ( in an SP)?
This doesn't answer your question directly (but you can use dynamic SQL), but a better solution is probably to put the backup date into a column, instead of creating one table per day. Then you can more easily query the archived data for multiple days, because it's all in one table. Something like this:
create table dbo.Backup_LOAD_Employee (
BackupDate date,
--- other columns
)
go
insert into dbo.Backup_LOAD_Employee (BackupDate, ...)
select cast(getdate() as date), ... -- other columns
from dbo.LOAD_Employee
truncate table dbo.LOAD_Employee

increment a value when a row is selected SQL

Is there any way to essentially keep track of how many times a row has been pulled from a SQL table?
For example in my table I have a column count. Every time a SQL statement pulls a particular row (lets call it rowA), rowA's 'count' value increases 1.
Either in the settings of the table or in the statement would be fine, but i cant find anything like this.
I know that I could split it into two statements to achieve the same thing, but I would prefer to only send one.
The best way to do this is to restrict read-access of the table to a stored procedure.
This stored procedure would take various inputs (filter options) to determine which rows are returned.
Before the rows are returned, their counter field is incremented.
Note that the update and the select command share the same where clause.
create procedure Select_From_Table1
#pMyParameter varchar(20), -- sample filter parameter
as
-- First, update the counter, only on the fields that match our filter
update MyTable set Counter = Counter + 1
where
MyFilterField like CONCAT('%', #pMyParameter, '%') -- sample filter enforcement
-- Now, return those rows
select
*
from
MyTable
where
MyFilterField like CONCAT('%', #pMyParameter, '%') -- sample filter enforcement
A decent alternative would be to handle it on the application side in your data-access layer.