davidi1
asked on
SQL Query Help - Sum of Varchar
Team,
Below is my query:
Declare @Dates Varchar(255)
Set @Dates = ('WK10')
Select A.AcceptTeam, A.CreateDate, A.CharDate, a.Escalations, A.Abandons, a.Volume, B.FiscalWk1 from
(select AcceptTeam, Convert(Datetime, CreateDate) as CreateDate
, Convert(Varchar(6), convert (datetime, CreateDate), 107) as CharDate
, count(*) Volume
, sum(case when CSIStatus = 'Escalation' then 1 else 0 end) escalations
, Sum(Case when TalismaStatus = 'Abandoned' then 1 else 0 end) Abandons
, TimeZone from Chat_Analyzed where ServiceLine = 'Celerra Support' and AcceptTeam <> 'Data Unavailable'
group by AcceptTeam, CreateDate, TimeZone) a
Inner Join
(Select FiscalDate, FiscalWK1 from reports.dbo.FiscalCalender ) b
on a.CreateDate = b.FiscalDate
Where FiscalWK1 in (@Dates) and TimeZone = 'Amer'
Columns:
CSIStatus = Varchar(255)
TalismaStatus = Varchar(255)
When running the query, CSIStatus (Escalations) value is coming correctly however TalismaStatus(Abandons) is not showing any value. Just shows "0". However i do have some abandons for the running week.
Pls help
Below is my query:
Declare @Dates Varchar(255)
Set @Dates = ('WK10')
Select A.AcceptTeam, A.CreateDate, A.CharDate, a.Escalations, A.Abandons, a.Volume, B.FiscalWk1 from
(select AcceptTeam, Convert(Datetime, CreateDate) as CreateDate
, Convert(Varchar(6), convert (datetime, CreateDate), 107) as CharDate
, count(*) Volume
, sum(case when CSIStatus = 'Escalation' then 1 else 0 end) escalations
, Sum(Case when TalismaStatus = 'Abandoned' then 1 else 0 end) Abandons
, TimeZone from Chat_Analyzed where ServiceLine = 'Celerra Support' and AcceptTeam <> 'Data Unavailable'
group by AcceptTeam, CreateDate, TimeZone) a
Inner Join
(Select FiscalDate, FiscalWK1 from reports.dbo.FiscalCalender
on a.CreateDate = b.FiscalDate
Where FiscalWK1 in (@Dates) and TimeZone = 'Amer'
Columns:
CSIStatus = Varchar(255)
TalismaStatus = Varchar(255)
When running the query, CSIStatus (Escalations) value is coming correctly however TalismaStatus(Abandons) is not showing any value. Just shows "0". However i do have some abandons for the running week.
Pls help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
check the spell I thing it is wrong for Abandons
In query you have given 'Abandoned' and in Questin you asked for 'Abandons'
see which is correct and change accordingly
, Sum(Case when TalismaStatus = 'Abandons' then 1 else 0 end) Abandons
In query you have given 'Abandoned' and in Questin you asked for 'Abandons'
see which is correct and change accordingly
, Sum(Case when TalismaStatus = 'Abandons' then 1 else 0 end) Abandons
ASKER
@Pratima_mcs - Spelling is correct. "Abandoned" is how the data is in the table.
@Rajeev -
CreateDate column doesnt have time value in it.
This is how the table looks
CreateDate TalismaStatus CSIStatus
2/27/2011 Abandoned Cancelled
2/27/2011 Complete Cancelled
2/27/2011 Complete Cancelled
2/27/2011 Complete Closed
2/27/2011 Complete Closed
2/27/2011 Abandoned Cancelled
All the columns are Varchar in the table.
@Rajeev -
CreateDate column doesnt have time value in it.
This is how the table looks
CreateDate TalismaStatus CSIStatus
2/27/2011 Abandoned Cancelled
2/27/2011 Complete Cancelled
2/27/2011 Complete Cancelled
2/27/2011 Complete Closed
2/27/2011 Complete Closed
2/27/2011 Abandoned Cancelled
All the columns are Varchar in the table.
what output you are getting here
Select A.AcceptTeam, A.CreateDate, A.CharDate, a.Escalations, A.Abandons, a.Volume, B.FiscalWk1 from
(select AcceptTeam, Convert(Datetime, CreateDate) as CreateDate
, Convert(Varchar(6), convert (datetime, CreateDate), 107) as CharDate
, count(*) Volume
, sum(case when CSIStatus = 'Escalation' then 1 else 0 end) escalations
, Sum(Case when TalismaStatus = 'Abandoned' then 1 else 0 end) Abandons
, TimeZone from Chat_Analyzed where ServiceLine = 'Celerra Support' and AcceptTeam <> 'Data Unavailable'
group by AcceptTeam, CreateDate, TimeZone) a
Inner Join
(Select FiscalDate, FiscalWK1 from reports.dbo.FiscalCalender ) b
on a.CreateDate = b.FiscalDate
Select A.AcceptTeam, A.CreateDate, A.CharDate, a.Escalations, A.Abandons, a.Volume, B.FiscalWk1 from
(select AcceptTeam, Convert(Datetime, CreateDate) as CreateDate
, Convert(Varchar(6), convert (datetime, CreateDate), 107) as CharDate
, count(*) Volume
, sum(case when CSIStatus = 'Escalation' then 1 else 0 end) escalations
, Sum(Case when TalismaStatus = 'Abandoned' then 1 else 0 end) Abandons
, TimeZone from Chat_Analyzed where ServiceLine = 'Celerra Support' and AcceptTeam <> 'Data Unavailable'
group by AcceptTeam, CreateDate, TimeZone) a
Inner Join
(Select FiscalDate, FiscalWK1 from reports.dbo.FiscalCalender
on a.CreateDate = b.FiscalDate
ASKER
Result file uploaded.
Even in the output,
All the other values are correct except "Abandoned" value
Result.xlsx
Even in the output,
All the other values are correct except "Abandoned" value
Result.xlsx
try with like
, Sum(Case when TalismaStatus like 'Abandoned' then 1 else 0 end) Abandons
or with ltrim and rtrim .. might it will have spaces
, Sum(Case when ltrim(rtrim(TalismaStatus) ) = 'Abandoned' then 1 else 0 end) Abandons
, Sum(Case when TalismaStatus like 'Abandoned' then 1 else 0 end) Abandons
or with ltrim and rtrim .. might it will have spaces
, Sum(Case when ltrim(rtrim(TalismaStatus)
ASKER
When I run the first query, "Escalations" value also getting changed.
& the second query - no difference with the original value(ie., abandoned column still a zero)
Result---1st-Query.JPG
& the second query - no difference with the original value(ie., abandoned column still a zero)
Result---1st-Query.JPG
ASKER
Pratima - I'll give you points. My bad.., TeamName wasnt coming for Abandoned cases.., hence it was showing "0". Thanks
The query looks fine to me.
Though if you are not getting the required result, then please check:
1. The CreateDate column in Chat_Analyzed is having "time" value also?
2. The FiscalDate column in FiscalCalender is also having "time" or not?
It looks that for Escalations the data entered is without time. But for Abandons it is having time also. If such a scenario exists, then you need to use
Convert(Datetime, convert(varchar, CreateDate,111) ,111) as CreateDate
instead of
Convert(Datetime, CreateDate) as CreateDate
.
If not resolved, then please show us some sample data with columns properties.