SSRS Report Multi Parameters (start date,end date, MeterId, Displayby) - sql

In my SSRS report there are 4 parameters StartDate, EndDate, MeterId, & DisplayBy
Start Date: datetime datatype
EndDate : datetime datatype
MeterId : is a drop down list and this will populate based on SQL query
DisplayBy: is a drop down list and this has the following values (Hour,day,Month & year)
The Database that stores hourly values for Meters, the following are the DB table columns: (MeterId,ReadingDate,Hours,Quantity,Price)
When I select the startdate, end date and the meter Id and display i want to show report based on the startdate & enddate and then by display values.
If the display is hour, the we got display all the 24 hour values for the MeterId,Quantity, Price for the date range.
If the display is day, we got display total quantity and total price for the MeterId for that date range.
If the display is Month, we got display total quantity and total price for the MeterId for that date range.
If the display is Year, we got display total quantity and total price for the MeterId for that date range. Say for example If i select start date as 1-1-2016 and end date as 12-31-2016. My result should show 12 rows for each month with their total Quantity, Total price for that particular MeterID.
my DB table stores all the hourly values i know how to show the values on screen if the user selects the display dropdown as hour. But, dont know how to show the result for day/month/year or how to group it. Do I need to use "case" statement and if so what should i need to give on display parameters.
Please suggest your idea...
Row Grouping:
SELECT I.CustomerName, I.ReadingDate, I.IntegratedHour, I.IntegratedUsage, I.IntegratedGeneration, DATEPART(dd, I.ReadingDate) AS [Reading Day], DATEPART(mm,
I.ReadingDate) AS [Reading Month], DATEPART(yyyy, I.ReadingDate) AS [Reading Year]
FROM IntegratedHour_MV90 AS I INNER JOIN
CustRptMeterExtract AS CT ON CT.CustomerName = I.CustomerName
WHERE (I.ReadingDate >= #StartDate) AND (I.ReadingDate <= #EndDate) AND (I.CustomerName IN (#FacilityName))
Expected Result:
SSRS Current Output: Doesnot match

Depending on your layout you could set row grouping to an expression something like this
=SWITCH
(
Parameters!ReportBy.Value=1, Fields!Hour.Value,
Parameters!ReportBy.Value=2, Fields!Day.Value,
Parameters!ReportBy.Value=3, Fields!Month.Value,
Parameters!ReportBy.Value=4, Fields!Year.Value,
True, 0)
This assumes you have already have the hours/days/months/years in your dataset, if not then you would have to replace the field references with expressions to return the relevant month etc.
Based on what I can see above you'll need to add a grouping level for Customer before the group expression. Also, you Quantity expression should be a sum something like this
=SUM(FIelds!IntegratedGeneration.Value)
You may still have a problem though. I'm assuming Price is a unit price, so it does not make sense to sum that too. To get round this, you should calculate the LineValue (Qty * Price) in your dataset then change the price expression to be something like
=(SUM(FIelds!LineValue.Value)/SUM(Fields!IntegratedGeneratio‌​n.Value))
and this will give you the average price.
However, this may be slow and personally I would do the work in your dataset. Again assuming you have the months, years in your table then you could do something like this.
--DECLARE #ReportBy int = 1 -- uncomment for testing
select
MeterID, Price
, CASE #ReportBy
WHEN 1 THEN [Month]
WHEN 2 THEN [Year]
ELSE NULL
END AS GroupByColumn
INTO #t
from dbo.MyDataTable
SELECT
GroupByColumn
, SUM(Price) as Price
FROM #t
Group BY GroupByColumn
Order by GroupByColumn
This assumes your report parameter is called ReportBy, if not just swap the name out.

Related

Selecting another column from a query with a different date filter

I am working with some sales data and pulling the metrics for a particular week I am defining in the filter.
However, I want to add another column (first_sale_date) to my query. This will show the first time this asin/mp combo shows up in my table regardless of the date filter I am trying to pull the other metrics for.
Because I am already
filtering by date I don't know how to look back to all of the data in the table to find it's first appearance as it is before the week I am filtering for.
select date,
,asin
,marketplace
,SUM(ordered_product_sales) as OPS
,SUM(cogs) as cogs
**,min(date) as first_sale_date**
from prod.sales
where date > '2023-01-01'
group by 1,2,3,4
you can use a correlated subquery for this
select year
,week
,asin
,marketplace
,SUM(ordered_product_sales) as OPS
,SUM(cogs) as cogs
,( SELECT MIN(date) FROM sales sal1 WHERe sal1.asin = sal.asin and sal1.marketplace = sal.marketplace) as first_sale_date
from prod.sales sal
where year = '2023' and week = '1'
group by 1,2,3,4

To show date on which highest total price is made in ssrs

I have a table with the TotalPrice column and BookedOn column which contains a date in which a particular order was placed. I want to generate a report in SSRS that will show the date on which the highest sales were made.
There are several orders made on a particular date and I want to sum all the price and then compare in which date highest sales were made.
select top 1 CONVERT(date, BookedOn) Date, sum(TotalPrice) SumTotalPrice
from your_table_name
group by BookedOn
order by SumTotalPrice desc
I used convert because didn't know the format of your date column(whether it is datetime or date), you can use just date column otherwise

Is there a way to limit a sum up to a calculated date in a table?

I have a table with SentDate and RefundAmounts. I would like to sum up the amounts on each row from the date until a year into the future for every line.
In the example below I would like to add a column that says sum for the year.
This sum should be for the first line the sum of refunds from '2006-12-14' until '2007-12-14' which would be 3696,22 as there were no refunds during that period.
The second row would be from '2007-12-24' until '2008-12-24' which would be 463,05
SentDate YearAhead RefundAmount
2006-12-14 2007-12-14 3696,22
2007-12-24 2008-12-24 394,35
2008-12-18 2009-12-18 44,33
2008-12-19 2009-12-19 24,37
2009-12-16 2010-12-16 21,88
I have tried something along the lines of
select SentDate, dateadd(year,1,sentdate) YearAhead, SumRefund
from table
but I have no idea how to get the annual future sum for each row
Thanks for the suggestion. The final result should look as follows:
SentDate YearAhead RefundAmount SumForYear
2006/12/14 2007/12/14 3696,22 3696,22
2007/12/24 2008/12/24 394,35 463,05
2008/12/18 2009/12/18 44,33 90,58
2008/12/19 2009/12/19 24,37 46,25
2009/12/16 2010/12/16 21,88 21,88
You need to join with a BETWEEN to gather all rows that compose that period, then use GROUP BY with SUM to the result.
select
T.SentDate,
dateadd(year, 1, T.SentDate) YearAhead,
SUM(P.SumRefund) AS TotalOverYear
from
YourTable AS T
INNER JOIN YourTable AS P ON P.SentDate BETWEEN T.SendDate AND DATEADD(YEAR, 1, T.SendDate)
GROUP BY
T.SentDate
Something like this should suite your needs if you always need a complete year:
select sum(refund) as refundAmounts,year(min(SentDate)) as year from yourTable group by year(SentDate)
the year function gets only the year part from a datetime row and with group by you can split the aggregation of sum and min function to different groups.

How to create a dynamic where clause in sql?

So I have created a table that has the following columns from a transaction table with all customer purchase records:
1. Month-Year, 2.Customer ID, 3. Number of Transactions in that month.
I'm trying to create a table that has the output of
1. Month-Year, 2. Number of active customers defined by having at least 1 purchase in the previous year.
The code that I have currently is this but the case when obviously only capturing one date and the where clause isn't dynamic. Would really appreciate your help.
select month_start_date, cust_ID,
(case when month_start_Date between date and add_months(date, -12) then count(cust_ID) else 0 end) as active
from myserver.mytable
where
month_start_Date>add_months(month_start_date,-12)
group by 1,2
EDIT: I'm just trying to put a flag next to a customer if they are active in each month defined as having at least one transaction in the last year thanks!
You might use Teradata's proprietary EXPAND ON synax for creating time series:
SELECT month_start_date, COUNT(*)
FROM
( -- create one row for every month within the next year
-- after a customer's transaction
SELECT DISTINCT
BEGIN(pd) AS month_start_date,
cust_ID
FROM myserver.mytable
EXPAND ON PERIOD(month_start_date, ADD_MONTHS(month_start_date,12)) AS pd
BY ANCHOR MONTH_BEGIN -- every 1st of month
FOR PERIOD (DATE - 500, DATE) -- use this to restrict to a specific date range
) AS dt
GROUP BY month_start_date
ORDER BY month_start_date

Display a rolling 12 weeks chart in SSRS report

I am calling the data query in ssrs like this:
SELECT * FROM [DATABASE].[dbo].[mytable]
So, the current week is the last week from the query (e.g. 3/31 - 4/4) and each number represents the week before until we have reached the 12 weeks prior to this week and display in a point chart.
How can I accomplish grouping all the visits for all locations by weeks and adding it to the chart?
I suggest updating your SQL query to Group by a descending Dense_Rank of DatePart(Week,ARRIVED_DATE). In this example, I have one column for Visits because I couldn't tell which columns you were using to get your Visit count:
-- load some test data
if object_id('tempdb..#MyTable') is not null
drop table #MyTable
create table #MyTable(ARRIVED_DATE datetime,Visits int)
while (select count(*) from #MyTable) < 1000
begin
insert into #MyTable values
(dateadd(day,round(rand()*100,0),'2014-01-01'),round(rand()*1000,0))
end
-- Sum Visits by WeekNumber relative to today's WeekNumber
select
dense_rank() over(order by datepart(week,ARRIVED_DATE) desc) [Week],
sum(Visits) Visits
from #MyTable
where datepart(week,ARRIVED_DATE) >= datepart(week,getdate()) - 11
group by datepart(week,ARRIVED_DATE)
order by datepart(week,ARRIVED_DATE)
Let me know if I can provide any more detail to help you out.
You are going to want to do the grouping of the visits within SQL. You should be able to add a calculated column to your table which is something like WorkWeek and it should be calculated on the days difference from a certain day such as Sunday. This column will then by your X value rather than the date field you were using.
Here is a good article that goes into first day of week: First Day of Week