SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 28th, 2005, 11:21 AM
superlu superlu is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 16 superlu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 sec
Reputation Power: 0
Question T-SQL help (combining queries)

I am trying to pull two different information for each month of the year. However, I am having trouble with my SQL statement...I was wondering if someone can point me to the right direction.

SQL statement:
SELECT DatePart(mm, Reports.IncidentDate) as MM,
Count(Distinct Reports.ReportId) AS intNumIncidents,
CASE
WHEN RD.reportField_ID <> 563 THEN 0
WHEN RD.reportField_ID = 563 THEN avg(cast(RD.DataText as money))
ELSE 0
END AS AvgValue
FROM Reports left outer join reportData as RD on reports.reportID = RD.report_ID
WHERE (StoreNum = 33) AND
(Reports.intEnabled = 1) AND
((Reports.ReportType_Id = 1 OR Reports.ReportType_Id = 36) OR RD.reportField_ID = 563) AND
(DatePart(yyyy, Reports.IncidentDate) = 2004) AND
(RD.DataText <> '' AND RD.DataText is not null) AND
(Reports.intComplete = 1)
group by DatePart(mm, Reports.IncidentDate), RD.reportField_ID

- The intNumIncidents counts the number of incidents
-The AVGVALUE average it's monetary value in which the value is store in RD.reportField_ID = 563

the error I am getting is : Cannot convert a char value to money. The char value has incorrect syntax.
Basically, with the OR statement on ...OR RD.reportField_ID = 563, it will return all row containing a reportType_ID = 1 or 36 with different a reportField_ID <> 36. Thus, it wont be able to convert those variables. I can't figure out how to exclude those case when calculating my average.

If I replace the OR to an AND by the RD.reportField_ID = 563
( ((Reports.ReportType_Id = 1 OR Reports.ReportType_Id = 36) AND RD.reportField_ID = 563) AND )

SQL will return this
-------------------
MM | intNumIncidents | AvgValue
7 | 4 | 540.8550
8 | 4 | 148.2425
9 | 2 | 104.4800
10 | 4 | 136.3825
11 | 1 | 254.0000
12 | 2 | 198.3450

As you can notice, the data for Month 1-6 is missing.
If I run the intNumIncidents and AvgValue individually, it works fine...I want to combine these two queries into one to speed up my query. Please assist...I greatly appreciate anyone advise.

Thanks.

Reply With Quote
  #2  
Old March 28th, 2005, 12:37 PM
Memnoch's Avatar
Memnoch Memnoch is online now
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,879 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 18 h
Reputation Power: 500
Quote:
Cannot convert a char value to money.

Code:
CAST(CharValue) As Money

Reply With Quote
  #3  
Old March 28th, 2005, 02:47 PM
superlu superlu is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 16 superlu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 sec
Reputation Power: 0
didnt work

wrong syntax

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > T-SQL help (combining queries)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT