Advisor Answers. Match multiple items in a query. December, 2005 VFP 9/8/7

Similar documents
Advisor Answers. Create Cross-tabs. July, Visual FoxPro 9/8/7

Advisor Answers. January, Visual FoxPro 3.0 and 5.0

Advisor Answers. Creating New Sort Orders. June, Visual FoxPro 6.0, 5.0 and 3.0

ISM 4212/4480 Milestone V Submission Sample

9 No limit. Number of UNIONs 9 No limit. 30 No limit. 1 No limit

Generating crosstabs in VFP

Consolidate data from a field into a list

Connecting XML Data Sources to Word Using Windward Studios Report Designer

and I want the subsets with three members, the result would be:

Solutions to the Problems in SQL Practice Problems by Sylvia Moestl Vasilik. John Weatherwax

Advanced SQL. Chapter 8 finally!

Extend your queries with APPLY

Handling crosstabs and other wide data in VFP reports

Professional Edition Tutorial: Excel Spreadsheets

In SQL window, type SHOW TABLE STATUS; and discuss INNODB vs. MyISAM (foreign key support; transactions; row-level locking)

COOKBOOK Creating an Order Form

Stored Procedures and Functions. Rose-Hulman Institute of Technology Curt Clifton

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL. Overview

CS1100: Data, Databases, and Queries QUERY CONSTRUCTION. CS1100 Microsoft Access 1

Advisor Discovery. Use BindEvent() to keep things in synch. BindEvent() Refresher. June, By Tamar E. Granor, technical editor

CHAPTER: 4 ADVANCE SQL: SQL PERFORMANCE TUNING (12 Marks)

Oracle 1Z Oracle Database SQL Expert. Download Full Version :

QUETZALANDIA.COM. 5. Data Manipulation Language

Using SQL-SELECT Effectively

Connecting SQL Data Sources to Excel Using Windward Studios Report Designer

Learning Alliance Corporation, Inc. For more info: go to

IMPORTING DATA IN PYTHON I. Introduction to relational databases

Speed in Object Creation and. Destruction. March 2016 Number 49. Tamar E. Granor, Ph.D.

Oracle 1Z Oracle Database 12c SQL. Download Full Version :

Database Wizard Guide. i-net Designer

These are the steps you will take to complete the tutorial. They are also the common processes through which you will create your own operations.

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

Advisor Answers. Clean up a Project. May, Visual FoxPro 9/8/7

A File Open Dialog Doug Hennig

Chris Singleton 03/12/2017 PROG 140 Transactions & Performance. Module 8 Assignment

Oracle 1Z0-071 Exam Questions and Answers (PDF) Oracle 1Z0-071 Exam Questions 1Z0-071 BrainDumps

Assignment 6: SQL III

You can use PIVOT even when you don t know the list of possible values, and you can unpivot in order to normalize unnormalized data.

Practice Test. Microsoft Microsoft PRO: Designing and Optimizing Data Access by Using Microsoft SQL Server Version 2.

Manage Your Applications Doug Hennig

Advance SQL: SQL Performance Tuning. SQL Views

Aggregating and Pivoting Data

Summarizing aggregated data, Part 1

Microsoft Exam Transition Your MCTS on SQL Server 2008 to MCSA: SQL Server 2012, Part 1 Version: 7.8 [ Total Questions: 183 ]

Let's start by taking a look at the object generated by LIST OBJECTS. I used the following code to put a few objects in memory for testing:

Instructor: Craig Duckett. Lecture 11: Thursday, May 3 th, Set Operations, Subqueries, Views

Assignment Grading Rubric

SQL - Tables. SQL - Create a SQL Table. SQL Create Table Query:

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Splitting a Procedure File

Give users a control that makes entering dates as easy as it is in Intuit Quicken.

GETTING STARTED WITH CODE ON TIME Got data? Generate modern web apps in minutes. Learn to create sophisticated web apps with Code On Time application

Taking Advantage of Idle Cycles. Make Your Application Work When the User Isn't. The Idea. The Strategy. December, 2003

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

Working with the Registry. The Registry class makes it easy. The Registry Structure. January, By Tamar E. Granor

Querying Data with Transact-SQL

INFO-H415 - Advanced Databases NoSQL databases and Cassandra. Lev Denisov Todi Thanasi

Using Microsoft Access to Create Queries and Write SQL Statements

1z0-047 Exam Questions Demo Oracle. Exam Questions 1z Oracle Database SQL Expert.

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

MySQL Workshop. Scott D. Anderson

A File Open Dialog Box Doug Hennig

Oracle EXAM - 1Z Oracle Database SQL Expert. Buy Full Product.

Assignment 6: SQL III Solution

Including missing data

May I See Your License? Doug Hennig

Microsoft Exam

Integrating Word's Spellcheck with Your VFP Application

Question No : 1 Which three possible values can be set for the TIME_ZONE session parameter by using the ALTER SESSION command? (Choose three.

Subquery: There are basically three types of subqueries are:

PIVOT = Crosstabs, SQL Style

Zip it, Zip it Good Doug Hennig

Getting Started with Performance Manager

Session V-STON Stonefield Query: The Next Generation of Reporting

ISTEC.MIP Measurement Data Integration Platform

Graphing crosstabs. Tamar E. Granor, Ph.D.

Strings Dates and DateTimes Identifiers

Pronto User Guide. Birst Software Version Document Release Wednesday, November 01, Copyright Birst, Inc.

Module 11: Implementing Triggers

ORACLE PL/SQL DATABASE COURSE

CIS 363 MySQL. Chapter 12 Joins Chapter 13 Subqueries

Views in SQL Server 2000

Fig. 1 Relationship View of Northwind.mdb

Structure Query Language (SQL)

Going OVER and Above with SQL

Database Normalization

Kaotii.

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Quick Start Guide. EnterpriseEnablerQuickStartGuide with new tem. Model: Version 7.0, 9/2010 VERSION: Page 1 of 74

CATCH Me if You Can Doug Hennig

Ahmedabad SQL Server User Group Ahmedabad, Gujarat, India July 19, Microsoft MVP SQL Server Founder SQLAuthority.com

Combining Data.the EG way

Oracle Compare Two Database Tables Sql Query List All

Microsoft. Developing SQL Databases Version: Demo. [ Total Questions: 10] Web:

IntelliSense at Runtime Doug Hennig

1z Oracle Database SQL Expert

Q: I've been playing with the Microsoft Internet Transfer Control (inetctls.inet.1) and it would be great if only it worked.

If this is the first time you have run SSMS, I recommend setting up the startup options so that the environment is set up the way you want it.

COOKBOOK Row-Level Security

Q&As Querying Data with Transact-SQL (beta)

Transcription:

December, 2005 Advisor Answers Match multiple items in a query VFP 9/8/7 Q: I have a form with a multi-select listbox. After a user chooses some items, I want to find all the records in a table that have any of the selected values in a specified field. I know how to substitute one value into a query, but how can I handle a list of values, when I don't know how many there might be in the list? A: There are actually several ways to handle this problem. The first, building a query with a long series of ORs in the WHERE clause, would get pretty ugly, so we won't even consider that option. Instead, we'll look at two choices. The first is to use the IN operator against a list of values; the second is to put the matching values into a cursor and use that in the query. Figure 1 shows a form that lets you try both options, showing orders that include any of the selected products. Figure 1. Looking for matches to a set of values This form demonstrates both using the IN clause and putting the values to match into a cursor. The WHERE clause of SQL SELECT offers the IN operator to match an expression against any of a list of values. For example, the following query finds customers in North America (all the examples here use the Northwind database):

SELECT CompanyName ; FROM Customers ; WHERE UPPER(Country) IN ("USA", "CANADA", "MEXICO") ; INTO CURSOR NACustomers The trick in your case, then, is to build the list of values and substitute into the query. Here's the code in the custom UseIN method of the form (called from the Click method of the Use IN button), which does exactly that: LOCAL clistvalues, nindex * Build a list of items clistvalues = "" WITH ThisForm.lstProducts as ListBox FOR nindex = 1 TO.ListCount IF.Selected[ nindex ] clistvalues = clistvalues + "," + ;.List[ nindex, 2 ] ENDWITH clistvalues = SUBSTR(cListValues, 2) * Run the query IF NOT EMPTY(cListValues) SELECT OrderID, OrderDate, CustomerID, ; RequiredDate, ShippedDate ; FROM Orders ; WHERE OrderID IN ; (SELECT OrderID ; FROM OrderDetails ; WHERE ProductID IN (&clistvalues)) ; ORDER BY OrderID ; INTO CURSOR ProductsOrdered The first section of code loops through the list, adding each selected item to a comma-separated string. The second section runs the query, using the macro operator (&) to substitute the string. This solution has a couple of limitations. The most important relates to the IN operator. In VFP 8 and earlier, the list of items with IN is limited to 24. In VFP 9, the limit has been raised, but is restricted by the value of SYS(3055). In addition, macro-substituted strings are limited to 8,192 characters. In VFP 8 and earlier, the limit on IN is likely to hit you first, but in VFP 9, depending on the data you're matching, the macro limit could be a significant problem. However, when the overall list of items is small, using IN is a viable solution.

The second option isn't affected these limits. Instead of building a string, put the selected values into a cursor and use that cursor in the query. Here's the code in the form's custom UseCursor method (called from the Use cursor button's Click method): LOCAL clistvalues, nindex * Build a list of items CREATE CURSOR ProductsChosen (ProductID I) WITH ThisForm.lstProducts as ListBox FOR nindex = 1 TO.ListCount IF.Selected[ nindex ] INSERT INTO ProductsChosen ; VALUES ( VAL(.List[ nindex, 2] )) ENDWITH * Run the query IF RECCOUNT("ProductsChosen") > 0 SELECT OrderID, OrderDate, CustomerID, ; RequiredDate, ShippedDate ; FROM Orders ; WHERE OrderID IN ; (SELECT OrderID ; FROM OrderDetails ; JOIN ProductsChosen ; ON OrderDetails.ProductID = ; ProductsChosen.ProductID ) ; ORDER BY OrderID ; INTO CURSOR ProductsOrdered In this case, the first section of the code loops through the list, adding the primary key of each selected item to a cursor. Then, the second section runs the query, joining the newly created cursor to existing tables to restrict results to those orders containing the selected items. You may be surprised by the use of a subquery in both cases; its purpose is to get a single list of the relevant orders, which are then pulled out from the Orders table. If the whole process is performed in the main query (whether it's with IN or a join), the same order may appear more than once in the results. In my testing, there's no performance difference between the two methods. Use the Speed test button to check performance on your machine. The code is set up to run each technique 1000 times, and provide feedback every 100 passes, but you can change the values of

the variables npasscount and nincrement in the SpeedTest method (called from the Speed test button's Click method) to try other combinations: WAIT WINDOW "Testing speed of two approaches" NOWAIT LOCAL npass, nstart, nend, nintime, ncursortime LOCAL cmessage, npasscount, nincrement npasscount = 1000 nincrement = 100 WAIT WINDOW "Testing IN operator" NOWAIT cmessage = "IN operator: Pass " nstart = SECONDS() FOR npass = 1 TO npasscount IF MOD(nPass, nincrement) = 0 This.edtSpeedTest.Value = cmessage + ; TRANSFORM(nPass) This.UseIN() nend = SECONDS() nintime = nend - nstart WAIT WINDOW "Testing cursor" NOWAIT cmessage = "Cursor: Pass " nstart = SECONDS() FOR npass = 1 TO npasscount IF MOD(nPass, nincrement) = 0 This.edtSpeedTest.Value = cmessage + ; TRANSFORM(nPass) This.UseCursor() nend = SECONDS() ncursortime = nend - nstart This.edtSpeedTest.Value = "Speed Test Results for " + TRANSFORM(nPassCount) + " passes" + CHR(13) + CHR(10) + ; "Using IN operator: " + ; TRANSFORM(nINTime) + " seconds." + ; CHR(13) + CHR(10) + ; "Using cursor: " + TRANSFORM(nINTime) + " seconds." WAIT CLEAR The example form in Figure 1 is included on this month's Professional Resource CD.

Tamar