Solved

SQL Query Help - Sum of Varchar

Posted on 2011-03-18
9
952 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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