Why the Lookup function in SSRS is failing - sql

I have two tables in my database as follows:
SELECT TOP 12 [Date], [Amount]
FROM [Db].[dbo].[final]
UNION
SELECT MAX([F1]) 'Date', SUM([F2]) 'Amount' FROM (
SELECT TOP 5 [F1], [F2]
FROM [Db].[dbo].[origtable]
WHERE [F1] IS NOT NULL
ORDER BY [F1] DESC
) c
SELECT TOP 12 [date_period2] AS [Date], [trnchargeamt] AS [Amount]
FROM [Db].[dbo].[othertable]
ORDER BY [date_period2] DESC
Which displays the following (first query is the top and second query is the bottom):
Date Amount
2013-07-31 00:00:00.000 9658254
2013-08-31 00:00:00.000 6659659
2013-09-30 00:00:00.000 14256326
2013-10-31 00:00:00.000 8912215
2013-11-30 00:00:00.000 9326659
2013-12-31 00:00:00.000 10211985
2014-01-31 00:00:00.000 8652365
2014-02-28 00:00:00.000 16256326
2014-03-31 00:00:00.000 24454342
2014-04-30 00:00:00.000 16345908
2014-05-30 00:00:00.000 6976515.77
2014-05-30 00:00:00.000 23578640
Date Amount
201406 42492.78
201405 1846703.374
201405 44390961.65
201404 45413273.91
201403 46943807.39
201402 33744223.24
201401 41630266.94
201312 40672523.92
201311 42465408.47
201310 47878730.59
201309 39444986.25
201308 40554376.28
In my SSRS I have it like the following way to display the charts:
I am using the following lookup function to display the first table with the second table:
=Lookup(Fields!Date.Value,Fields!Date.Value, Fields!Amount.Value, "WMGDailyPaymentsRed")
I get the following warning and nothing is displayed for the second chart:
[rsRuntimeErrorInExpression] The Y expression for the chart ‘Chart8’ contains an error:
Exception of type 'Microsoft.ReportingServices.ReportProcessing.ReportProcessingException_ComparisonError' was thrown.`

As you can see from the results of your select statement the first SQL.. has an actual Date and the second sql has a date period with just year and month.. hence comparing both would not work. You would need to modify your first query to format the date into YYYYMM format and then it would work in the lookup.

Related

How to loop in Select Query SQL

Hi I have this Table like this
ICN StartContract EndContract ContractValue PeriodOfContract
A 2019-12-31 17:00:00.000 2020-03-30 17:00:00.000 19546194.00 3.00
B 2019-12-31 17:00:00.000 2020-12-30 17:00:00.000 1397095800.00 12.00
C 2021-02-28 17:00:00.000 2022-02-27 17:00:00.000 4016244584.00 12.00
D 2018-05-27 17:00:00.000 2021-05-30 17:00:00.000 9686992857.00 36.00
I want to create a view to loop a Period of Contract and create a new row in My View. Example Result
ICN Date Amount
A 2019-12-31 6,515,398
A 2020-01-31 6,515,398
A 2020-02-29 6,515,398
I've seen some loop examples but mostly it's used only on the function. I want to loop the period of the contract and populate a new row based on the period on my View.
This is My Query
/****** Script for SelectTopNRows command from SSMS ******/
SELECT
ICN, StartContract , EndContract, ContractValue, PeriodOfContract
FROM [FactProject]
Any suggestion or example query to do that? Thanks
;WITH n(n) AS
(
SELECT 0
UNION ALL
SELECT n+1 FROM n WHERE n+1 < 100
)
SELECT ICN, C.[Date], C.Amount FROM T
CROSS APPLY
(
SELECT
CAST(DATEADD(MONTH, n, StartContract) AS date) [Date],
CAST(ROUND(ContractValue / PeriodOfContract, 0) AS int) Amount
FROM n
WHERE
n.n < T.PeriodOfContract
) C
ORDER BY ICN, [Date]
I used WITH n(n) to generate a list of int numbers (explained here). Note: You should increase the number 100 if you may have a PeriodOfContract value more than 100.
Output:
ICN Date Amount
A 2019-12-31 6515398
A 2020-01-31 6515398
A 2020-02-29 6515398
B 2019-12-31 116424650
B 2020-01-31 116424650
...

Merge two records date if ToDate equal second FromDate SQL Server?

Example data:
FK_EmployeeId FromDate ToDate DateDiff
20325 2016-06-24 00:00:00.000 2016-06-25 00:00:00.000 2
20325 2016-06-25 00:00:00.000 2016-06-26 00:00:00.000 2
20325 2016-06-26 00:00:00.000 2016-06-28 00:00:00.000 3
20325 2016-06-28 00:00:00.000 2016-06-29 00:00:00.000 2
20325 2016-06-29 00:00:00.000 2016-06-30 00:00:00.000 2
20325 2016-06-30 00:00:00.000 2016-07-01 00:00:00.000 2
20325 2016-07-01 00:00:00.000 2016-07-02 00:00:00.000 2
20325 2016-07-02 00:00:00.000 2016-07-03 00:00:00.000 2
20325 2016-07-03 00:00:00.000 2016-07-04 00:00:00.000 2
20325 2016-07-04 00:00:00.000 2016-07-05 00:00:00.000 2
And I would like to get the following output:
FK_EmployeeId FromDate ToDate DateDiff
20325 2016-06-24 00:00:00.000 2016-06-26 00:00:00.000 3
20325 2016-06-28 00:00:00.000 2016-07-05 00:00:00.000 8
I think you are trying to do something like the below using a windowing function to join the records to themselves to perform the date comparisons across 2 rows.
However as people have already suggested in the comments your sample data above contains all sequential dates so its not really clear how your expecting this to work. I've therefore excluded a middle value in the sample data to demonstrate the approach.
--TABLE JUST FOR EXAMPLE QUERY
DECLARE #SomeTable TABLE ([FK_EmployeeId] INT,[FromDate] DATETIME,[ToDate] DATETIME,[DateDiff] INT)
--YOUR SAMPLE DATA
INSERT INTO #SomeTable
([FK_EmployeeId],[FromDate],[ToDate],[DateDiff])
SELECT 20325,'2016-06-24 00:00:00.000','2016-06-25 00:00:00.000',2
UNION SELECT 20325,'2016-06-25 00:00:00.000','2016-06-26 00:00:00.000',2
UNION SELECT 20325,'2016-06-26 00:00:00.000','2016-06-28 00:00:00.000',3
UNION SELECT 20325,'2016-06-28 00:00:00.000','2016-06-29 00:00:00.000',2
UNION SELECT 20325,'2016-06-29 00:00:00.000','2016-06-30 00:00:00.000',2
--UNION SELECT 20325,'2016-06-30 00:00:00.000','2016-07-01 00:00:00.000',2 --<<
UNION SELECT 20325,'2016-07-01 00:00:00.000','2016-07-02 00:00:00.000',2
UNION SELECT 20325,'2016-07-02 00:00:00.000','2016-07-03 00:00:00.000',2
UNION SELECT 20325,'2016-07-03 00:00:00.000','2016-07-04 00:00:00.000',2
UNION SELECT 20325,'2016-07-04 00:00:00.000','2016-07-05 00:00:00.000',2
--THE POINT:
;WITH cte AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY [FK_EmployeeId] ORDER BY [ToDate]) AS 'RowNo'
FROM
#SomeTable
)
SELECT
a.*,
DATEDIFF(DAY,a.[FromDate],b.[ToDate]) AS 'NewDiff'
FROM
cte a
JOIN cte b
ON a.[RowNo] + 1 = b.[RowNo]
WHERE
a.[ToDate] <> b.[FromDate]
A little more detail in the question next time please.

SQL Server query join several tables

I have a query that I don't think should be that hard to make, however, I've spent a lot of time on it now and still can't get it the way I want, so I hope someone here can help me.
Basically, I need to create a report that will give a value for each month, for each area. However, not all areas deliver data each month; in that case the view should return NULL for that month and area. So, the view need to look something like this:
Month Area Value
2012-08-01 Area1 2
2012-08-01 Area2 3
2012-09-01 Area1 3
2012-09-01 Area2 NULL
My data table looks something like this
Date Area Value
2012-08-01 Area1 2
2012-08-01 Area2 3
2012-09-01 Area1 3 -- Notice that Area2 is not present for September here
I have a table with all the available areas
Furthermore, I have created a table-valued function that returns all dates from a given date until now.
For example this statement
SELECT * FROM Periods_Months('2012-01-01')
would return 8 records like:
DateValue Year Month YearMonth
2012-01-01 00:00:00.000 2012 1 20121
2012-02-01 00:00:00.000 2012 2 20122
2012-03-01 00:00:00.000 2012 3 20123
2012-04-01 00:00:00.000 2012 4 20124
2012-05-01 00:00:00.000 2012 5 20125
2012-06-01 00:00:00.000 2012 6 20126
2012-07-01 00:00:00.000 2012 7 20127
2012-08-01 00:00:00.000 2012 8 20128
Based on the suggestions, my query now looks like this:
WITH months AS (
SELECT DateValue, YearMonth FROM Periods_Months('2011-01-01')
)
select m.DateValue
,CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,m.DateValue)+1,0)) AS Date) AS DateReported -- Get last day in month
,ResponseTime AS Value
,g.ExternalId
from GISDB.dbo.GisObjects g
CROSS JOIN months m
LEFT OUTER JOIN
( -- SELECT data from data table, grouped by area and month
SELECT dbo.YearMonth(CloseDate) AS YearMonth
,MAX(CloseDate) AS LastDate
,GisObjectId
,SUM(DATEDIFF(HH,RegDate,CloseDate)) AS ResponseTime -- calculate response time between start and end data (the value we need)
FROM DataTable
WHERE CloseDate IS NOT NULL
AND GisObjectId IS NOT NULL
GROUP BY GisObjectId, dbo.YearMonth(CloseDate) -- group by area and month
) c
ON g.ObjectId = c.GisObjectId AND c.YearMonth = m.YearMonth
WHERE g.CompanyId = 3 AND g.ObjectTypeId = 1 -- reduce the GIS objects that we compare to
ORDER BY m.DateValue, g.ObjectId
But the result is this (Value is always NULL):
DateValue DateReported Value ExternalId
2011-01-01 00:00:00.000 31-01-2011 NULL 9994
2011-01-01 00:00:00.000 31-01-2011 NULL 9993
2011-01-01 00:00:00.000 31-01-2011 NULL 9992
2011-01-01 00:00:00.000 31-01-2011 NULL 9991
2011-01-01 00:00:00.000 31-01-2011 NULL 2339
2011-01-01 00:00:00.000 31-01-2011 NULL 2338
2011-01-01 00:00:00.000 31-01-2011 NULL 2337
2011-01-01 00:00:00.000 31-01-2011 NULL 2336
2011-01-01 00:00:00.000 31-01-2011 NULL 2335
2011-01-01 00:00:00.000 31-01-2011 NULL 2334
2011-01-01 00:00:00.000 31-01-2011 NULL 2327
2011-01-01 00:00:00.000 31-01-2011 NULL 2326
2011-01-01 00:00:00.000 31-01-2011 NULL 2325
2011-01-01 00:00:00.000 31-01-2011 NULL 2324
2011-01-01 00:00:00.000 31-01-2011 NULL 2323
2011-01-01 00:00:00.000 31-01-2011 NULL 2322
etc.
I suppose you have a table with all your areas, which I call area_table.
WITH month_table AS (
SELECT dateValue FROM Periods_Months('2012-01-01')
)
select * from area_table
CROSS JOIN month_table
LEFT OUTER JOIN myValueTable
ON area_table.name = myValueTable.area
AND myValueTable.date = left(convert(varchar(30),month_table.dateValue,120),10)
ORDER BY myValueTable.Month, myValueTable.area
Suppose Areas is your table for all available areas, t - is your data table:
SELECT pm.dateValue,Ar.Area, t.value
FROM Periods_Months('2012-01-01') pm, Areas ar
left join t on (pm.dateValue=t.Date) and (ar.Area=t.Area)
order by pm.DateValue,ar.Area

Group By,Order by DateTime

I have nearly 15000 data rows with the first column containing date in the format:
2012-05-10 09:00:00.000
I need this data to be sorted by year then month, then day, then hour so for example:
2012-05-10 09:00:00.000
2012-05-10 10:00:00.000
2012-05-10 11:00:00.000
2012-05-10 12:00:00.000
2012-05-11 09:00:00.000
2012-05-11 10:00:00.000
2012-05-11 11:00:00.000
2012-05-11 12:00:00.000
2012-06-01 02:00:00.000
2012-06-01 03:00:00.000
2012-06-01 04:00:00.000
2012-06-01 05:00:00.000
Current SQL Query to do this is below:
SELECT MIN(Datetime)
GROUP BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
HAVING MIN(jmusa_LOG1.DateTime) NOT IN(SELECT DateTime FROM AverageRawData)
ORDER BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
You are describing a normal date sort, so you can just do:
select MyDate
from AverageRawData
order by MyDate
If you don't want duplicates, add DISTINCT like this:
select distinct MyDate
from AverageRawData
order by MyDate
If this does not meet your requirements, please provide sample data used to generate your output example.

Get value for each date if not exist then take previous last updated value

I have table which have Column EmployeeID, AccountID,updated date. Each row have Data for Account if it changes on date.
If no change then there is no record for that AccountID on that date. Example
EmployeeID AccountID UpdatedDate
1775 1 2010-12-04 00:00:00.000
1775 1 2010-08-13 23:59:59.000
1775 1 2010-08-13 00:00:00.000
1775 2 2010-12-04 00:00:00.000
1775 3 2010-12-04 00:00:00.000
1775 4 2010-12-04 00:00:00.000
1775 5 2010-12-04 00:00:00.000
1775 6 2010-12-04 00:00:00.000
1775 7 2010-12-04 00:00:00.000
1775 7 2010-06-29 23:59:59.000
I have to get value of each account for each person on each day . if there is no value on current day then it should take last update value from previous day based on Max update date value. and show the result like
EmployeeID,Date, Values of each account.
1775;20120307;45;0;0;0;0;0;0;0;0;0;0;504;0;0;25.0;0.0;0.0;0.0;0.0;0.0;0.0;100;;;;;
Can any one help me?
You can either create a table of dates and left join against that, or create your dates dynamically as described here: generate days from date range