Unix Technical Forum

Help with query plez

This is a discussion on Help with query plez within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello all, I'm trying to write a stored procedure that will prompt the user for a date range and ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:49 AM
ndn_24_7
 
Posts: n/a
Default Help with query plez

Hello all,

I'm trying to write a stored procedure that will prompt the user for a
date range and produce a report based on that date range and 4 incident
types. I only need count how may times these incidents happen within
the data range. The query looks like this

SELECT incident, @Enter_Beginning_Date AS [Beginning Date],
@Enter_Ending_Date AS [Ending Date], COUNT(*) AS Occurances
FROM dbo.Incident
WHERE (DateOccured BETWEEN @Enter_Beginning_Date AND
@Enter_Ending_Date) AND (incident = 'Customer Accident')
GROUP BY incident

This works fine, but I need to get incident = customer accident,
Customer Illness, Employee Accident and Employee Illness in my incident
table. When ever I try to add Incident = Customer Illness to this
query, I get no results. Any assistance will be greatly appreciated.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:49 AM
QueryBuilder
 
Posts: n/a
Default Re: Help with query plez

First of all, how are you prompting the user for values thru Stored
procedures????????

Your question is pretty hard to answer as you havent provided any info
about your table or Data...!

Do you have records that qualifies Incident = 'Customer Illness'...!

It would help people to help you if you post some DDL/DML codes to
understand where the problem is....

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 05:49 AM
ndn_24_7
 
Posts: n/a
Default Re: Help with query plez

sorry about the vagness,

The table I'm trying to query looks something like this

ID(int) Incident(nvarchar) DateOccured (datetime)
1200 Customer Illness 1/1/2003
1201 Customer Illness 1/2/2003
1202 Customer Accident 1/2/2003
1203 Customer Accident 1/3/2003
1204 Employee Accident 1/5/2003
1205 Employee Illness 1/6/2003

The stored procedure prompts the user for a beginning
(@Enter_Beginning_Date) and ending date(@Enter_Ending_Date) which uses
the DateOccured column for the specified date range. I want the query
to count the number of occurences that happen within the specified date
range. I want to create a report that looks loke this

Incident Number of Occurences
Customer Illness 2
Customer Accident 2
Employee Accident 1
Employee Illness 1

When ever I query for one incident (Customer Accident), the query will
work fine, but when I insert another statement [(incident = Customer
Illness) AND (incident = Customer Accident)] I get no results, when I
know there are at least 3 incidents of Customer Illness. Does this help?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 05:49 AM
SQLANG
 
Posts: n/a
Default Re: Help with query plez

Based on the data you gave..

declare @Enter_Ending_Date datetime
declare @Enter_Beginning_Date datetime

set @Enter_Ending_Date='1/2/2003'
set @Enter_Beginning_Date='1/2/2003'

SELECT incident,
@Enter_Beginning_Date AS [Beginning Date],
@Enter_Ending_Date AS [Ending Date],
COUNT(*) AS Occurances
FROM Incident
WHERE (DateOccured BETWEEN @Enter_Beginning_Date AND
@Enter_Ending_Date) AND
(incident = 'Customer Illness') or (incident = 'Customer Accident')
--(incident = 'Customer Accident')
GROUP BY incident

/*******************************
RESULTSET

Customer Accident 2003-01-02 00:00:00.000 2003-01-02 00:00:00.000 2
Customer Illness 2003-01-02 00:00:00.000 2003-01-02 00:00:00.000 1

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 05:49 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Help with query plez

On 26 Jan 2005 12:09:50 -0800, ndn_24_7 wrote:

(snip)
>The stored procedure prompts the user for a beginning
>(@Enter_Beginning_Date) and ending date(@Enter_Ending_Date) which uses
>the DateOccured column for the specified date range.


Hi ndn_27_7,

I presume you meant to write that the front-end app prompts the user for
these dates and passes them to SQL Server when calling the stored
procedure, as there is no way that a stored procedure will ever prompt for
input.


>When ever I query for one incident (Customer Accident), the query will
>work fine, but when I insert another statement [(incident = Customer
>Illness) AND (incident = Customer Accident)] I get no results, when I
>know there are at least 3 incidents of Customer Illness. Does this help?


The WHERE clause is checked against each row individually. No single row
will ever have both 'Customer Illness' and 'Customer Accident' in the same
column (incident).

SELECT incident, @Enter_Beginning_Date AS [Beginning Date],
@Enter_Ending_Date AS [Ending Date], COUNT(*) AS Occurances
FROM dbo.Incident
WHERE DateOccured BETWEEN @Enter_Beginning_Date
AND @Enter_Ending_Date
AND ( incident = 'Customer Accident'
OR incident = 'Customer Illness')
GROUP BY incident

or

SELECT incident, @Enter_Beginning_Date AS [Beginning Date],
@Enter_Ending_Date AS [Ending Date], COUNT(*) AS Occurances
FROM dbo.Incident
WHERE DateOccured BETWEEN @Enter_Beginning_Date
AND @Enter_Ending_Date
AND incident IN ('Customer Accident', 'Customer Illness')
GROUP BY incident

(Note that IN is just a short form for writing a bunch of OR's)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 05:49 AM
ndn_24_7
 
Posts: n/a
Default Re: Help with query plez

It worked very good, THank you for all your assistance

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:17 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com