Getting totals in SSRS today(), Month, Quarterly and year to date - reporting-services

Data from main table
i have the following SQL query:
SELECT
ISNULL(SUM(intIntrosHot), 0) Hot, ISNULL(SUM(intIntrosCold), 0) Cold,
ISNULL(ISNULL(SUM(intIntrosHot), 0) + ISNULL(SUM(intIntrosCold), 0),0) Total
FROM tblUBMReport
WHERE tblUBMReport.intProp IN 865
---AND dtReport BETWEEN #StartDate AND #EndDate;
but for the life of me, I have no idea how to get or design my table in SSRS for total hot leads for today, total hot leads for the month, total hot leads for this quarter, and total hot leads for this year. I need to have my table look like the picture below any help will be appreciated.
This is an image showing what I'm looking to get as an outcome:
enter image description here

You should be able to get the results desired by grouping your data by date and then filtering the data by the date range.
SELECT dtReport,
ISNULL(SUM(intIntrosHot), 0) Hot, ISNULL(SUM(intIntrosCold), 0) Cold,
ISNULL(ISNULL(SUM(intIntrosHot), 0) + ISNULL(SUM(intIntrosCold), 0),0) Total
FROM tblUBMReport
WHERE tblUBMReport.intProp IN 865
GROUP BY dtReport
Then add the Expressions for each date range and each temp:
CURRENT DATE
=SUM(IIF(Fields!dtReport.Value = TODAY, Fields!Cold.Value, 0))
MTD
=SUM(IIF(Fields!dtReport.Value >= TODAY.AddDays(1 - TODAY.Day) AND Fields!dtReport.Value <= TODAY, Fields!Cold.Value, 0))
QTD
=SUM(IIF(Fields!dtReport.Value >= DateSerial(Today.Year, (Int((Today.Month - 1) / 3) + 1, 1) AND Fields!dtReport.Value <= TODAY,
Fields!Cold.Value,
0)
)
If you hot and cold were on separate lines, it would be easier since the table could be grouped by the Hot/Cold.

I was able to solve my issue and wanted to post it in here so other people who run across the issue can use the solution.
SELECT
'Hot' AS strType,
SUM(CASE WHEN dtReport = #EndDate THEN intIntrosHot ELSE 0 END) AS intDT,
SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND #EndDate THEN intIntrosHot ELSE 0 END) AS intMTD,
SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND #EndDate THEN intIntrosHot ELSE 0 END) AS intQTD,
SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND #EndDate THEN intIntrosHot ELSE 0 END) AS intYTD
FROM
tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = #EndDate
WHERE
intProp IN (#Props) AND dtReport BETWEEN #StartDate AND #EndDate
UNION
SELECT
'Cold' AS strType,
SUM(CASE WHEN dtReport = #EndDate THEN intIntrosCold ELSE 0 END) AS intDT,
SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfMonth AND #EndDate THEN intIntrosCold ELSE 0 END) AS intMTD,
SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfQuarter AND #EndDate THEN intIntrosCold ELSE 0 END) AS intQTD,
SUM(CASE WHEN dtReport BETWEEN tblDate.dtFirstDayOfYear AND #EndDate THEN intIntrosCold ELSE 0 END) AS intYTD
FROM
tblUBMReport WITH (NOLOCK)
INNER JOIN tblDate WITH (NOLOCK) ON tblDate.dtDate = #EndDate
WHERE
intProp IN (#Props) AND dtReport BETWEEN #StartDate AND #EndDate

Related

MySQL : using sum in( case when ) statement shows 0 as result

new to MySQL..so pls help me out with this basic code..
i have a query something like this...
select weekofyear(id_time),
(id),
#Tat1:=exp1,
#Tat2:=exp2,
#check1:=exp3,
#check2:=exp4,
(case when #check2=0 then
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+10))) then 1 else 0 end)
else
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+20))) then 1 else 0 end)
end) as BO
from datb
where cid=18
and id_time between '2019-11-01 06:00:00' and '2019-11-25 06:00:00'
and it gives correct results as--here
however i want to use sum after case when statement so that I can get total values where BO=1 and group by week of year , so i made following changes-
select weekofyear(id_time),
count(id),
#Tat1:=exp1,
#Tat2:=exp2,
#check1:=exp3,
#check2:=exp4,
sum(case when #check2=0 then
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+10))) then 1 else 0 end)
else
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+20))) then 1 else 0 end)
end) as BO
from datb
where cid=18
and id_time between '2019-11-01 06:00:00' and '2019-11-25 06:00:00'
group by weekofyear(id_time)
but it always returns 0 as output.
Output --here 2
Please help , I don't know what am I doing wrong here.
Thanx !
As others have already said, session variables can be unpredictable (especially when aggregation gets mixed in). That said, it doesn't look like you're using the session variables to carry over values from one row to the next (as is often done), but to just make aliases of sorts for calculations you don't want to repeat.
A better way to handle that is just through subqueries.
SELECT woy, id, Tat1, Tat2, check1, check2
, CASE
WHEN check2=0 THEN (
CASE
WHEN (Tat1>(Tat2+30) OR (check1=1 AND (Tat1>Tat2+10))) THEN 1
ELSE 0
END
)
ELSE (
CASE WHEN (Tat1>(Tat2+30) OR (check1=1 AND (Tat1>Tat2+20))) THEN 1
ELSE 0
END
)
END AS BO
FROM (
SELECT WEEKOFYEAR(id_time) AS woy
, id
, exp1 AS Tat1
, exp2 AS Tat2
, exp3 AS check1
, exp4 AS check2
FROM datb
WHERE cid=18
AND id_time BETWEEN '2019-11-01 06:00:00' AND '2019-11-25 06:00:00'
) AS subQ
;
You can then tweak the above query for aggregation, or use it as a subquery for an aggregating outer query.

SSRS calculate % of desired colums in column group in matrix

SELECT PatchPhase.PhaseName, MAX(PatchPhase.Sequence) AS seq,
PatchState.Application, PatchState.Objectname, PatchState.Jobname,
PatchState.Streamname, COUNT(*) AS Total,
PatchState.Jobdescription,
PatchState.Status, PatchState.Timestamp
FROM PatchState
INNER JOIN PatchPhase ON PatchState.Phase = PatchPhase.PhaseTech
WHERE (PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application)) AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY PatchPhase.PhaseName, PatchState.Application, PatchState.Objectname,
PatchState.Jobname, PatchState.Streamname, PatchState.Jobdescription, PatchState.Status,
PatchState.Timestamp
ORDER BY PatchState.Application
I am working on matrix and I have column group of status which contains 3 columns (planned, running,completed). I want to sum planned+completed and divide by total column.
Total column is outside the column group.
I do find some answers but I did not get how should I use in my code
can someone please help?
Try using this to aggregate, and then use a table instead of a matrix. You will not need a column group now. As I cannot run the sql I can't promise it is perfect but perhaps you could have a little play with it if not. It should offer a guide if nothing else.
SELECT
PatchPhase.PhaseName,
MAX(PatchPhase.Sequence) AS seq,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) as 'Planned',
SUM(case when PatchState.Status = 'Running' then 1 else 0 end) as 'Running',
SUM(case when PatchState.Status = 'Completed' then 1 else 0 end) as 'Completed',
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end)) /
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end) + SUM(case when PatchState.Status = 'Completed' then 1 else 0 end)) as totalPercentage
PatchState.Timestamp
FROM
PatchState
INNER JOIN PatchPhase
ON PatchState.Phase = PatchPhase.PhaseTech
WHERE
(PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application))
AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY
PatchPhase.PhaseName,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
PatchState.Status,
PatchState.Timestamp
ORDER BY
PatchState.Application
You can get a value displayed in a text box from expression =Reportitems!Textbox133.Value
instead of textbox133 in the above expression you have to give the textbox name of the textbox from which you want the value. In your case the total column

Case in where condition to calculate individual totals

select
Count(*)
from gl
join TRVMAINDATA msit
on gl.TRANSACTIONID = msit.CHARGETRANSACTIONID
where gl.CREATEDDATETIME >= DATEADD(MONTH,-2,getdate()) AND
datediff(DAY,gl.BUSINESSPROCESSDATE,gl.CREATEDDATETIME)>=4 AND
gl.MARKETCODE In('535','532','056','050','039','036','034','033','030','029','027','023','022','021','018','015','012','011','010','009','007','006','005','002','001' ) and DATEADD(MONTH,DATEDIFF(MONTH,0,gl.CREATEDDATETIME),0) = '2/1/2017 12:00:00 AM'
The above code gives the total for market code mentioned.
But i also want the market code for other code which doesn't belog here. for example
select
Count(*)
from gl
join TRVMAINDATA msit
on gl.TRANSACTIONID = msit.CHARGETRANSACTIONID
where gl.CREATEDDATETIME >= DATEADD(MONTH,-6,getdate()) AND
datediff(DAY,gl.BUSINESSPROCESSDATE,gl.CREATEDDATETIME)>=0
and
DATEADD(MONTH,DATEDIFF(MONTH,0,gl.CREATEDDATETIME),0) = '2/1/2017 12:00:00 AM'
AND
(case
when gl.MARKETCODE In('535','532','056','050','039','036','034','033','030','029','027','023','022','021','018','015','012','011','010','009','007','006','005','002','001') then 'Proprietary'
when gl.MARKETCODE='037' then 'US'
else 'partner'
end )
Is it possible to calculate totals of other market codes in one sql query or do i have to calculate individually?
I think you would like to do count with case when in select statement like this:
SELECT COUNT(CASE
WHEN gl.marketcode IN ( '535', '532', '056', '050',
'039', '036', '034', '033',
'030', '029', '027', '023',
'022', '021', '018', '015',
'012', '011', '010', '009',
'007', '006', '005', '002', '001' ) THEN 1
ELSE NULL
END) AS `Proprietary`,
COUNT(CASE
WHEN gl.marketcode = '037' THEN 1
ELSE NULL
END) AS `US`,
COUNT(CASE
WHEN gl.marketcode NOT IN ( '535', '532', '056', '050',
'039', '036', '034', '033',
'030', '029', '027', '023',
'022', '021', '018', '015',
'012', '011', '010', '009',
'007', '006', '005', '002',
'001', '037' ) THEN 1
ELSE NULL
END) AS `partner`
FROM gl
JOIN trvmaindata msit
ON gl.transactionid = msit.chargetransactionid
WHERE gl.createddatetime >= DATEADD(month, -2, GETDATE())
AND DATEDIFF(day, gl.businessprocessdate, gl.createddatetime) >= 4
AND DATEADD(month, DATEDIFF(month, 0, gl.createddatetime), 0) = '2/1/2017 12:00:00 AM'

MySQL using Sum and Case

I'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.
BusinessUnit OrderDate Canceled
UnitA 1/15/2013 N
UnitA 10/1/2013 N
UnitB 10/15/2013 N
UnitB 10/22/2013 N
UnitB 10/22/2013 N
Based on the records above, I'd like the result to appear like below
BusinessUnit TodaysOrders ThisMonthsOrders ThisYearsOrders
UnitA 0 1 2
UnitB 2 3 3
My current code is below. It's giving me error (something about DatabaseName.sum does not exist. Check the
Function Name Parsing and Resolution' section... )
Select
SUM (CASE WHEN (OrderDate)=DATE(NOW()) THEN 1 ELSE 0 END) AS TodaysOrders,
SUM (CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) AND MONTH(OrderDate) = MONTH(CURDATE()) THEN 1 ELSE 0 END) AS ThisMonthsOrders,
SUM (CASE WHEN YEAR(main_order_managers.creation_date) = YEAR(CURDATE()) THEN 1 ELSE 0 END) AS ThisYearsOrders
code continues
FROM OrderTable WHERE OrderTable.Canceled. <> 'Y';
Is Sum Case the best use here?
The error is caused by the space between function name and parenthesis
SUM (CASE WHEN ...
^^
Read more Function Name Parsing and Resolution
Try
SELECT BusinessUnit,
SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
FROM OrderTable
WHERE Canceled <> 'Y'
GROUP BY BusinessUnit
Here is SQLFiddle demo

SQL query - How to exclude WHERE for specific field

See the SQL query below, it work fine. It calculate the number of Yes, NOT, Other and the number of matching mobile number [Sales field] (D.MobileNo = S.mobile)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
I want to exclude WHERE for the Sales field - it should not be part from CheckDate. Meaning it should check any record in the dairy table without CheckDate for the Sales field.
How can that be done?
If you really want those results in only one query, this might do the trick.
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL,
SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
GROUP BY D.Username
ORDER BY TOTAL DESC
Note that "TOTAL" will count all rows (including those who where not within your CheckDate range), TOTALINCHECKDATE return the same value as in your previous query.
Obviously, this can still be optimized.
Assuming username exists also in SALES table
SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not`
, SUM(Other) As Other, SUM(sales) Sales, SUM(total)
FROM (
-- get diary data
SELECT username,mobileNo As mobile,
CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes,
CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`,
CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other,
0 As sales, 1 as total
FROM dairy as D
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
UNION ALL
-- get all sales
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total
FROM sales
WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
) AS a
GROUP BY Username
Also try your original query with date addition
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 ) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC