Showing posts with label criteria. Show all posts
Showing posts with label criteria. Show all posts

Friday, March 9, 2012

multiple criteria in WHERE clause (was "T-SQL")

How can you handle multiple criteria query in T-SQL ? i wrote selection query and in my where clause i have about 7 different criteria and for some reason when i run the query i do not get any error but i do not get any data return.

So is there any other way to handle multiple criteria in T-SQL ?Uhmm...how do you ever expect Field1 to equal 1 AND equal 2 at the same time? Curious mathematical alternate universe you live in, but since your server seems to be bound to this particular reality, I suggest you look up the "OR" clause in Books Online.|||Maybe you can put this statement inside of a loop and run it until @.@.rowcount > 0. If it runs long enough, math rules might change to include an uncertaintly principal where a finite object can have more than one value at one time. Just an idea.|||blindman,

Actual Field1 is the QuestionID and Field2 is the Answer

Basically one question would have 5 different choice of ans

Ex: Question1 Ans: Excellent, Good....etc

So i need to say in my query that return all rows where QuestionID 1 has Excellent QuestionID2 has Excellent|||Breaking out the crayons today...
WHERE (dbo.Field1= 1 AND Field2= 'Test')
OR (dbo.Field1= 2 AND Field2= 'Test')
OR (dbo.Field1= 3 AND Field2= 'Test')
OR (dbo.Field1= 4 AND Field2= 'Test')
OR (dbo.Field1= 5 AND Field2= 'Test')
...which is logically equivalent to: WHERE dbo.Field1 = IN (1, 2, 3, 4, 5) AND Field2= 'Test'

...I smell an MS Access GUID addict who thinks every new line is a separate predicate. You need to read up on SELECT statements in Books Online before you go any further with SQL programming.|||blindman,

No, i'm not breaking the Crayons today :-)

I understand what you are saying in your query but in my case it won't work since i'm trying to find all questions which has answer = Excellent.

This is how the questions are ask
Question1 :...........?
Ans: you are given five diff choice and ask to choose one|||Dude, I see references to "Field1", "Field2", 1, 2, 3, 4, 5, and 'test' in your query. Nowhere do I see any reference to 'Excellent'. It is also becoming more and more obvious that you have not even tried to figure this out yourself using the resources available in Books Online. So either do a little more research, or state your question more clearly.|||This query is obviously the output of a tool like Access. The solution in SQL would be simple. It's time to take the leap from GUI created logic to writing your own SQL. You will be glad after the initial learning curve.

OR

You could jump over to the Access forums and get better help.|||No, I don't think this is direct Access output, because it doesn't make syntactical sense. I think it is the output of someone who thinks that if they format their SQL statement to LOOK like the Access Query GUI grid it will behave like an Access Query.
Regardless, it's time to either take off the training wheels or hire a chauffeur to drive you around. Books Online.|||blindman,

Thanks for your help. I figure it out.

And for your quote " it's time to either take off the training wheels or hire a chauffeur to drive you around."

I think u need to take your quote for ur self since i figure out the query without online book reference.|||Well, gosh, how great for you. Maybe next time you can do that BEFORE posting such a simplistic question.|||Do I have to send you to your rooms?

Good Lord.

Dude, did you read the sticky at the top of the forum? It would really help us if you follow that model...

Blind dude...at least buy them dinner first....|||Heck no, it's dutch or nothin'.

Wednesday, March 7, 2012

Multiple Columns with different Values OR Single Column with Multiple Criteria?

Hi,

I have multiple columns in a Single Table and i want to search values in different columns. My table structure is

col1 (identity PK)
col2 (varchar(max))
col3 (varchar(max))

I have created a single FULLTEXT on col2 & col3.
suppose i want to search col2='engine' and col3='toyota' i write query as

SELECT

TBL.col2,TBL.col3

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col2,'engine') TBL1

ON

TBL.col1=TBL1.[key]

INNER JOIN

CONTAINSTABLE(TBL,col3,'toyota') TBL2

ON

TBL.col1=TBL2.[key]

Every thing works well if database is small. But now i have 20 million records in my database. Taking an exmaple there are 5million record with col2='engine' and only 1 record with col3='toyota', it take substantial time to find 1 record.

I was thinking this i can address this issue if i merge both columns in a Single column, but i cannot figure out what format i save it in single column that i can use query to extract correct information.
for e.g.;
i was thinking to concatinate both fields like
col4= ABengineBA + ABBToyotaBBA
and in search i use
SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABBToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]

Result = 1 row

But it don't work in following scenario
col4= ABengineBA + ABBCorola ToyotaBBA

SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABB*ToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]


Result=0 Row
Any idea how i can write second query to get result?

Before you merged the two columns could you not write your query something like this

select *

from TBL

where col2 = 'engine' and col3 = 'toyota'

You could put variables in place of the engine and toyota and create a stored procedure that passes in the values you want to search on.

I don't think there is a need to merge the columns matter of fact it will most likely run faster without being merged.

Or maybe I missed you rquestion.

Multiple Columns with different Values OR Single Column with Multiple Criteria?

Hi,

I have multiple columns in a Single Table and i want to search values in different columns. My table structure is

col1 (identity PK)
col2 (varchar(max))
col3 (varchar(max))

I have created a single FULLTEXT on col2 & col3.
suppose i want to search col2='engine' and col3='toyota' i write query as

SELECT

TBL.col2,TBL.col3

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col2,'engine') TBL1

ON

TBL.col1=TBL1.[key]

INNER JOIN

CONTAINSTABLE(TBL,col3,'toyota') TBL2

ON

TBL.col1=TBL2.[key]

Every thing works well if database is small. But now i have 20 million records in my database. Taking an exmaple there are 5million record with col2='engine' and only 1 record with col3='toyota', it take substantial time to find 1 record.

I was thinking this i can address this issue if i merge both columns in a Single column, but i cannot figure out what format i save it in single column that i can use query to extract correct information.
for e.g.;
i was thinking to concatinate both fields like
col4= ABengineBA + ABBToyotaBBA
and in search i use
SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABBToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]

Result = 1 row

But it don't work in following scenario
col4= ABengineBA + ABBCorola ToyotaBBA

SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABB*ToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]


Result=0 Row
Any idea how i can write second query to get result?

Before you merged the two columns could you not write your query something like this

select *

from TBL

where col2 = 'engine' and col3 = 'toyota'

You could put variables in place of the engine and toyota and create a stored procedure that passes in the values you want to search on.

I don't think there is a need to merge the columns matter of fact it will most likely run faster without being merged.

Or maybe I missed you rquestion.