Solved

SQL Query Help - Sum of Varchar

Posted on 2011-03-18
9
950 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Script to backup a Database Dayli on SQL Server Express 3 23
PolyServe for SQL server 13 41
Reformat SQL - so SSRS can read the columns 25 30
How can I find this data? 3 23
Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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