Average in SSRS - sql

I am having some issues with an avg subquery in my report. I am trying to get the count(distinct(d.orderno)) value AND the avg(count(distinct(d.orderno))) value so that way I can get a comparison into a percentage between the count and the average, but it is simply not working. Please take a look at my code:
SELECT
d.packingoperator,
d.packingunit,
datepart(hh, d.datetimepacked) as hourPacked,
avg(count(distinct(d.orderno))) as targetrate,
count(distinct(d.orderno)) as orderspacked,
(select count(distinct(d1.orderno))
from mck_hvs.oldorderdetails d1 with (nolock)
where d1.refrigerate != 'N'
and convert(date, d1.datetimepacked) = convert(date, #date)
and d1.packingoperator = d.packingoperator
and datepart(hh, d1.datetimepacked) = datepart(hh,d.datetimepacked)) as coldcount
FROM
mck_hvs.oldorderdetails d with (nolock)
WHERE
convert(date, d.datetimepacked) = convert(date, #date)
GROUP BY
d.packingoperator,
datepart(hh, d.datetimepacked),
d.packingunit
ORDER BY
d.packingoperator,
datepart(hh, d.datetimepacked)
I have also tried this option as well:
SELECT
d.packingoperator,
d.packingunit,
datepart(hh, d.datetimepacked) as hourPacked,
count(distinct(d.orderno)) as orderspacked,
(select count(distinct(d1.orderno))
from mck_hvs.oldorderdetails d1 with (nolock)
where d1.refrigerate != 'N'
and convert(date, d1.datetimepacked) = convert(date, #date)
and d1.packingoperator = d.packingoperator
and datepart(hh, d1.datetimepacked) = datepart(hh,d.datetimepacked)) as coldcount,
(select avg(target) from (
select count(distinct(d2.orderno)) as target
from mck_hvs.oldorderdetails d2 with( nolock )
where convert(date, d2.datetimepacked) = convert(date, #date)
and d2.packingoperator = d.packingoperator
and datepart(hh, d2.datetimepacked) = datepart(hh, d.datetimepacked)) as targetrate
FROM
mck_hvs.oldorderdetails d with (nolock)
WHERE
convert(date, d.datetimepacked) = convert(date, #date)
GROUP BY
d.packingoperator,
datepart(hh, d.datetimepacked),
d.packingunit
ORDER BY
d.packingoperator,
datepart(hh, d.datetimepacked)

Have you thought about breaking out some of your sub-aggregations into cross-applies? Please see below. This is very very inelegant code, however it might be able to get you on the right track.
SELECT
d.packingoperator,
d.packingunit,
DATEPART(hh, d.datetimepacked) as hourPacked,
AVG(ct.orderNoCt) as targetrate,
COUNT(dt.orderNoDt) as orderspacked,
(
SELECT
COUNT(DISTINCT(d1.orderno))
FROM mck_hvs.oldorderdetails d1 with (NOLOCK)
where d1.refrigerate != 'N'
and CONVERT(date, d1.datetimepacked) = CONVERT(date, #date)
and d1.packingoperator = d.packingoperator
and DATEPART(hh, d1.datetimepacked) = DATEPART(hh,d.datetimepacked)
) as coldcount
FROM
mck_hvs.oldorderdetails d with (nolock)
CROSS APPLY
(
SELECT
COUNT(DISTINCT d2.orderNo) as orderNoCt
FROM mck_hvs.oldorderdetails d2 with (nolock)
WHERE
d2.packingoperator = d.packingoperator
AND d2.packingunit = d.packingunit
and DATEPART(hh, d2.datetimepacked) = DATEPART(hh, d.datetimepacked)
) ct
CROSS APPLY
(
SELECT
DISTINCT d3.orderNo as orderNoDt
FROM mck_hvs.oldorderdetails d3 with (nolock)
WHERE
d3.packingoperator = d.packingoperator
AND d3.packingunit = d.packingunit
and DATEPART(hh, d3.datetimepacked) = DATEPART(hh, d.datetimepacked)
) dt
WHERE
CONVERT(DATE, d.datetimepacked) = CONVERT(DATE, #date)
GROUP BY
d.packingoperator
,DATEPART(hh, d.datetimepacked)
,d.packingunit
ORDER BY
d.packingoperator
,DATEPART(hh, d.datetimepacked)

Related

Divide by Case When

I'm currently doing some work on a 6 month rolling report, but i seem to be a little bit stuck on one area.
I have one column Liveatcutoff_PC it will be populated by 1 for live or 0 for not live. If it's 0 it means it's cancelled. I want to work out the cancellation % for the date range in my code but I'm not having much luck.
SELECT
UserID
,Agent
,COUNT(CASE WHEN SaleDate>=DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-6, 0)
AND SaleDate < DATEADD(MONTH, DATEDIFF(MONTH, -6, GETDATE())-6, 0)
AND LiveAtCutOff_PC='0' THEN LiveAtCutOff_PC END)
FROM PDS_SALES_PMI
WHERE SaleDate>='2019-01-01'
AND Leaver='0'
GROUP BY UserID, Agent
Help much appreciated.
If I understand correctly, you can use AVG():
AVG(CASE WHEN SaleDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-6, 0) AND
SaleDate < DATEADD(MONTH, DATEDIFF(MONTH, -6, GETDATE())-6, 0) THEN LiveAtCutOff_PC * 1.0
END) as LiveAtCutoff_PC_ratio
Your question specifies that LiveAtCutOff_PC has values of 0 and 1, which implies that the field is a number. However, the code compares it to a string, which suggests otherwise. If the values are only 0 and 1 then the above should work for both numbers and strings (although for bit you would need to convert the value).
So since you requirement is to find out the percentage of canceled sales then you don't have to do it in one query. Case will just calculate everything, including nulls(that will come up because you don't have 'else')
you might want to try subquery instead or use CTE.
CTE example
with CTE as (
Select UserID
,Agent
,COUNT(*) as Canceled
from PDS_SALES_PMI
where SaleDate >=DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-6, 0)
AND SaleDate < DATEADD(MONTH, DATEDIFF(MONTH, -6, GETDATE())-6, 0)
AND LiveAtCutOff_PC='0'
AND SaleDate >='2019-01-01'
AND Leaver = 0
group by UserID, AgentID)
Select a.UserID
,a.AgentID
,Canceled/COUNT(*) * 100.0 as Canceled_Percent
From PDS_SALES_PMI a INNER JOIN CTE
on a.AgentID = CTE.AgentID
and a.UserID = CTE.UserID
Where SaleDate >=DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-6, 0)
AND SaleDate < DATEADD(MONTH, DATEDIFF(MONTH, -6, GETDATE())-6, 0)
AND SaleDate >='2019-01-01'
AND Leaver = 0
Group by a.UserID, a.AgentID
Subquery Example
Select a.UserID
,a.AgentID
,Canceled/COUNT(*) * 100.0 as Canceled_Percent
From PDS_SALES_PMI a INNER JOIN (
Select UserID
,Agent
,COUNT(*) as Canceled
from PDS_SALES_PMI
where SaleDate >=DATEADD(MONTH, DATEDIFF(MONTH, 0,GETDATE())-6, 0)
AND SaleDate < DATEADD(MONTH, DATEDIFF(MONTH, -6, GETDATE())-6, 0)
AND LiveAtCutOff_PC='0'
AND SaleDate >='2019-01-01'
AND Leaver = 0
group by UserID, AgentID) as Tmp
on a.AgentID = Tmp.AgentID
and a.UserID = Tmp.UserID
Where SaleDate >=DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-6, 0)
AND SaleDate < DATEADD(MONTH, DATEDIFF(MONTH, -6, GETDATE())-6, 0)
AND SaleDate >='2019-01-01'
AND Leaver = 0
Group by a.UserID, a.AgentID

Simply SQL query

I have a SQL script and would like to have multiple counts depending on different where clauses, instead of repeating such as this is there a way I can simplify this?
select
UnverifiedEmails =
(
select count(distinct c.ContactRef)
from ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
WHERE DateVerified IS NULL
),
VerifiedEmails =
(
select count(distinct c.ContactRef)
from ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
WHERE DateVerified IS NOT NULL
),
LastMonthVerified =
(
select count(distinct c.ContactRef)
from GDPR_ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
WHERE DateVerified IS NOT NULL
AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
),
LastMonthUnverified =
(
select count(distinct c.ContactRef)
from ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
WHERE DateVerified IS NULL
AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
)
You can do aggregation with single SELECT statement :
SELECT COUNT(DISTINCT CASE WHEN DateVerified IS NULL THEN c.ContactRef END) UnverifiedEmails,
COUNT(DISTINCT CASE WHEN DateVerified IS NOT NULL THEN c.ContactRef END) VerifiedEmails,
COUNT(DISTINCT CASE WHEN (DateVerified IS NOT NULL AND
DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
)
THEN c.ContactRef
END) LastMonthVerified,
COUNT(DISTINCT CASE WHEN (DateVerified IS NULL AND
DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
)
THEN c.ContactRef
END) LastMonthUnverified
FROM ContactEmailAddressVerification c LEFT JOIN
EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID;
You may use conditional aggregation:
SELECT
COUNT(DISTINCT CASE WHEN DateVerified IS NULL
THEN c.ContactRef END) AS UnverifiedEmails,
COUNT(DISTINCT CASE WHEN DateVerified IS NOT NULL
THEN c.ContactRef END) AS VerifiedEmails,
COUNT(DISTINCT CASE WHEN DateVerified IS NOT NULL AND
DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, GETDATE()))
THEN c.ContactRef END) AS LastMonthVerified,
COUNT(DISTINCT CASE WHEN DateVerified IS NULL AND
DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, GETDATE()))
THEN c.ContactRef END) AS LastMonthUnverified
FROM ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID;
The idea here is to make a single pass over the joined tables, and then take counts/sums conditionally, depending on the logic in each of the WHERE clauses of your original query.
use case when don't need multiple sub-query
select count(distinct case when DateVerified IS NULL then c.ContactRef end) UnverifiedEmails ,
count(distinct case when DateVerified IS not NULL then c.ContactRef end) VerifiedEmails,
count(distinct case when DateVerified IS NOT NULL
AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
then c.ContactRef end ) LastMonthVerified,
count(distinct case when DateVerified IS NULL
AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
then c.ContactRef end) LastMonthUnverified
from ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
ON e.EmailAddressVerificationID = c.EmailAddressVerificationID

Remove old records based on date SQL

Here is my query
SELECT
cf.CLIENTID, p.Id as ProfileID, t.TITLEDESC as Title,
cf.ClntForenme as Name, cf.CLNTSURNME as Surname,
pm.Lender, pm.Product, pm.LenderReference,
pm.AmountRequested as LoanAmount,
pm.DateCompleted,
CASE
WHEN pm.DateCompleted BETWEEN (SELECT DATEADD(YEAR, -1, GETDATE())) AND GETDATE()
THEN 'Completed under a year ago'
WHEN pm.DateCompleted BETWEEN (SELECT DATEADD(YEAR, -2, GETDATE())) AND (SELECT DATEADD(YEAR, -1, GETDATE()))
THEN 'Backlog WOM 1 Year'
WHEN pm.DateCompleted BETWEEN (SELECT DATEADD(YEAR, -3, GETDATE())) AND (SELECT DATEADD(YEAR, -2, GETDATE()))
THEN 'Backlog WOM 2 Year'
WHEN pm.DateCompleted BETWEEN (SELECT DATEADD(YEAR, -4, GETDATE())) AND (SELECT DATEADD(YEAR, -3, GETDATE()))
THEN 'Backlog WOM 3 Year'
ELSE ''
END Source,
CASE
WHEN pm.Id > 0 THEN 'Check Perspectiove for ERC'
ELSE ''
END ERC,
ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS ID
FROM
tbl_Profile as p
INNER JOIN
tbl_Profile_Mortgage as pm ON p.id = pm.FK_ProfileId
LEFT JOIN
tbl_ClientFile as cf ON p.ClientId = cf.CLIENTID
LEFT JOIN
[dbo].tbl_DDTitles as t ON cf.CLNTTITLE = t.titleid
WHERE
pm.MortgageStatus = 7
AND p.CaseTypeId = 1
AND pm.DateCompleted BETWEEN (SELECT DATEADD(YEAR, -4, GETDATE())) AND GETDATE()
This query returns 990 records.
Some clients will have multiple profiles (ProfileID) what I want to do is only show me the ProfileID with the latest DateCompleted.
So, for instance, I have a clientID of 5566 this client has 3 profiles each with there own Datecompleted i only want to see the last profiles information based on Datecompleted.
Any help would be much appreciated.
Cheers
One option for you is to partition your data by client, profile, then check which datecompleted is the greatest value (aka whichever ones have a DateRank of 1). Then just select those that have that value as 1.
select * from
(
SELECT cf.CLIENTID ,p.Id as ProfileID,t.TITLEDESC as Title,cf.ClntForenme as Name,cf.CLNTSURNME as Surname,pm.Lender,pm.Product,pm.LenderReference,pm.AmountRequested as LoanAmount,
pm.DateCompleted,
CASE
WHEN pm.DateCompleted BETWEEN (select dateadd(year, -1, getdate())) AND getdate() THEN 'Compelted Under a year ago'
WHEN pm.DateCompleted BETWEEN (select dateadd(year, -2, getdate())) AND (select dateadd(year, -1, getdate())) THEN 'Backlog WOM 1 Year'
WHEN pm.DateCompleted BETWEEN (select dateadd(year, -3, getdate())) AND (select dateadd(year, -2, getdate())) THEN 'Backlog WOM 2 Year'
WHEN pm.DateCompleted BETWEEN (select dateadd(year, -4, getdate())) AND (select dateadd(year, -3, getdate())) THEN 'Backlog WOM 3 Year'
ELSE ''
END Source,
CASE
WHEN pm.Id > 0 THEN 'Check Perspectiove for ERC'
ELSE ''
END ERC,
ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS ID,
ROW_NUMBER() OVER (Partition by CLIENTID, ProfileID order by DateCompleted desc) as DateRank --Changes here
FROM tbl_Profile as p
INNER JOIN tbl_Profile_Mortgage as pm
ON p.id = pm.FK_ProfileId
LEFT JOIN tbl_ClientFile as cf
ON p.ClientId = cf.CLIENTID
LEFT JOIN [dbo].tbl_DDTitles as t on cf.CLNTTITLE = t.titleid
WHERE pm.MortgageStatus = 7 and p.CaseTypeId = 1
AND pm.DateCompleted between (select dateadd(year, -4, getdate())) AND getdate()
) clientinfo
where clientinfo.DateRank = 1

Select Case in SQL Statement

I have the below SQL query that returns the following output for employee time ATTENDANCE.
Note when InOut = '0' it means in, and if InOut = '1' it means out.
Query:
SELECT
DATEPART(mi, LogTime) AS [InMin],
OutletName as [InOutletName]
FROM
[dbo].[AccessLog]
INNER JOIN
dbo.Outlets ON dbo.Outlets.OutletCode = dbo.AccessLog.TerminalID
WHERE
DATEPART(HOUR, LogTime) = '15'
AND InOut = '0'
AND CAST(LogDate AS date) = '2016-12-01'
Output:
InMin InOutletName
--------------------
47 GJ-SH1
The output I am looking for in to get OutMin, OutOutletName and this can be applied when InOut = '1'
Desired output:
InMin InOutletName OutMin OutOutletName
-----------------------------------------
47 GJ-SH1 10 GJ-SH1
I didn't get a chance to try it out in my SSMS, but required query should be something like this -
SELECT
Case When InOut = '0' Then DATEPART(mi, LogTime) End AS [InMin],
Case When InOut = '0' Then OutletName End as [InOutletName],
Case When InOut = '1' Then DATEPART(mi, LogTime) End AS [OutMin],
Case When InOut = '1' Then OutletName End as [OutOutletName]
FROM
[dbo].[AccessLog]
INNER JOIN
dbo.Outlets ON dbo.Outlets.OutletCode = dbo.AccessLog.TerminalID
WHERE
DATEPART(HOUR, LogTime) = '15'
AND CAST(LogDate AS date) = '2016-12-01';
A simple solution would be to join the AccessLog table twice:
SELECT
DATEPART(mi, ali.LogTime) AS [InMin],
DATEPART(mi, alo.LogTime) AS [OutMin],
OutletName as [InOutletName]
FROM dbo.Outlets
INNER JOIN [dbo].[AccessLog] ali
ON dbo.Outlets.OutletCode = ali.TerminalID
LEFT JOIN [dbo].[AccessLog] alo
ON dbo.Outlets.OutletCode = al0.TerminalID AND CAST(ali.LogDate AS date) = CAST(alo.LogDate AS date) AND alo.InOut = '1'
WHERE DATEPART(HOUR, LogTime) = '15'
AND ali.InOut = '0'
AND CAST(ali.LogDate AS date) = '2016-12-01'
Note that for the AccessLog representing the out date I've used a left join, so that you will also get Outlet names that hasn't logged out yet.
I just wrote it on the fly, not sure about the result but you can try it. It may help:
;with InQuery as (SELECT
DATEPART(mi, LogTime) AS [InMin],
OutletName as [InOutletName]
FROM [dbo].[AccessLog]
INNER JOIN dbo.Outlets
ON dbo.Outlets.OutletCode = dbo.AccessLog.TerminalID
WHERE DATEPART(HOUR, LogTime) = '15'
AND InOut = '0'
AND CAST(LogDate AS date) = '2016-12-01'),
OutQuery As (SELECT
DATEPART(mi, LogTime) AS [OutMin],
OutletName as [OutOutletName]
FROM [dbo].[AccessLog]
INNER JOIN dbo.Outlets
ON dbo.Outlets.OutletCode = dbo.AccessLog.TerminalID
WHERE DATEPART(HOUR, LogTime) = '15'
AND InOut = '1'
AND CAST(LogDate AS date) = '2016-12-01')
select iq.*, oq.* from InQuery iq, OutQuery oq

How to improve the performance of this query that spans across multiple databases?

here is my query taking nearly 20 mins. pls suggest me changes to increase performance
SELECT DISTINCT
CONVERT(varchar(10),x.notice_date,120) Date,
Y.branch_name,
count(case when x.status='broken' and x.branch_name=y.branch_name then 1 end)
Broken,
count(case when x.type='Lote' and x.branch_name=y.branch_name then 1 end)
Lost,
( SELECT COUNT(A.car_no)
FROM DB2.dbo.z_mat A
WHERE DateAdd(Day, DateDiff(Day, 0,a.notice_date), 0)
= DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
AND a.branch_name=y.branch_name
) mat,
( SELECT COUNT(B.car_no)
FROM DB2.dbo.z_cat B
WHERE DateAdd(Day, DateDiff(Day, 0,b.notice_date), 0)
= DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
AND b.branch_name=y.branch_name
) cat,
( SELECT COUNT(C.car_no)
FROM DB2.dbo.z_pat C
WHERE DateAdd(Day, DateDiff(Day, 0,c.notice_date), 0)
= DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
AND c.branch_name=y.branch_name
) pat
FROM DB1.dbo.Cars x
, DB2.dbo.Branch Y
WHERE DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
> '2011-01-01'
GROUP BY CONVERT(varchar(10),x.notice_date,120)
, DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
, y.branch_name
This might help. Give it a try.
SELECT
DISTINCT CONVERT(VARCHAR(10), car.notice_date, 120) AS NoticeDate
, brc.branch_name
, COUNT(CASE WHEN car.status = 'broken' AND car.branch_name = brc.branch_name THEN 1 END) Broken
, COUNT(CASE WHEN car.status = 'Lote' AND car.branch_name = brc.branch_name THEN 1 END) Lost
, mat.mat_count
, cat.cat_count
, pat.pat_count
FROM DB1.dbo.Cars car
, DB2.dbo.Branch brc
CROSS APPLY (
SELECT COUNT(mat.car_no) mat_count
FROM DB2.dbo.z_mat mat
WHERE DATEDIFF(d, mat.notice_date, car.notice_date) = 0
AND mat.branch_name = brc.branch_name
) mat
CROSS APPLY (
SELECT COUNT(cat.car_no) cat_count
FROM DB2.dbo.z_cat cat
WHERE DATEDIFF(d, cat.notice_date, car.notice_date) = 0
AND cat.branch_name = brc.branch_name
) cat
CROSS APPLY (
SELECT COUNT(pat.car_no) pat_count
FROM DB2.dbo.z_pat pat
WHERE DATEDIFF(d, pat.notice_date, car.notice_date) = 0
AND pat.branch_name = brc.branch_name
) pat
WHERE car.notice_date > '2011-01-01'
GROUP BY CONVERT(VARCHAR(10), car.notice_date, 120)
, brc.branch_name
SELECT CONVERT(varchar(10),x.notice_date,120) Date,Y.branch_name,
COUNT(case when x.status='broken' then 1 end) Broken,
COUNT(case when x.type='Lote' then 1 end) Lost,
SUM(mat) mat, SUM(cat) cat,SUM(pat) pat
FROM DB1.dbo.Cars x
JOIN DB2.dbo.Branch Y ON x.branch_name=y.branch_name
-- group by date and branch name for z_mat table
LEFT JOIN (select COUNT(car_no) mat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_mat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS a
ON a.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = a.notice_date
-- group by date and branch name for z_cat table
LEFT JOIN (select COUNT(car_no) cat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_cat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS b
ON b.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = b.notice_date
-- group by date and branch name for z_pat table
LEFT JOIN (select COUNT(car_no) pat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_pat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS c
ON c.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = c.notice_date
WHERE DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)>'2011-01-01'
GROUP BY CONVERT(varchar(10),x.notice_date,120),DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0),y.branch_name