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

Similar documents
Generating crosstabs in VFP

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

Consolidate data from a field into a list

Handling crosstabs and other wide data in VFP reports

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.

Summarizing aggregated data, Part 1

Advisor Answers. January, Visual FoxPro 3.0 and 5.0

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

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

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

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

PIVOT = Crosstabs, SQL Style

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

More on MS Access queries

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

Extend your queries with APPLY

Designing a Database -- Understanding Relational Design

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

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

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:

Stat Wk 3. Stat 342 Notes. Week 3, Page 1 / 71

Getting Started with AnyBook

Chapter 18 Outputting Data

Sending crosstabs to Excel

Introduction. Using Styles. Word 2010 Styles and Themes. To Select a Style: Page 1

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

Introduction. Opening and Closing Databases. Access 2010 Managing Databases and Objects. Video: Working with Databases in Access 2010

Watch the video below to learn more about number formats in Excel. *Video removed from printing pages. Why use number formats?

Introduction to Microsoft Excel

Including missing data

Going OVER and Above with SQL

Excel. Excel Options click the Microsoft Office Button. Go to Excel Options

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

Sorting and Filtering Data

M i c r o s o f t E x c e l A d v a n c e d P a r t 3-4. Microsoft Excel Advanced 3-4

Introduction. Table Basics. Access 2010 Working with Tables. Video: Working with Tables in Access To Open an Existing Table: Page 1

Database Tables Lookup Wizard Relationships Forms Subforms Queries Reports

TechTip: Exploit DB2 Web Query's Defined and Computed Fields

MITOCW ocw f99-lec07_300k

UAccess ANALYTICS. Intermediate Reports & Dashboards. updated v3.00

User Preferences & Security Snapshots

Data Warehouses, OLAP, and You Leslie Koorhan

Phone: Fax: Directions for setting up MARCO Insert Item #A-6LI 3 H x 4 W

Hello, and welcome to another episode of. Getting the Most Out of IBM U2. This is Kenny Brunel, and

Watch the video below to learn more about freezing panes in Excel. *Video removed from printing pages. To freeze rows:

Introducing Skype for Business

Microsoft Excel 2010 Training. Excel 2010 Basics

Lookup Transformation in IBM DataStage Lab#12

Naming Things in Adafruit IO

The Stack, Free Store, and Global Namespace

ADMISTRATOR S GUIDE INTERACTIVE REPORTING VERSION 3.3. Volume. Sales Analysis and Reporting Products

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Simple Invoicing Desktop Database with MS Access 2013/2016. David W. Gerbing School of Business Administration Portland State University

A Brief Writer s Guide to Book Indexing

User Manual Mail Merge

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

printf( Please enter another number: ); scanf( %d, &num2);

Unit 3 Fill Series, Functions, Sorting

Contents. What's New. Version released. Newsletter #31 (May 24, 2008) What's New New version released, version 4.3.3

Unit 3 Functions Review, Fill Series, Sorting, Merge & Center

Contents. What's New. Upcoming new version. Newsletter #43 (Aug 6, 2017) A couple quick reminders:

SEE GRADING CRITERIA AT THE BOTTOM. Database Tables Lookup Wizard Relationships Forms Queries Reports

PeopleSoft Data Security

It Might Be Valid, But It's Still Wrong Paul Maskens and Andy Kramek

While you can always enter data directly into database tables, you might find it easier to use

Introduction to Access 97/2000

Create your first workbook

Access Review. 4. Save the table by clicking the Save icon in the Quick Access Toolbar or by pulling

Notice on Access to Advanced Lists...2 Database Overview...2 Example: Real-life concept of a database... 2

ITS Training Class Charts and PivotTables Using Excel 2007

Assignment #6: Markov-Chain Language Learner CSCI E-220 Artificial Intelligence Due: Thursday, October 27, 2011

Use mail merge to create and print letters and other documents

ADMISTRATOR S GUIDE INTERACTIVE REPORTING VERSION 3.2. Volume. Sales Analysis and Reporting Products

ISM 4212/4480 Milestone V Submission Sample

Microsoft Excel Basics Ben Johnson

Database Wizard Guide. i-net Designer

Module 6. Campaign Layering

Integrating Visual FoxPro and MailChimp

their in the new a program such as Excel or Links aren't just document.

Chapter 4. Microsoft Excel

VISI ON CALC QuickStart Course

WAAT-PivotTables Accounting Seminar

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

Information Warehouse - Report Studio

Create and Modify Queries 7

Microsoft Access XP (2002) - Advanced Queries

GEO 425: SPRING 2012 LAB 9: Introduction to Postgresql and SQL

Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis. Copyright 2012 L. Leona Davis All Rights Reserved

Crystal Reports 2008 Designer 2 Workshop

Microsoft Office Illustrated Introductory, Building and Using Queries

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

Introduction to Microsoft Excel 2007

Relational Database Management Systems for Epidemiologists: SQL Part II

Basic Query for Human Resources

SurveyToGo Scripting Best Practices

PowerPoint Instructions

STIX Profile Development Tutorial

Connecting XML Data Sources to Word Using Windward Studios Report Designer

Using SQL-SELECT Effectively

Step 1: Create a totals query to show the total cost price and total sale price of the wine for each supplier.

Transcription:

July, 2006 Advisor Answers Create Cross-tabs Visual FoxPro 9/8/7 Q: I have a database that stores sales data. The details table contains one record for each sale of each item. Now I want to create a report showing sales of each item for each of three years. I want a column for each year and a row for each product. How can I create such a report? A: The kind of report you're looking for is called a cross-tab. Writing your own cross-tabulation can be pretty ugly. Fortunately, VFP provides a tool to do it for you. The tool is VFPXTAB.PRG and it's in the VFP home directory. You hand it a cursor or table containing three columns and it creates a new table or cursor where, by default, the first column provides the row information, the second column provides the column information, and the third column supplies the data that goes where rows and columns intersect. Let's look at an example to both clarify and show you how to use VFPXTAB. I'll use data from the Northwind database that comes with VFP. Figure 1 shows the report.

Figure 1: Creating cross-tabs The VFPXTab tool lets you create cross-tabs, so you can consolidate data for periods of time, locations, or other groupings. The first step in creating this report is to run a query that collects the data of interest. VFPXTab expects three columns; the example uses products for the rows, year for the columns and sales amount for the actual data. To call VFPXTab, you need a cursor with those three items. Here's the query that does the initial data collection: SELECT ProductID, YEAR(OrderDate) as nyear, ; Quantity*UnitPrice AS nsales ; FROM Orders ; JOIN OrderDetails ; ON Orders.OrderID = OrderDetails.OrderID ; WHERE YEAR(OrderDate) BETWEEN 1996 AND 1998 ; INTO CURSOR ProductsWithYears Note that I don't have to group and sum in this query; the cross-tab program takes care of that. The next step is easy: Call VFPXTab. The system variable _GENXTab points to the current cross-tab generator program. By using it, rather

than pointing to a specific program, you make it easier to switch to another cross-tab generator. Regardless of which generator you're using, for runtime, you need to explicitly set _GENXTab to the right program and make sure to include that program in your project. VFPXTab uses the table in the current work area as its input. It accepts up to 10 parameters, but all of them are optional. (They're documented in the header comments of the program.) In this case, you can ignore most of them. When you don't specify otherwise, VFPXTab stores the results in a table named XTab.DBF. While the name XTab is fine, I'd prefer a cursor, so I'll pass the first two parameters, the name of the result, and a flag indicating whether to create a table (.F.) or a cursor (.T.): DO (_GENXTAB) WITH "Xtab",.T. Figure 2 shows the result. The first column contains the product ID; there's one row for each product. After that, there's one column for each year. VFPXTab reads the data in the second column of the cursor you supply and creates a column for each unique value. The name of the column is based on that value; in this case, because the values are numeric and VFP field names must begin with a letter or underscore, the field names are prefaced with "N_". The intersection of each row and column contains the total sales for that product in that year.

Figure 2: Result VFPXTab creates one column for each unique value in the second column of the original data. A little polish While VFPXTab has done the hard part, there's a little more to do before running the report. I want to show the name of the product, not its ID. In addition, while VFPXTab sorts on the first column, in this case, that's not the right order, since I want the report alphabetical by product name. Here's a query that combines the cross-tab results with the Products table to get the data in exactly the form needed for the report: SELECT XTab.*, Products.ProductName ; FROM XTab ; JOIN Products ; ON XTab.ProductID = Products.ProductID ; ORDER BY ProductName ; INTO CURSOR SalesByYear

The only thing left to do is call the report. You'll find a program to build the data (SalesByYear.PRG) and the report in Figure 1 (SalesByYear.FRX) on this month's Professional Resource CD. The other parameters you're likely to deal with are the fifth, sixth, and seventh. These let you specify which fields of your original cursor or table supply the rows (fifth param), columns (sixth param) and data (seventh param) for the cross-tab. Although VFPXTab does a good job with what it can do, it is limited in some ways. In particular, it can only cross-tabulate one item at a time. If you want to cross-tabulate multiple fields (for example, quantity sold and total sales), you have to look at other tools. There are a couple of public domain cross-tab generators available for VFP, including FastXTab and MatXTab. You can find more information about them on the FoxPro Wiki at http://fox.wikis.com. Tamar