Too many Group By dilutes query results - sql-server

I need some assistance on a SQL Query. I hope i titled it correctly as I do not want to mislead anyone.
I am attempting to generate a query which will eventually be placed into a view and be used in the Microsoft Power BI tool that will return a count for a given event in the EVENT field/column, which are:
6 – Out of Spec Above
7 – Out of Spec Below
…that occurred within a given time frame along with the total number of measurements tested.
I am successful if I only group by Date, Item Code, Plant Code, and Property using the query below.
SELECT
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) AS [Date],
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1 AS Plant_Code,
dbo.OC_VDATA.UDL5 as Property,
SUM(CASE WHEN OC_VDATA.EVENT = 6 THEN 1 WHEN OC_VDATA.EVENT = 7 THEN 1 ELSE 0 END) AS Out_of_Spec,
COUNT(*) AS Total_Measured
FROM
dbo.OC_VDATA INNER JOIN
dbo.OC_VDAT_AUX ON dbo.OC_VDATA.PARTNO = dbo.OC_VDAT_AUX.PARTNOAUX
AND dbo.OC_VDATA.DATETIME = dbo.OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(dbo.OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(dbo.OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) = '08/21/2019'
and stagingPLM.dbo.ITEM_CODES.ITEM_CODE = '20SL'
AND dbo.OC_VDATA.UDL1 = '1011'
AND dbo.OC_VDATA.UDL5 = 'WEIGHT'
--(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-13, 0))
GROUP BY
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1,
dbo.OC_VDATA.UDL5
First Query Results:
If I add another grouping level, the EVENT, my Total_Measured column gets diluted. I expected this to happen.
SELECT
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) AS [Date],
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1 AS Plant_Code,
dbo.OC_VDATA.UDL5 as Property,
dbo.OC_VDATA.EVENT,
SUM(CASE WHEN OC_VDATA.EVENT = 6 THEN 1 WHEN OC_VDATA.EVENT = 7 THEN 1 ELSE 0 END) AS Out_of_Spec,
COUNT(*) AS Total_Measured
FROM
dbo.OC_VDATA INNER JOIN
dbo.OC_VDAT_AUX ON dbo.OC_VDATA.PARTNO = dbo.OC_VDAT_AUX.PARTNOAUX
AND dbo.OC_VDATA.DATETIME = dbo.OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(dbo.OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(dbo.OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101) = '08/21/2019'
and stagingPLM.dbo.ITEM_CODES.ITEM_CODE = '20SL'
AND dbo.OC_VDATA.UDL1 = '1011'
AND dbo.OC_VDATA.UDL5 = 'WEIGHT'
--(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-13, 0))
GROUP BY
CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
dbo.OC_VDATA.UDL1,
dbo.OC_VDATA.UDL5,
dbo.OC_VDATA.EVENT
Second Query Results:
With all that said, what I really want is the results from the second query and merge in the Total_Measured column from the first query.
Results I want:
I thought using a CTE was the answer but that takes a while and eventually generates an error.
WITH CTE1 AS
(
SELECT
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101) AS Date,
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1 AS Plant_Code,
OC_VDATA.UDL5 AS Property,
--SUM(CASE WHEN OC_VDATA.EVENT = 6 THEN 1 WHEN OC_VDATA.EVENT = 7 THEN 1 ELSE 0 END) AS Out_of_Spec,
COUNT(OC_VDATA.EVENT) AS Total_Measured
FROM
OC_VDATA INNER JOIN
OC_VDAT_AUX ON OC_VDATA.PARTNO = OC_VDAT_AUX.PARTNOAUX
AND OC_VDATA.DATETIME = OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(month, - 13, GETDATE()))
GROUP BY
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1,
OC_VDATA.UDL5
),
CTE2 AS
(
SELECT
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101) AS Date,
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1 AS Plant_Code,
OC_VDATA.UDL5 AS Property,
OC_VDATA.EVENT,
COUNT(*) AS Out_of_Spec
FROM
OC_VDATA INNER JOIN
OC_VDAT_AUX ON OC_VDATA.PARTNO = OC_VDAT_AUX.PARTNOAUX
AND OC_VDATA.DATETIME = OC_VDAT_AUX.DATETIMEAUX INNER JOIN
stagingPLM.dbo.ITEM_CODES ON LEFT(OC_VDATA.PARTNO, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
AND LEFT(OC_VDAT_AUX.PARTNOAUX, 12) = stagingPLM.dbo.ITEM_CODES.SPEC_NO
WHERE
(CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102) >= DATEADD(month, - 13, GETDATE()))
GROUP BY
CONVERT(varchar, CONVERT(datetime, OC_VDAT_AUX.UDL40, 102), 101),
stagingPLM.dbo.ITEM_CODES.ITEM_CODE,
OC_VDATA.UDL1,
OC_VDATA.UDL5,
OC_VDATA.EVENT
)
SELECT
cte1.Date,
cte1.ITEM_CODE,
cte1.Plant_Code,
cte1.PROPERTY,
EVENT,
Out_of_Spec,
Total_Measured
FROM
cte1 INNER JOIN cte2 on cte1.Date = cte2.date
and cte1.ITEM_CODE = cte2.ITEM_CODE
and cte1.Plant_Code = CTE2.Plant_Code
and cte1.Property = cte2.Property
How to achieve what I want? Does anyone have any tips/tricks? Am I even on the right track? Frustration is starting to set in.

DECLARE #TargetDate varchar(10) = '08/21/2019'
;WITH PlainList AS
(
SELECT
[Date] = CONVERT(varchar, CONVERT(datetime, dbo.OC_VDAT_AUX.UDL40, 102), 101)
, Item_Code = stagingPLM.dbo.ITEM_CODES.ITEM_CODE
, Plant_Code = dbo.OC_VDATA.UDL1
, Property = dbo.OC_VDATA.UDL5
, [Event] = dbo.OC_VDATA.[EVENT]
, Out_of_Spec = CASE OC_VDATA.[EVENT]
WHEN 6 THEN 1
WHEN 7 THEN 1
ELSE 0
END
FROM dbo.OC_VDATA
INNER JOIN dbo.OC_VDAT_AUX ON dbo.OC_VDAT_AUX.PARTNOAUX = dbo.OC_VDATA.PARTNO
AND dbo.OC_VDAT_AUX.DATETIMEAUX = dbo.OC_VDATA.[DATETIME]
INNER JOIN stagingPLM.dbo.ITEM_CODES ON stagingPLM.dbo.ITEM_CODES.SPEC_NO = LEFT(dbo.OC_VDATA.PARTNO , 12)
AND stagingPLM.dbo.ITEM_CODES.SPEC_NO = LEFT(dbo.OC_VDAT_AUX.PARTNOAUX , 12)
WHERE 1=1
AND stagingPLM.dbo.ITEM_CODES.ITEM_CODE = '20SL'
AND dbo.OC_VDATA.UDL1 = '1011'
AND dbo.OC_VDATA.UDL5 = 'WEIGHT'
)
SELECT
[Date]
, Item_Code
, Plant_Code
, Property
, [Event]
, Out_of_Spec = SUM(Out_of_Spec)
, Total_Measured = SUM(1)
FROM PlainList P
GROUP BY
[Date]
, Item_Code
, Plant_Code
, Property
, [Event]
HAVING [Date] = #TargetDate

Related

How to display monthly balance for month with no balance SQL Server

I have the following block of code which I am using to query a SQL Server database that summarizes all balances for each of the previous 13 months. It works pretty good, but there are a few months when there were no balances to report. These months are not displaying which I do need. At this point, I am at a loss for what to try next.
DECLARE #StartDate DATE, #EndDate DATE;
SELECT
#StartDate = CONVERT(VARCHAR(11), DATEADD(month, -13, GETDATE())),
#EndDate = CONVERT(VARCHAR(11), DATEADD(month, 0, GETDATE()));
;WITH d(d) AS
(
SELECT
DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, #StartDate), 0))
FROM
(SELECT TOP (DATEDIFF(MONTH, #StartDate, #EndDate) + 1)
n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
FROM
sys.all_objects ORDER BY [object_id]) AS n
)
SELECT
FORMAT(d.d, 'MMM yy') AS Purchase_Date,
CAST(ROUND(SUM(lm.BALANCE), 0) AS FLOAT) AS Balance
FROM
d
LEFT OUTER JOIN
dbo.purchases AS lm ON lm.purchase_date >= d.d
AND lm.purchase_date < DATEADD(MONTH, 1, d.d)
WHERE
lm.Buyer_code = 'FirstTime'
AND lm.PROGRAM_ID = 'NewBuyers'
GROUP BY
d.d
ORDER BY
d.d
You are overriding the LEFT JOIN by the where clause, which requires that every row have certain values from the left joined table. In effect it is equivalent to an inner join. You need to allow rows from d to survive into the result, which you can do by using the wanted conditions directly in the LEFT JOIN:
SELECT
FORMAT( d.d, 'MMM yy' ) AS Purchase_Date
, CAST( ROUND( SUM( lm.BALANCE ), 0 ) AS float ) AS Balance
FROM d
LEFT OUTER JOIN DBO.purchases AS lm ON lm.purchase_date >= d.d
AND lm.purchase_date < DATEADD( MONTH, 1, d.d )
AND lm.Buyer_code = 'FirstTime'
AND lm.PROGRAM_ID = 'NewBuyers
GROUP BY
d.d
ORDER BY
d.d

Getting the result with date from yesterday

I am trying to tally all Invoices without Sales Order and show it as an SQL query. The date should be the Invoices entered yesterday. The challenge is it seems that the result is static and won't change
SELECT TOP (100) PERCENT CONVERT(varchar, DATEADD(dd, - 1, GETDATE()), 103) AS Date,
'Invoices w/o SO' AS Type,
COUNT(dbo.Invoice.InvoiceID) AS Orders,
COUNT(dbo.Invoice.InvoiceID) AS Chairs,
ISNULL(ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]
FROM dbo.Invoice INNER JOIN dbo.Customers
ON dbo.Invoice.CustomerID = dbo.Customers.CustomerID
INNER JOIN dbo.InvoiceDetails
ON dbo.Invoice.InvoiceID = dbo.InvoiceDetails.InvoiceID
WHERE (dbo.InvoiceDetails.ItemNo = 'TRIAL CHAIR')
OR
(dbo.InvoiceDetails.ItemNo = 'STORAGE')
OR
(dbo.InvoiceDetails.ItemNo = 'RSTF01')
OR
(dbo.InvoiceDetails.ItemNo = 'FRDMC01')
AND
(dbo.Invoice.CredInvoiceNo IS NULL)
AND
(dbo.Invoice.EntryDate >= CONVERT(char(8), DATEADD(dd, - 1, GETDATE()), 112))
AND
(dbo.Customers.CustomerID <> 187)
Screenshot
Thanks everyone.
I changed the query and it worked!
SELECT
CONVERT(varchar, DATEADD(dd, -1, GETDATE()), 103) AS Date,
'Invoices w/o SO' AS Type,
COUNT(dbo.Invoice.InvoiceID) AS Orders,
COUNT(dbo.Invoice.InvoiceID) AS Chairs,
ISNULL(ROUND(SUM(dbo.InvoiceDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]
FROM dbo.Invoice
INNER JOIN dbo.Customers
ON dbo.Invoice.CustomerID = dbo.Customers.CustomerID
INNER JOIN dbo.InvoiceDetails
ON dbo.Invoice.InvoiceID = dbo.InvoiceDetails.InvoiceID
WHERE (dbo.InvoiceDetails.ItemNo IN ('TRIAL CHAIR', 'STORAGE', 'RSTF01', 'FRDMC01'))
AND (dbo.Invoice.CredInvoiceNo IS NULL)
AND (dbo.Customers.CustomerID <> 187)
AND (DATEDIFF(DAY, dbo.Invoice.EntryDate, GETDATE()) = 1)

case when in sqlserver

i try to use case in for my condition to show out which is late,but after i use case when my result show out too many result.
select
grp.checktime,
--min(CONVERT(smalldatetime,l.checktime)) as clockin,
--max(CONVERT(smalldatetime,l.checktime)) as clockout,
ClockIn = case when min(cast(l.checktime as time)) <= cast(sc.StartTime as time)
then convert(varchar(100), cast(l.checktime as time), 100)
else 'Late ClockIn ' + convert(varchar(100), cast(l.checktime as time), 100)
end,
Clockout = case when max(cast(l.checktime as time)) >= cast(sc.EndTime as time)
then convert(varchar(100), cast(l.checktime as time), 100)
else 'Early ClockOut ' + convert(varchar(100), cast(l.checktime as time), 100)
end,
l.userid,
u.showname,
u.BADGENUMBER
from checkinout l
inner join userinfo u on l.userid = u.userid
inner join UserUsedsclasses uuc on u.userid = uuc.userid
inner join SchClass sc on uuc.SchId = sc.schClassid
inner join (
select distinct CONVERT(Date,checktime) as checktime
from checkinout
group by CONVERT(Date,checktime)
) as grp on grp.checktime = CONVERT(Date, l.checktime)
where uuc.SchId = 1 and u.badgenumber = 107
and u.badgenumber not in (79, 103, 78)
and l.checktime >= dateadd(month, datediff(month, 0, GETDATE() ) , 0)
and l.checktime < dateadd(month, datediff(month, 0, GETDATE ())+1, 0)
group by grp.checktime,l.userid,u.showname,u.BADGENUMBER,sc.StartTime,l.checktime,sc.EndTime
my result show me
Change your group by to group by the date of l.checktime.
select
grp.checktime,
--min(convert(smalldatetime,l.checktime)) as clockin,
--max(convert(smalldatetime,l.checktime)) as clockout,
ClockIn = case when min(cast(l.checktime as time)) <= cast(sc.StartTime as time)
then convert(varchar(100), cast(l.checktime as time), 100)
else 'Late ClockIn ' + convert(varchar(100), cast(l.checktime as time), 100)
end,
Clockout = case when max(cast(l.checktime as time)) >= cast(sc.EndTime as time)
then convert(varchar(100), cast(l.checktime as time), 100)
else 'Early ClockOut ' + convert(varchar(100), cast(l.checktime as time), 100)
end,
l.userid,
u.showname,
u.BADGENUMBER
from checkinout l
inner join userinfo u on l.userid = u.userid
inner join UserUsedsclasses uuc on u.userid = uuc.userid
inner join SchClass sc on uuc.SchId = sc.schClassid
inner join (
select distinct convert(Date,checktime) as checktime
from checkinout
group by convert(Date,checktime)
) as grp on grp.checktime = convert(Date, l.checktime)
where uuc.SchId = 1 and u.badgenumber = 107
and u.badgenumber not in (79, 103, 78)
and l.checktime >= dateadd(month, datediff(month, 0, getdate() ) , 0)
and l.checktime < dateadd(month, datediff(month, 0, getdate ())+1, 0)
group by
grp.checktime
, l.userid
, u.showname
, u.BADGENUMBER
, sc.StartTime
, convert(date, l.checktime)
, sc.EndTime

SQL Server: How to return value for each past 6 months

I have a query that returns a score on whether or not 3 of the columns = 1 and then if the ProviderID exists in the 2nd table. I need to be able to return a score for each month for 6 months using column Time_Stamp, not including current month. The below returns the score for last month. How can I include the remaining 5 months and ROW_NUMBER() them?
DECLARE #ProviderID int = '1717';
WITH cte as
(
SELECT TOP 1
a.ProviderID, Time_Stamp,
SUM(CASE WHEN [AdditionalReports] = '1' THEN 5 ELSE 0 END) as AdditionalReports,
SUM(CASE WHEN [UniqueReportRequests] = '1' THEN 15 ELSE 0 END) as UniqueReportsRequests,
SUM(CASE WHEN [SurveyCompleted] = '1' THEN 30 ELSE 0 END) as SurveyCompleted,
MAX(CASE WHEN b.ProviderID IS NULL THEN 0 ELSE 50 END) as SubscriptionExists
FROM
ProviderValueCard a
LEFT JOIN
SubscriptionsTV b ON a.ProviderID = b.ProviderID
WHERE
a.ProviderID = #ProviderID AND GroupID = 2
AND Time_Stamp BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
GROUP BY
Time_Stamp, a.ProviderID, event
ORDER BY
event DESC, Time_Stamp DESC
)
SELECT
ProviderID, Time_Stamp,
(AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
FROM
cte
Here is how to grab the first/last day of previous months:
2 months ago:
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0) as FirstD2monthsago,
DATEADD(DAY, -DAY(GETDATE()), DATEADD(MONTH, -1, GETDATE())) AS last_day_2_months_ago
3 months ago etc:
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 3, 0) as FirstD3monthsago,
DATEADD(DAY, -DAY(GETDATE()), DATEADD(MONTH, -2, GETDATE())) AS last_day_3_months_ago
Desired output
ProviderID Time_Stamp TotalScore Row_Number
----------- ----------------------- -----------
1717 2014-08-29 12:11:17.610 70 1
1717 2014-07-29 12:11:17.610 95 2
1717 2014-06-29 12:11:17.610 100 3
1717 2014-05-29 12:11:17.610 70 4
1717 2014-04-29 12:11:17.610 70 5
1717 6
DECLARE #ProviderID INT, #Now DATETIME, #Months INT
SELECT #ProviderID = 1717, #Now = GETDATE(), #Months = 5
WITH
date_range_cte AS (
SELECT 1 AS RowNum, DATEADD(mm,-1,#Now) AS StartDate, DATEADD(mm,0,#Now) AS EndDate
UNION ALL
SELECT d.RowNum + 1 AS RowNum, DATEADD(mm,(-d.RowNum - 1),#Now) AS StartDate, DATEADD(mm,-d.RowNum,#Now) AS EndDate
FROM date_range_cte d
WHERE d.RowNum + 1 <= #Months
),
main_cte AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY a.ProviderID, d.RowNum, d.StartDate ORDER BY Time_Stamp DESC) AS ordinal_position,
a.ProviderID,
d.RowNum,
d.StartDate,
[AdditionalReports] * 5 AS AdditionalReports,
[UniqueReportRequests] * 15 AS UniqueReportsRequests,
[SurveyCompleted] * 30 as SurveyCompleted,
CASE WHEN b.ProviderID IS NULL THEN 0 ELSE 50 END as SubscriptionExists
FROM ProviderValueCard a
INNER JOIN date_range_cte d ON d.StartDate < Time_Stamp AND Time_Stamp <= d.EndDate
LEFT OUTER JOIN SubscriptionsTV b ON a.ProviderID = b.ProviderID
WHERE a.ProviderID = #ProviderID AND GroupID = 2
)
SELECT ProviderID, RowNum, StartDate, (AdditionalReports + UniqueReportsRequests + SurveyCompleted + SubscriptionExists) AS TotalScore
FROM main_cte
WHERE ordinal_position = 1
ORDER BY RowNum
Here's a couple of ways (psuedocode):
1 - Make a cte just like your existing one for each month you want to get. The only thing you need to change in each one is this line:
AND Time_Stamp BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(DAY, -(DAY(GETDATE())), GETDATE())
For 2 months ago, you would change it to this:
AND Time_Stamp BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 2, 0) AND DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0)
and so on for the other months back.
2 - Referring to the same line in your cte above, change it to a -6 to get data for the past 6 months. Then include a MONTH(TimeStamp) column in your select list and group by it to get one row per month for the past 6 months.

Converting date

I am looking to convert the following date
select (CONVERT(varchar(11), DATEADD(year, + 1,CONVERT(varchar(11), getdate(),111)))),datename(month,getdate()) for it returns 'jun 26 2015' ,'JUne'
I needed it to return as the following '2015/6' , June
This gives me what I want when using just get date
select CONVERT(varchar(7), getdate(), 111) = 2014/06
but I need it +1 year on my timewait field.
My actual code is as follows:
SELECT c.account_owner_by_SalesDiv, CONVERT(varchar(7), a.timeWait, 111)as rollingdate, DATENAME(MOnth, a.timeWait)
FROM SalesForce.dbo.SalesForceContact AS b INNER JOIN
Dossier_Replication.dbo.vw_SFAD_Contact_data AS c ON b.ContactID = c.CONTACTID__C RIGHT OUTER JOIN
satVRS.dbo.rptNECACallHistory AS a ON b.UserID = a.UserID_Caller
WHERE (b.Platform = 'HandsonVRS') AND (a.timeWait BETWEEN CONVERT(varchar(10), DATEADD(year, - 1, CONVERT(varchar, DATEADD(month, DATEDIFF(month, 0,
GETDATE()-1), 0), 120)), 120) AND CONVERT(varchar, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0), 101)) AND (a.isReport = '1') AND (a.NECA_isReport = '1') AND
(a.ConvoTime > '0') AND (c.account_owner_by_SalesDiv IN ('Enterprise Account', 'National Account', 'Major Account', 'House Account', 'Inside Sales Account'))
GROUP BY c.account_owner_by_SalesDiv, CONVERT(varchar(7), a.timeWait, 111), DATENAME(MOnth, a.timeWait)
order by rollingdate
Thanks in advance.
This gives 2015/06, June:
SELECT
CONVERT(VARCHAR(7), DATEADD(YEAR,1,GETDATE()), 111) + ', ' + DATENAME(MONTH,GETDATE())
If you don't want the leading zero you could use the SUBSTRING function to trim it.

Resources