PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > sql operator question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sql operator question

Réponse
 
LinkBack Outils de la discussion
Vieux 19/12/2007, 23h12   #1
ll
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sql operator question

Hi,
I'm working with a SQL statement to select from tables and only return
that which matches the semester AND value of 'N'. The statement below
does return results according to my criteria, but it also returns all
of the other results following the semester criteria that don't have a
value of 'N'.
Thanks for any you can provide,
Louis
---------------------------------------------

SELECT AMS_CourseCategory.CourseCatDesc,
AMS_CourseCategory.CourseCatID, AMS_Courses.Class,
AMS_Courses.Semester, AMS_Courses.TemplateVersion,
AMS_Courses.YearInProgram, AMS_Courses.CourseID,
AMS_Courses.Class,AMS_Courses.Semester, AMS_Courses.YearTaught,
AMS_ContentOverviewObjectiveOutcome.CourseID,
AMS_ContentOverviewObjectiveOutcome.Objective,
AMS_ContentOverviewObjectiveOutcome.Out1,
AMS_ContentOverviewObjectiveOutcome.Out9,
AMS_ContentOverviewObjectiveOutcome.Out8,
AMS_ContentOverviewObjectiveOutcome.OutP15,
AMS_ContentOverview.Complete FROM AMS_ContentOverviewObjectiveOutcome
INNER JOIN AMS_Courses ON AMS_ContentOverviewObjectiveOutcome.CourseID
= AMS_Courses.CourseID INNER JOIN AMS_CourseCategory ON
AMS_Courses.CourseCatID = AMS_CourseCategory.CourseCatID INNER JOIN
AMS_ContentOverview ON AMS_Courses.CourseID =
AMS_ContentOverview.CourseID WHERE Semester = 'FA-SP' OR Semester =
'FA' OR Semester = 'SP' AND Out8 = 'N' order by AMS_Courses.CourseCatID
  Réponse avec citation
Vieux 20/12/2007, 04h12   #2
Tom van Stiphout
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sql operator question

On Wed, 19 Dec 2007 15:12:41 -0800 (PST), ll <barn104_1999@yahoo.com>
wrote:

You probably meant to write:
WHERE Semester IN ('FA-SP', 'FA' , 'SP') AND Out8 = 'N'
which is equivalent to:
WHERE (Semester = 'FA-SP' OR Semester =
'FA' OR Semester = 'SP') AND Out8 = 'N'

It's a matter of operator precedence.

-Tom.



>Hi,
>I'm working with a SQL statement to select from tables and only return
>that which matches the semester AND value of 'N'. The statement below
>does return results according to my criteria, but it also returns all
>of the other results following the semester criteria that don't have a
>value of 'N'.
>Thanks for any you can provide,
>Louis
>---------------------------------------------
>
>SELECT AMS_CourseCategory.CourseCatDesc,
>AMS_CourseCategory.CourseCatID, AMS_Courses.Class,
>AMS_Courses.Semester, AMS_Courses.TemplateVersion,
>AMS_Courses.YearInProgram, AMS_Courses.CourseID,
>AMS_Courses.Class,AMS_Courses.Semester, AMS_Courses.YearTaught,
>AMS_ContentOverviewObjectiveOutcome.CourseID,
>AMS_ContentOverviewObjectiveOutcome.Objective,
>AMS_ContentOverviewObjectiveOutcome.Out1,
>AMS_ContentOverviewObjectiveOutcome.Out9,
>AMS_ContentOverviewObjectiveOutcome.Out8,
>AMS_ContentOverviewObjectiveOutcome.OutP15,
>AMS_ContentOverview.Complete FROM AMS_ContentOverviewObjectiveOutcome
>INNER JOIN AMS_Courses ON AMS_ContentOverviewObjectiveOutcome.CourseID
>= AMS_Courses.CourseID INNER JOIN AMS_CourseCategory ON
>AMS_Courses.CourseCatID = AMS_CourseCategory.CourseCatID INNER JOIN
>AMS_ContentOverview ON AMS_Courses.CourseID =
>AMS_ContentOverview.CourseID WHERE Semester = 'FA-SP' OR Semester =
>'FA' OR Semester = 'SP' AND Out8 = 'N' order by AMS_Courses.CourseCatID

  Réponse avec citation
Vieux 24/12/2007, 09h52   #3
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sql operator question

ll (barn104_1999@yahoo.com) writes:
> I'm working with a SQL statement to select from tables and only return
> that which matches the semester AND value of 'N'. The statement below
> does return results according to my criteria, but it also returns all
> of the other results following the semester criteria that don't have a
> value of 'N'.


> WHERE Semester = 'FA-SP' OR Semester = 'FA' OR Semester = 'SP'
> AND Out8 = 'N'
> order by AMS_Courses.CourseCatID


AND binds tighter than OR, so your WHERE clause says:

WHERE Semester = 'FA-SP' OR
Semester = 'FA' OR
(Semester = 'SP' AND Out8 = 'N')
order by AMS_Courses.CourseCatID

You need to add parentheses:

WHERE (Semester = 'FA-SP' OR Semester = 'FA' OR Semester = 'SP')
AND Out8 = 'N'
order by AMS_Courses.CourseCatID

In this case you could also use the IN operator:

WHERE Semester IN ('FA-SP', 'FA', 'SP')
AND Out8 = 'N'
order by AMS_Courses.CourseCatID



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 24/12/2007, 20h48   #4
Ed Murphy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sql operator question

ll wrote:

> SELECT AMS_CourseCategory.CourseCatDesc,
> AMS_CourseCategory.CourseCatID, AMS_Courses.Class,
> AMS_Courses.Semester, AMS_Courses.TemplateVersion,
> AMS_Courses.YearInProgram, AMS_Courses.CourseID,
> AMS_Courses.Class,AMS_Courses.Semester, AMS_Courses.YearTaught,
> AMS_ContentOverviewObjectiveOutcome.CourseID,
> AMS_ContentOverviewObjectiveOutcome.Objective,
> AMS_ContentOverviewObjectiveOutcome.Out1,
> AMS_ContentOverviewObjectiveOutcome.Out9,
> AMS_ContentOverviewObjectiveOutcome.Out8,
> AMS_ContentOverviewObjectiveOutcome.OutP15,
> AMS_ContentOverview.Complete FROM AMS_ContentOverviewObjectiveOutcome
> INNER JOIN AMS_Courses ON AMS_ContentOverviewObjectiveOutcome.CourseID
> = AMS_Courses.CourseID INNER JOIN AMS_CourseCategory ON
> AMS_Courses.CourseCatID = AMS_CourseCategory.CourseCatID INNER JOIN
> AMS_ContentOverview ON AMS_Courses.CourseID =
> AMS_ContentOverview.CourseID WHERE Semester = 'FA-SP' OR Semester =
> 'FA' OR Semester = 'SP' AND Out8 = 'N' order by AMS_Courses.CourseCatID


The AND/OR issue has already been answered by others. I just feel
obliged to point out that you've got a ton of redundant verbiage due
to non-use of prefixes. Compare this rewrite:

select cc.CourseCatDesc,
cc.CourseCatID,
c.Class,
c.Semester,
c.TemplateVersion,
c.YearInProgram,
c.CourseID,
c.Class,
c.Semester,
c.YearTaught,
cooo.CourseID,
cooo.Objective,
cooo.Out1,
cooo.Out9,
cooo.Out8,
cooo.OutP15,
co.Complete
from AMS_ContentOverviewObjectiveOutcome cooo
join AMS_Courses c on cooo.CourseID = c.CourseID
join AMS_CourseCategory cc on c.CourseCatID = cc.CourseCatID
join AMS_ContentOverview co on c.CourseID = co.CourseID
where c.Semester in ('FA-SP', 'FA', 'SP')
and cooo.Out8 = 'N'
order by c.CourseCatID

Some stylistic notes:

* c.Semester is redundant (appears twice)

* cooo.CourseID is redundant (always same value as c.CourseID)

* ORDER BY uses c.CourseCatID while SELECT uses cc.CourseCatID
(though the values are always the same)

* Out1, Out9, and Out8 look like a 1NF violation. I don't know
what the deal is with OutP15.

http://en.wikipedia.org/wiki/First_n...across_columns

What does the entire AMS_ContentOverviewObjectiveOutcome table
look like? Consider refactoring it, or at least maintaining and
using a view that presents its data in a 1NF-compliant form (then
revising the code gradually over time, until all references to the
non-1NF form are eventually eliminated).
  Réponse avec citation
Vieux 04/01/2008, 20h53   #5
ll
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sql operator question

On Dec 24 2007, 2:48 pm, Ed Murphy <emurph...@socal.rr.com> wrote:
> ll wrote:
> > SELECT AMS_CourseCategory.CourseCatDesc,
> > AMS_CourseCategory.CourseCatID, AMS_Courses.Class,
> > AMS_Courses.Semester, AMS_Courses.TemplateVersion,
> > AMS_Courses.YearInProgram, AMS_Courses.CourseID,
> > AMS_Courses.Class,AMS_Courses.Semester, AMS_Courses.YearTaught,
> > AMS_ContentOverviewObjectiveOutcome.CourseID,
> > AMS_ContentOverviewObjectiveOutcome.Objective,
> > AMS_ContentOverviewObjectiveOutcome.Out1,
> > AMS_ContentOverviewObjectiveOutcome.Out9,
> > AMS_ContentOverviewObjectiveOutcome.Out8,
> > AMS_ContentOverviewObjectiveOutcome.OutP15,
> > AMS_ContentOverview.Complete FROM AMS_ContentOverviewObjectiveOutcome
> > INNER JOIN AMS_Courses ON AMS_ContentOverviewObjectiveOutcome.CourseID
> > = AMS_Courses.CourseID INNER JOIN AMS_CourseCategory ON
> > AMS_Courses.CourseCatID = AMS_CourseCategory.CourseCatID INNER JOIN
> > AMS_ContentOverview ON AMS_Courses.CourseID =
> > AMS_ContentOverview.CourseID WHERE Semester = 'FA-SP' OR Semester =
> > 'FA' OR Semester = 'SP' AND Out8 = 'N' order by AMS_Courses.CourseCatID

>
> The AND/OR issue has already been answered by others. I just feel
> obliged to point out that you've got a ton of redundant verbiage due
> to non-use of prefixes. Compare this rewrite:
>
> select cc.CourseCatDesc,
> cc.CourseCatID,
> c.Class,
> c.Semester,
> c.TemplateVersion,
> c.YearInProgram,
> c.CourseID,
> c.Class,
> c.Semester,
> c.YearTaught,
> cooo.CourseID,
> cooo.Objective,
> cooo.Out1,
> cooo.Out9,
> cooo.Out8,
> cooo.OutP15,
> co.Complete
> from AMS_ContentOverviewObjectiveOutcome cooo
> join AMS_Courses c on cooo.CourseID = c.CourseID
> join AMS_CourseCategory cc on c.CourseCatID = cc.CourseCatID
> join AMS_ContentOverview co on c.CourseID = co.CourseID
> where c.Semester in ('FA-SP', 'FA', 'SP')
> and cooo.Out8 = 'N'
> order by c.CourseCatID
>
> Some stylistic notes:
>
> * c.Semester is redundant (appears twice)
>
> * cooo.CourseID is redundant (always same value as c.CourseID)
>
> * ORDER BY uses c.CourseCatID while SELECT uses cc.CourseCatID
> (though the values are always the same)
>
> * Out1, Out9, and Out8 look like a 1NF violation. I don't know
> what the deal is with OutP15.
>
> http://en.wikipedia.org/wiki/First_n...2:_Repeating_g...
>
> What does the entire AMS_ContentOverviewObjectiveOutcome table
> look like? Consider refactoring it, or at least maintaining and
> using a view that presents its data in a 1NF-compliant form (then
> revising the code gradually over time, until all references to the
> non-1NF form are eventually eliminated).


Thanks for all the input! Yes, the grouping was the answer. In
revising the code, I'm sure I'll consider the prefixes at some point
in the stage.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h28.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19094 seconds with 13 queries