Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query Help - Sum of Varchar

Posted on 2011-03-18
9
Medium Priority
?
963 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 2000 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
Technology Partners: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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