DECLARE
#StartDate DATE,
#EndDate DATE,
#NumMonths INT,
SET #NumMonths = 12
SET #StartDate = DATEADD(m, -#NumMonths, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
SET #EndDate = DATEADD(m, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
SELECT ROW_NUMBER() OVER (ORDER BY SignUpDate) RowNum, SignUpDate,
TotalSignUp / TotalSignUp M1,
--M2 / TotalSignUp M2,
--M3 / TotalSignUp M3,
--M4 / TotalSignUp M4,
--M5 / TotalSignUp M5,
--M6 / TotalSignUp M6,
--M7 / TotalSignUp M7,
--M8 / TotalSignUp M8,
--M9 / TotalSignUp M9,
--M10 / TotalSignUp M10,
--M11 / TotalSignUp M11,
--M12 / TotalSignUp M12
FROM (
SELECT SignUpDate,
COUNT(DISTINCT ClientID) TotalSignUp,
SUM(CASE WHEN MonthActive = DATEADD(m, 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M2,
SUM(CASE WHEN MonthActive = DATEADD(m, 2, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M3,
SUM(CASE WHEN MonthActive = DATEADD(m, 3, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M4,
SUM(CASE WHEN MonthActive = DATEADD(m, 4, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M5,
SUM(CASE WHEN MonthActive = DATEADD(m, 5, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M6,
SUM(CASE WHEN MonthActive = DATEADD(m, 6, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M7,
SUM(CASE WHEN MonthActive = DATEADD(m, 7, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M8,
SUM(CASE WHEN MonthActive = DATEADD(m, 8, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M9,
SUM(CASE WHEN MonthActive = DATEADD(m, 9, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M10,
SUM(CASE WHEN MonthActive = DATEADD(m, 10, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M11,
SUM(CASE WHEN MonthActive = DATEADD(m, 11, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0)) THEN 1.0 ELSE 0.0 END) M12
How can I perform a loop for the select statement so that I can dynamically produce the number of M select statements as set by the #numMonths variable?
e.g., I will want the user of the report to be able to select the #numMonths as an input variable and they could say, want to go back 6 months, or 24 months. I would want the select statement to dynamically pivot in a loop depending on what the #numMonths value is.
To put it in other words I don't want to type out M1-M36 36 times just in case the user wants to go back 3 years, that will look so messy. I want a 1 line 'template' and a Counter + 1 until it reaches the number of #numMonths
DROP TABLE IF EXISTS ##TimePeriods
GO
DECLARE
#StartDate DATE,
#EndDate DATE,
#NumMonths INT,
#SQL NVARCHAR(MAX)
SET #NumMonths = 5
SET #StartDate = DATEADD(m, -#NumMonths, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
SET #EndDate = DATEADD(m, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
;WITH TimePeriods AS
(
SELECT #StartDate AS TheDate
UNION ALL
SELECT DATEADD(m,1,TheDate) FROM TimePeriods
WHERE TheDate < #EndDate
)
SELECT * INTO ##TimePeriods FROM TimePeriods
DECLARE #ColList VARCHAR(MAX) = ''
SELECT #ColList = STRING_AGG(QUOTENAME(TheDate),',')
FROM ##TimePeriods
SET #SQL =
'
SELECT
*
FROM
(
SELECT 1 AS TheData, *
FROM ##TimePeriods
-- JOIN YOUR DATA IN HERE
) SourceData
PIVOT
(MAX(TheData)
FOR TheDate IN ('+#ColList+')
)
AS PivotTable
'
EXEC (#SQL)
I have below T-SQL code used in SQL Server 2014.
This code produces 1000s of rows. But i need only top 50 rows (from Supplier Column) from this.
In the below code, if I use SELECT Top 50 s.[CusNo] Supplier then I am not getting desired results.
What changes needs to be done in the below code in order to get only Top 50 rows (of Supplier column) with out any change in the existing result.
SELECT s.[CusNo] Supplier,
RTRIM(CAST(s.[Customer] AS VARCHAR(50)) ) AS Name,
s.[ConcessionNo] Concession,
RTRIM(CAST(s.[ConcessionName] AS VARCHAR(50)) ) AS ConcessionName,
sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5)
then s.SELLINC else 0 end) ActualSales,
sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end) LastYrVariance,
(sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5) then s.SELLINC else 0 end))-
(sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */) then s.SELLINC else 0 end)) LastYrVariancePounds,
(IsNull(sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5)
then s.SELLINC else 0 end)-sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end),0)/NullIf(sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end),0))*100 LastYrVariancePercentage,
sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
then s.SELLINC else 0 end) YrToDateActual,
sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end) LastYrToDateActual,
(sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
then s.SELLINC else 0 end))
-
(sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end)) YrToDateVariancePounds,
((IsNull
(
(sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
then s.SELLINC else 0 end))
-
(sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end))
,0)
)
/
(NullIf
(
sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
then s.SELLINC else 0 end)
,0)))*100 LastYrToDateVariancePercentage
FROM [dbo].[CustomerReports] s
WHERE s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5 or s.SELLINC is null or s.SELLINC = '0'
GROUP BY s.[CusNo], s.[Customer], s.ConcessionNo, s.ConcessionName
order by YrToDateActual desc
When I run this Query In SSRS I get below results (Which is Correct). But displays all the data.
if I use SELECT Top 50 s.[CusNo] Supplier (Rest of the code is same) then I am getting the below,
So please see the highlighted section in both the images. When I add Top 50 in SQL code then few rows (in 3rd column ) which were present in 1st image is not present in 2nd image.
Try this:
With cte as (**your long query goes here**)
Select Top(50) * From cte
The sort order in your query is by YrToDateActual desc, so the TOP 50 is giving you the highest 50 YrToDateActual amounts. Your SSRS report is grouping by Supplier number and name, but doesn't look to be sorting by either of these columns, so I'm guessing that the order of these suppliers is somewhat random in the SSRS output. In this case I don't think you're ever going to be able to get the first 50 rows of your original SSRS output. The best you could do would be to also sort your SSRS output (e.g. by Supplier number) and then add this sort into your query (order by Supplier, YrToDateActual desc). Adding the TOP 50 into your query would cut off your new sorted SSRS output after the first 50 results.
What do you want to see in the report? Do you want to see the top 50 suppliers based on their total value (e.g. Actual £ Total) and then all the Concessions (as per your first screenshot) ?
If so, you will need to rank the suppliers by their total and select any that are rank 50 or below.
Here's a very simplified example that might help. In this I've used SUM() OVER() rather than using GROUP BY but this means you might want to change the SELECT to SELECT DISTINCT depending on you needs as you will get lots of duplicate rows. Obviously it's not got your lengthy case statements in but you should be able to adapt it easily.
SELECT * FROM
(
SELECT
x.*
, DENSE_RANK() OVER(ORDER BY SupplierTotal DESC) as SupplierRank
, DENSE_RANK() OVER(PARTITION BY SupplierID ORDER BY ConcessionTotal DESC) as ConcessionRank
FROM
( SELECT DISTINCT
SupplierID, ConcessionID, MyOtherRequiredColumns,
SUM(myValue) OVER(PARTITION BY SupplierID) AS SupplierTotal,
SUM(myValue) OVER(PARTITION BY SupplierID, ConcessionID) AS ConcessionTotal
FROM MyTable
) x
) z
WHERE SupplierRank <= 50
This will also rank the concessions within supplier which might be useful..
You will also have to change you report where you have summed the values up (e.g. 99,389 in Actual £ column). Instead of summing the rows below, change the expression to be =FIRST(Fields!SupplierTotal.Value)
I am using SSRS 2014.
I want to display only first 50 rows. What will be the expression for this please.
I used top n functionality it doesn't work. So I thought to take another route to achieve the result via expression.
Below image with out adding 'top n'. In the below image I wanted to display only first 50 rows in Supplier column
Below image with adding 'top n'. Then results are changed. Few rows in Concession column are missing.
SQL code is,
SELECT s.[CusNo] Supplier,
RTRIM(CAST(s.[Customer] AS VARCHAR(50)) ) AS Name,
s.[ConcessionNo] Concession,
RTRIM(CAST(s.[ConcessionName] AS VARCHAR(50)) ) AS ConcessionName,
sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5)
then s.SELLINC else 0 end) ActualSales,
sum(case when s.Date
BETWEEN
convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7 * 7, '19000107'), 120)
AND
Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
then s.SELLINC else 0 end) YrToDateActual
FROM [dbo].[CustomerReports] s
WHERE s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5 or s.SELLINC is null or s.SELLINC = '0'
GROUP BY s.[CusNo], s.[Customer], s.ConcessionNo, s.ConcessionName
order by YrToDateActual desc
Any help please?
If you want the first 50 suppliers, you should be able to use the following expression in the row visibility property:
=RunningValue(Fields!Supplier.Value, CountDistinct,"YourDataset") > 50
The report should perform better if you put this expression as a filter on the group instead of a row visibility expression, but at the moment I can't test to confirm whether this expression would be allowed there.
Modify your ORDER BY clause to:
ORDER BY s.[CusNo], YrToDateActual desc
I have a SQL statement that has multiple 'count case when' statements my issue is I don't want to show results in all results are null
if a source description returns null for all four count statements I don't want to show the source description row. i'm stuck and not sure how this can be achieved
any advice would be greatly appreciated
here is my sql
SELECT CASE WHEN CHARINDEX('**-**', SourceDescription) > 0 then
SourceDescription
WHEN CHARINDEX('-', SourceDescription) > 0 THEN
LEFT(SourceDescription, CHARINDEX('-', SourceDescription)-1) else
SourceDescription END as 'Source',
count(case when StartDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0) THEN 1 ELSE NULL END) as 'Total',
count (case when CompletionDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0) THEN 1 ELSE NULL END)as 'Completed',
count (case when ClosedDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0) and CompletionDate is null then 1 ELSE null END)as 'Closed',
count (case when ExchangeDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0)then 1 else NULL END)as 'Exchanged'
from
CASE_record with (nolock)
inner join case_warehouse with (nolock) on CASE_record.casekey = case_warehouse.casekey
WHERE TypeCode = 'b'
group by CASE WHEN CHARINDEX('**-**', SourceDescription) > 0 then
SourceDescription
WHEN CHARINDEX('-',SourceDescription) > 0 THEN
LEFT(SourceDescription, CHARINDEX('-', SourceDescription)-1) else
SourceDescription END
You could just include each of the predicates in your where clause to ensure you only return records that match at least one criterion:
WHERE TypeCode = 'b'
AND ( StartDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0)
OR CompletionDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0)
OR (ClosedDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0) AND CompletionDate IS NULL)
OR ExchangeDate = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()) - 1, 0)
)