Inputting data from dataset to SSRS table - reporting-services

I am new to this and i am wondering how to input data into SSRS table and auto generate for the subsequent months. This is the format of the table.
Appreciated for any help given.

You can generate a date range using the following SQL
DECLARE #date_start AS DATETIME
SET #date_start = '01-DEC-2017'
;WITH
finalvalues
AS
(
SELECT tbl.* FROM (VALUES
( '01-Dec-2017', 6414.6563, 429.6846, -1390.8474)
, ( '02-Dec-2017', 6476.6563, 432.751, -1312.4928)
, ( '03-Dec-2017', 6538.6563, 435.8174, -1234.1382)
, ( '04-Dec-2017', 6600.6563, 438.8838, -1155.7836)
, ( '05-Dec-2017', 6662.6563, 441.9502, -1077.429)
, ( '06-Dec-2017', 6724.6563, 445.0166, -999.074399999999)
, ( '07-Dec-2017', 6786.6563, 448.083, -920.719799999999)
, ( '08-Dec-2017', 6848.6563, 451.1494, -842.365199999999)
, ( '09-Dec-2017', 6910.6563, 454.2158, -764.010599999999)
, ( '10-Dec-2017', 6972.6563, 457.2822, -685.655999999999)
, ( '11-Dec-2017', 7034.6563, 460.3486, -607.301399999999)
, ( '12-Dec-2017', 7096.6563, 463.415, -528.946799999999)
, ( '13-Dec-2017', 7158.6563, 466.4814, -450.592199999999)
, ( '14-Dec-2017', 7220.6563, 469.5478, -372.2376)
, ( '15-Dec-2017', 7282.6563, 472.6142, -293.883)
, ( '16-Dec-2017', 7344.6563, 475.6806, -215.5284)
) tbl ([Date], [IncLoad], [ITLoad], [RH])
)
,
manufactured_dates
AS
(
SELECT
day_date = DATEADD(day, dte.[number], #date_start)
FROM
master.dbo.spt_values AS dte
WHERE
1=1 -- <-- used in testing to be able to comment out other clauses below
AND dte.[type] = 'P'
AND dte.[number] <= 365 -- <-- filter how many rows you want to see here
)
SELECT
'Date' = md.[day_date]
, 'IncLoad' = AVG(incload)
, 'ITLoad' = AVG(ITLoad)
FROM
finalvalues AS fv
FULL OUTER JOIN manufactured_dates AS md ON md.[day_date] = fv.[Date]
GROUP BY
md.[day_date]

Related

Joining Table with Itself MYSQL

I have a scenario where i have table app_users in which i am storing multiple types of users , and each user have different column for phone number , and Names , i am trying to get phone number and name of specific type of user , it's hard to understand this way , but see the working example Query below :
SET #sk := (SELECT app_users.skeeper_phone as sphone FROM app_users WHERE app_users. app_user_type = 'SKEEPER' LIMIT 1);
SELECT
acs.id as complainId,
IF(
(#sk) > 0,
#sk,
''
) as sk_phone,
(
SELECT
IF(
auu.institute_phone IS NULL,
auu.department_phone,
auu.institute_phone
) as cphone
FROM app_users as auu
WHERE auu.id = acs.app_customer_id
LIMIT 1
) as cphone,
(
SELECT auuu.other_user_phone as ephone
FROM app_users as auuu,app_admin_assign_eng as aase
WHERE aase.app_complain_service_id = acs.id AND auuu.id = aase.engineer_id
LIMIT 1
) as ephone,
(
SELECT auuu.fullname as ename
FROM app_users as auuu,app_admin_assign_eng as aase
WHERE aase.app_complain_service_id = acs.id AND auuu.id = aase.engineer_id
LIMIT 1
) as ename,
(
SELECT auuu.other_user_phone as ephone
FROM app_users as auuu,app_admin_assign_eng as aase
WHERE aase.app_complain_service_id = acs.id AND auuu.id = aase.admin_id
LIMIT 1
) as aphone
FROM app_complain_services as acs,app_users as au
WHERE
acs.id = 5
GROUP BY acs.id
Above query is working alright , but it is not optimized , what could be optimization and other ways to solve similar problem?

There is already an object named 'PDBCompr' in the database

I am using SQL server 2008. I want to insert data from a temp table to database. I am using While loop to insert data from temp table to database table.
Now I am facing an issue:
object already exist in the database.
declare #rev as int ,
#sQuotationNo NVARCHAR(15),
#sQRevNo int
set #rev=(select top 1 QRevNo from PDBCompr Where QuotationNo='JCS_G1415_008' and QRevNo<>'3' order by QRevNo desc)
;with cte as
(
SELECT
ROW_NUMBER() OVER(ORDER BY QuotationNo) AS sSLNO,
[CompanyCode] ,
[ProjectCode] ,
[PRevNo],
[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from PDBCompr
where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='2'
and AddCmprId not in (select distinct AddCmprId from PDBCompr where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='3' )
)
select * into #temp from cte
declare #cnt int , #loopCnt int=1
select #cnt =( select COUNT(*) from #temp)
while (#loopCnt<=#cnt)
begin
;with cte2 as
(
SELECT
sSLNO,
[CompanyCode] ,
[ProjectCode] ,
(select Max(PRevNo)+1 from PDBCompr) [PRevNo],
(select Max(CSlNo) +1 from PDBCompr)[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from #temp where sSLNO=#loopCnt
)
select * into PDBCompr from cte2
set #loopCnt= #loopCnt+1
drop table #temp
end
Please help me to find a proper solution. Thanks in advance
select * into PDBCompr from cte2
in this line you are again making a table "PDBCompr", but in above code you are selecting records from this table it means its already exist.
so if you want to insert record in this table then you need to change your query like below:
insert into PDBCompr select * from cte2

Subtracting Time using timediff

help. I have this query in mysql. What it does is combine fields from three tables. One field (Duration (h)) it subtracts the end time and start time. May time data is in the form of time only and not date time. So I decided to use timediff function. This runs well for time within the same day. But not time differences like 00:00 - 23:30; in this case I get the answer -23.5. What can I do to correct this. Thanks in advance.
SELECT `tblproductiondata`.`productionDay` , `tbllinestoppagecategories`.`category` , `tblshifts`.`ID` , `tblstoppagedescriptions`.`endTime` , `tblstoppagedescriptions`.`startTime` , IFNULL( ROUND( `tblstoppagedescriptions`.`duration(mins)` /60, 2 ) , ROUND( (
TIME_TO_SEC( TIMEDIFF( `tblstoppagedescriptions`.`endTime` , `tblstoppagedescriptions`.`startTime` ) ) /3600 ) , 2 )
) AS `Duration (h)`
FROM (
`tbllinestoppagecategories`
INNER JOIN `tblstoppagereasons` ON `tbllinestoppagecategories`.`categoryID` = `tblstoppagereasons`.`stoppagecategory`
)
INNER JOIN (
`tblshifts`
INNER JOIN (
`tblproductiondata`
INNER JOIN `tblstoppageDescriptions` ON `tblproductiondata`.`productionID` = `tblstoppagedescriptions`.`prodDate`
) ON `tblshifts`.`ID` = `tblproductiondata`.`shiftName`
) ON `tblstoppageReasons`.`resID` = `tblstoppagedescriptions`.`stoppageReason`
WHERE (
(
(
`tbllinestoppagecategories`.`category`
) <> "Changeover Maintenance Activities"
)
)
ORDER BY `tblproductiondata`.`productionDay` ASC
try
TIME_TO_SEC(
TIMEDIFF(
CONCAT(tblproductiondata.productionDay, ' ', tblstoppagedescriptions.endTime),
CONCAT(tblproductiondata.productionDay, ' ', `tblstoppagedescriptions`.`startTime`)
)
)

SQL Server Split Datetime for Day-Hourly Query?

SQL Server 2008.
declare #pardate table ( pardateid int, pardatewhen datetime2(3) )
insert into #pardate values ( 1 , '2011-09-17 12:43' )
insert into #pardate values ( 2 , '2011-09-17 12:44' )
insert into #pardate values ( 3 , '2011-10-11 12:45' )
insert into #pardate values ( 4 , '2011-10-12 12:46' )
insert into #pardate values ( 5 , '2011-10-13 12:47' )
insert into #pardate values ( 6 , '2011-11-20 12:48' )
insert into #pardate values ( 7 , '2011-11-21 12:49' )
insert into #pardate values ( 8 , '2011-11-22 12:50' )
declare #child table ( childid int , pardateid int , childvalue char(6) )
insert into #child values ( 1 , 1 , 'aaaaaa' )
insert into #child values ( 2 , 2 , 'bbbbbb' )
insert into #child values ( 3 , 3 , 'cccccc' )
insert into #child values ( 4 , 4 , 'dddddd' )
insert into #child values ( 5 , 5 , 'cccccc' )
insert into #child values ( 6 , 6 , 'cccccc' )
insert into #child values ( 7 , 7 , 'eeeeee' )
insert into #child values ( 8 , 8 , 'ffffff' )
select pardatewhen , childvalue , COUNT(childvalue)
from #child childtable join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by pardatewhen , childvalue
I am trying to get a count of #child.childvalue every day, every hour, so there would be 8760 rows in my result.
First pass had a loop and a CONVERT which takes ~5 minutes to run with the actual result set (this is just a sample for illustation). I did create a CTE to make a calendar temp table (using http://www.sqlpointers.com/2006/07/generating-temporary-calendar-tables.html), and thought it could be joined somehow to add "empty values" into the result set.
I need to get a result set that looks like this
date hour count
...
2011-09-17 0 0
....
2011-09-17 12 2
....
2011-10-11 12 1
How can that be done efficiently?
Thanks.
try this.
;WITH cal AS
(SELECT CAST('2011-01-01' AS DATETIME) AS cal_date
UNION ALL
SELECT DATEADD(hour,1,cal_date)
FROM cal
WHERE cal_date < '2011-12-31 23:00'
)
, par AS
(
select CAST(pardatewhen AS DATE) AS pardate, DATEPART(hh,pardatewhen) AS parhour , COUNT(childvalue) as num
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by CAST(pardatewhen AS DATE), DATEPART(hh,pardatewhen)
)
SELECT CAST(cal.cal_date AS DATE) AS [date],DATEPART(hh,cal.cal_date) AS [hour],ISNULL(par.num,0) AS [childvalue_count]
FROM cal
LEFT JOIN par
ON CAST(cal.cal_date AS DATE) = par.pardate
AND DATEPART(hh,cal.cal_date) = par.parhour
OPTION (MAXRECURSION 9999)
Something like (have childvalue in your query but not in your example result?)
select Cast(pardatewhen as Date) as [date], DatePart(hour,pardatewhen) as [hour] , childvalue , COUNT(childvalue)
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by Cast(pardatewhen as Date), DatePart(hour,pardatewhen), childvalue
Note Date type was introduced in SQL 2008

SQL Server 2008 Pivot Key Value to Joined Parent?

SQL Server 2008, I have the following parent/child table schemata and rows:
create table #par( parid int primary key identity(1,1) , partext varchar(6) )
create table #chi( chiid int primary key identity(1,1) , parid int null , chirefid int null , chiinfo varchar(6) )
create table #chiref ( chirefid int primary key identity(1,1) , chisubdesc varchar(9) )
insert into #par values ( 'par1' ) , ('par2')
insert into #chiref values ( 'chi1' )
insert into #chiref values ( 'chi2' )
insert into #chiref values ( 'chi3' )
insert into #chi values ( 1 , 1 , 'aaa' )
insert into #chi values ( 1 , 2 , 'bbb' )
insert into #chi values ( 2 , 1 , 'ccc' )
insert into #chi values ( 2 , 2 , 'ddd' )
insert into #chi values ( 2 , 3 , 'eee' )
The child #chi has just key/value pairs inside, and I need to convert the text (key) into a column and put the value inside, so the result set is shaped like the below. What is the best way to do that (I cannot change the key/value stuff, it is inherited from another system). And there is a join on the #chiref table for the actual column names (which is really killing me).
partext chi1 chi2 chi3
par1 aaa bbb
par2 ccc ddd eee
Thanks.
EDIT - The thing to remember is that the column names have to match the "key value" in the table. So if the EAV row key is "chi1" then the pivot has to have "chi1". I had simply "1" which broke. So I got it.
Thanks again!
select partext, [chi1], [chi2], [chi3]
from
(
select p.partext, c.chitext, c.chiinfo
from #chi c
join #par p
on c.parid = p.parid
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in ([chi1], [chi2], [chi3])
) as PivotTable
or this one is a little more efficient, although more code:
with c
as
(
select parid, [chi1], [chi2], [chi3]
from
(
select parid, chitext, chiinfo
from #chi
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in ([chi1], [chi2], [chi3])
) as PivotTable
)
select p.partext, c.*
from c
join #par p
on c.parid = p.parid
if you need a dynamic column number, you can do dynamic query:
declare #ColumnList varchar(max)
select #ColumnList = isnull(#columnList + ', ', '') + '[' + chitext + ']'
from
(
select distinct chitext
from #chi
) tt
order by chitext
declare #Command varchar(max) = '
with c
as
(
select parid, ' + #ColumnList + '
from
(
select parid, chitext, chiinfo
from #chi
) AS SourceTable
pivot
(
min(chiinfo)
for chitext in (' + #ColumnList + ')
) as PivotTable
)
select p.partext, ' + #ColumnList + '
from c
join #par p
on c.parid = p.parid
'
execute(#Command)