473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Narrowed Down Search

Hi All,

I'm trying to write a query that gets more granular as criteria is
available. for example:

select name where A is true, B MIGHT be true and C MIGHT be true.

Is that possible? The idea is that the more information available the
smaller the size of the dataset.

Thanks for any input.

May 9 '07 #1
5 1404
Looch (lu**********@yahoo.com) writes:
I'm trying to write a query that gets more granular as criteria is
available. for example:

select name where A is true, B MIGHT be true and C MIGHT be true.

Is that possible? The idea is that the more information available the
smaller the size of the dataset.
I'm not sure that I understand. SQL Server evaluates the conditions in
the WHERE clause as it sees fit. If the conditions are simple column
conditions, SQL Server is likely to evaluate them at once for the same
row. If the conditions refers to other table, there are more variations.

Maybe you could post a clarifying example by posting CREATE TABLE statements
for your tables and some INSERT statements with sample data and what result
you desire from the sample?
--
Erland Sommarskog, SQL Server MVP, es****@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
May 9 '07 #2
Looch <lu**********@yahoo.comwrote in news:1178739215.805604.182750
@y5g2000hsa.googlegroups.com:
Hi All,

I'm trying to write a query that gets more granular as criteria is
available. for example:

select name where A is true, B MIGHT be true and C MIGHT be true.

Is that possible? The idea is that the more information available the
smaller the size of the dataset.

Thanks for any input.
Perhaps something like

SELECT name FROM atable WHERE ColA = ValueA AND ColB = COALESCE(@B, ColB)
AND ColC = COALESCE(@C, ColC)
May 10 '07 #3
Erland,

I'll explain a little more. I'm just looking for a select query result
to fill a dataset.

I have a three text boxes in a VB app that I want to use as criteria
for a query.

txt1 = Serial Number
txt2 = Ship Date
txt3 = Item Description

All three item attributes (txt1 - 3) are columns in table that
includes much more information (Customer Name, Address, etc). Granted
this isn't ideally following the relationtional model.

If the user knows the Serial Number, that will return one record which
would be great. If they know only the Item Description that may return
100 records. If they know the Item Description and Ship Date that may
narrow it down to 25 records. I don't want to require txt1 - 3 be
included in the query but if the information is available than it
would be used in the query to help narrow down the search.

Thanks again.
May 10 '07 #4
Looch wrote:
I have a three text boxes in a VB app that I want to use as criteria
for a query.

txt1 = Serial Number
txt2 = Ship Date
txt3 = Item Description

All three item attributes (txt1 - 3) are columns in table that
includes much more information (Customer Name, Address, etc). Granted
this isn't ideally following the relationtional model.

If the user knows the Serial Number, that will return one record which
would be great. If they know only the Item Description that may return
100 records. If they know the Item Description and Ship Date that may
narrow it down to 25 records. I don't want to require txt1 - 3 be
included in the query but if the information is available than it
would be used in the query to help narrow down the search.
select <columns>
from <table>
where (@sn is null or sn = @sn)
and (@sd is null or sd = @sd)
and (@desc is null or desc = @desc)
May 10 '07 #5
Looch (lu**********@yahoo.com) writes:
I'll explain a little more. I'm just looking for a select query result
to fill a dataset.

I have a three text boxes in a VB app that I want to use as criteria
for a query.

txt1 = Serial Number
txt2 = Ship Date
txt3 = Item Description

All three item attributes (txt1 - 3) are columns in table that
includes much more information (Customer Name, Address, etc). Granted
this isn't ideally following the relationtional model.

If the user knows the Serial Number, that will return one record which
would be great. If they know only the Item Description that may return
100 records. If they know the Item Description and Ship Date that may
narrow it down to 25 records. I don't want to require txt1 - 3 be
included in the query but if the information is available than it
would be used in the query to help narrow down the search.
While the query suggested by Ed works, I would recommend this:

IF @serialnumber IS NOT NULL
BEGIN
SELECT ...
FROM tbl
WHERE serialnumber = @serialnumber
END
ELSE IF @shipdate IS NOT NULL
BEGIN
SELECT ...
FROM tbl
WHERE shipdate = @shipdate
AND itemdescription LIKE coalesce(@itemdesc, '%')
END
ELSE
BEGIN
SELECT ...
FROM tbl
WHERE itemdescription LIKE @itemdesc
END

The reason for this is performance. Assuming that the table has some
size, users don't want to wait a long time if they enter a serial
number. I assume that there is a unique index on the serial number.
I also assume that there is an index on shipdate, which is why I
single out this criteria as well. If there is no index on shipdate,
there is no point in this.

You may think: what if there are even more choices, isn't this getting
very complex then? Yes, and in that case dynamic SQL is to prefer. But
with only three choices, the above works well.

--
Erland Sommarskog, SQL Server MVP, es****@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
May 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: KK | last post by:
Drop-down menus are the hottest thing since Wonder Bread but . . . 1. Alot of people put them in the they-look-nice-but-you-cant-code-them-right-so-they-always-look-messed-up category (a la...
2
by: Praveen | last post by:
In the ISPF editor I am using, for a particullar PO Dataset I am getting the result of FIND statement narrowed down to the colums 48-56. i.e. if I give "FIND 'TO' ALL", the result I am getting as...
2
by: pemigh | last post by:
I have an almost-ready-to-install database with one problem. Data is displayed chronologically, and when the user goes to the last record and searches up to get most recent results, he often...
7
by: Smitty | last post by:
I have a function that imports an Excel file into an Access table using SQL. I then close the OleDataReader and the OleDbConnection, then dispose the OleDbCommand, then OleDbConnection. The calling...
4
by: Rudy | last post by:
Hello! I have 4 diffrent drop downlist. I want a user to select a value from a drop down list, and place it in a SELECT statement. How would I put that value in the select statement. And if the...
3
by: excel_hari | last post by:
Hi, I couldnt locate a Classic ASP group hence posting here. One of my colleagues has designed an intranet site and one of the pages has a drop-down box with close to 300 options. I want to...
0
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
2
by: leeperman | last post by:
In Dreaweaver I cannot filter my database results to display only specific data that is retrieved from mulptile drop down list on my search page. The drop down list selections are posted to my...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.