Solved

SQL Query Help - Sum of Varchar

Posted on 2011-03-18
9
947 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:davidi1
  • 4
  • 4
9 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 35164163
check the spell I thing it is wrong for Abandons
try this

, Sum(Case when TalismaStatus = 'Abandons' then 1 else 0 end) Abandons
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 35164164
Hi,
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.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35164171
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
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Author Comment

by:davidi1
ID: 35164186
@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.

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35164219
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
0
 
LVL 2

Author Comment

by:davidi1
ID: 35164274
Result file uploaded.

Even in the output,

All the other values are correct except "Abandoned" value
Result.xlsx
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35164288
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
0
 
LVL 2

Author Comment

by:davidi1
ID: 35164312
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
0
 
LVL 2

Author Comment

by:davidi1
ID: 35164389
Pratima - I'll give you points. My bad.., TeamName wasnt coming for Abandoned cases.., hence it was showing "0". Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question