Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » ElevateDB Technical Support » Technical Articles » Cross-Tab Result Sets with ElevateDB Scripts |
CREATE TABLE CrossTabSourceTable ( "Y-Axis Column" <Data Type> "X-Axis Column" <Data Type> "Total" <Data Type> )
CREATE DATABASE "CrossTabExample" PATH 'c:\crosstabexample' DESCRIPTION 'Cross-tab result set example database' CREATE TABLE "Customer" ( "No" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL, "Name" VARCHAR(60) COLLATE "ANSI" NOT NULL, "Address1" VARCHAR(60) COLLATE "ANSI" NOT NULL, "Address2" VARCHAR(60) COLLATE "ANSI", "City" VARCHAR(40) COLLATE "ANSI" NOT NULL, "StateProvince" VARCHAR(40) COLLATE "ANSI" NOT NULL, "PostalCode" VARCHAR(30) COLLATE "ANSI" NOT NULL, "Country" VARCHAR(40) COLLATE "ANSI" NOT NULL, "SalesPersonID" INTEGER NOT NULL, CONSTRAINT "No" PRIMARY KEY ("No"), CONSTRAINT "SalesPersonID" FOREIGN KEY ("SalesPersonID") REFERENCES "SalesPerson" ("ID") ) INSERT INTO "Customer" VALUES (1, 'Acme Fasteners, Inc.', '100 Main Street', NULL, 'Pleasantville', 'NY', '99999', 'United States', 1); INSERT INTO "Customer" VALUES (2, 'Smith Brothers Machinery, Inc.', '200 Cherry Street', NULL, 'Orangeland', 'CA', '77777', 'United States', 2); INSERT INTO "Customer" VALUES (3, 'Mister Plow, Inc.', '742 Evergreen Terrace', NULL, 'Springfield', 'Illinois', '55555', 'United States', 1); INSERT INTO "Customer" VALUES (4, 'Planet Express, Inc.', '3.14159265 Circle Street', NULL, 'New New York', 'NY', '3000', 'United States', 1); CREATE TABLE "SalesPerson" ( "ID" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL, "Name" VARCHAR(40) COLLATE "ANSI" NOT NULL, CONSTRAINT "ID" PRIMARY KEY ("ID") ) INSERT INTO "SalesPerson" VALUES (1, 'Guy Smiley'); INSERT INTO "SalesPerson" VALUES (2, 'Tom Sawyer'); CREATE TABLE "Orders" ( "CustomerNo" INTEGER NOT NULL, "OrderNo" INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL, "OrderDate" DATE NOT NULL, "OrderTotal" DECIMAL(20,2) DEFAULT 0.00 NOT NULL, CONSTRAINT "OrderNo" PRIMARY KEY ("OrderNo"), CONSTRAINT "CustomerNo" FOREIGN KEY ("CustomerNo") REFERENCES "Customer" ("No") ) INSERT INTO "Orders" VALUES (1, 1, DATE '2007-05-26', 2500); INSERT INTO "Orders" VALUES (1, 2, DATE '2007-06-14', 2200); INSERT INTO "Orders" VALUES (1, 3, DATE '2007-09-19', 3000); INSERT INTO "Orders" VALUES (2, 4, DATE '2007-01-06', 5000); INSERT INTO "Orders" VALUES (2, 5, DATE '2007-03-29', 4300); INSERT INTO "Orders" VALUES (3, 6, DATE '2007-10-02', 9800); INSERT INTO "Orders" VALUES (3, 7, DATE '2007-12-16', 3800); INSERT INTO "Orders" VALUES (4, 8, DATE '2007-07-18', 12000); INSERT INTO "Orders" VALUES (4, 9, DATE '2007-04-12', 6000); INSERT INTO "Orders" VALUES (4, 10, DATE '2007-04-28', 3000);
Table | Description |
Customer | Customer information, including the assigned salesperson |
SalesPerson | Salesperson information |
Orders | Customer orders with the order date and order total |
SELECT '#'+ CAST(SalesPerson.ID AS VARCHAR) + ' - ' + SalesPerson.Name AS SalesPerson, EXTRACT(MONTH FROM OrderDate) AS MonthNo, SUM(OrderTotal) AS TotalSales FROM Orders INNER JOIN Customer ON Customer.No=Orders.CustomerNo INNER JOIN SalesPerson ON SalesPerson.ID=Customer.SalesPersonID WHERE OrderDate BETWEEN ? AND ? GROUP BY SalesPerson, MonthNo
SCRIPT (IN StartDate DATE, IN EndDate DATE) BEGIN END
SCRIPT (IN StartDate DATE, IN EndDate DATE) BEGIN BEGIN EXECUTE IMMEDIATE 'DROP TABLE MonthlySalesCrossTab'; EXCEPTION END; EXECUTE IMMEDIATE 'CREATE TEMPORARY TABLE MonthlySalesCrossTab ( SalesPerson VARCHAR(60), January DECIMAL(20,2) DEFAULT 0.00, February DECIMAL(20,2) DEFAULT 0.00, March DECIMAL(20,2) DEFAULT 0.00, April DECIMAL(20,2) DEFAULT 0.00, May DECIMAL(20,2) DEFAULT 0.00, June DECIMAL(20,2) DEFAULT 0.00, July DECIMAL(20,2) DEFAULT 0.00, August DECIMAL(20,2) DEFAULT 0.00, September DECIMAL(20,2) DEFAULT 0.00, October DECIMAL(20,2) DEFAULT 0.00, November DECIMAL(20,2) DEFAULT 0.00, December DECIMAL(20,2) DEFAULT 0.00 )'; END
SCRIPT (IN StartDate DATE, IN EndDate DATE) BEGIN DECLARE ResultCursor CURSOR WITH RETURN FOR ResultStmt; UNPREPARE ResultStmt; BEGIN EXECUTE IMMEDIATE 'DROP TABLE MonthlySalesCrossTab'; EXCEPTION END; EXECUTE IMMEDIATE 'CREATE TEMPORARY TABLE MonthlySalesCrossTab ( SalesPerson VARCHAR(60), January DECIMAL(20,2) DEFAULT 0.00, February DECIMAL(20,2) DEFAULT 0.00, March DECIMAL(20,2) DEFAULT 0.00, April DECIMAL(20,2) DEFAULT 0.00, May DECIMAL(20,2) DEFAULT 0.00, June DECIMAL(20,2) DEFAULT 0.00, July DECIMAL(20,2) DEFAULT 0.00, August DECIMAL(20,2) DEFAULT 0.00, September DECIMAL(20,2) DEFAULT 0.00, October DECIMAL(20,2) DEFAULT 0.00, November DECIMAL(20,2) DEFAULT 0.00, December DECIMAL(20,2) DEFAULT 0.00 )'; PREPARE ResultStmt FROM 'SELECT * FROM MonthlySalesCrossTab'; OPEN ResultCursor; END
SCRIPT (IN StartDate DATE, IN EndDate DATE) BEGIN DECLARE SummaryCursor CURSOR FOR SummaryStmt; DECLARE ResultCursor CURSOR WITH RETURN FOR ResultStmt; UNPREPARE ResultStmt; BEGIN EXECUTE IMMEDIATE 'DROP TABLE MonthlySalesCrossTab'; EXCEPTION END; EXECUTE IMMEDIATE 'CREATE TEMPORARY TABLE MonthlySalesCrossTab ( SalesPerson VARCHAR(60), January DECIMAL(20,2) DEFAULT 0.00, February DECIMAL(20,2) DEFAULT 0.00, March DECIMAL(20,2) DEFAULT 0.00, April DECIMAL(20,2) DEFAULT 0.00, May DECIMAL(20,2) DEFAULT 0.00, June DECIMAL(20,2) DEFAULT 0.00, July DECIMAL(20,2) DEFAULT 0.00, August DECIMAL(20,2) DEFAULT 0.00, September DECIMAL(20,2) DEFAULT 0.00, October DECIMAL(20,2) DEFAULT 0.00, November DECIMAL(20,2) DEFAULT 0.00, December DECIMAL(20,2) DEFAULT 0.00 )'; PREPARE ResultStmt FROM 'SELECT * FROM MonthlySalesCrossTab'; OPEN ResultCursor; PREPARE SummaryStmt FROM 'SELECT ''#'' + CAST(SalesPerson.ID AS VARCHAR) + '' - '' + SalesPerson.Name AS SalesPerson, EXTRACT(MONTH FROM OrderDate) AS MonthNo, SUM(OrderTotal) AS TotalSales FROM Orders INNER JOIN Customer ON Customer.No=Orders.CustomerNo INNER JOIN SalesPerson ON SalesPerson.ID=Customer.SalesPersonID WHERE OrderDate BETWEEN ? AND ? GROUP BY SalesPerson, MonthNo'; OPEN SummaryCursor USING StartDate, EndDate; END
SCRIPT (IN StartDate DATE, IN EndDate DATE) BEGIN DECLARE SummaryCursor CURSOR FOR SummaryStmt; DECLARE ResultCursor CURSOR WITH RETURN FOR ResultStmt; DECLARE CurrentSalesPerson VARCHAR DEFAULT ''; DECLARE CurrentTotalSales DECIMAL(20,2) DEFAULT 0; DECLARE SalesPerson VARCHAR DEFAULT ''; DECLARE MonthNo INTEGER DEFAULT 0; DECLARE MonthName VARCHAR DEFAULT ''; DECLARE TotalSales DECIMAL(20,2) DEFAULT 0; UNPREPARE ResultStmt; BEGIN EXECUTE IMMEDIATE 'DROP TABLE MonthlySalesCrossTab'; EXCEPTION END; EXECUTE IMMEDIATE 'CREATE TEMPORARY TABLE MonthlySalesCrossTab ( SalesPerson VARCHAR(60), January DECIMAL(20,2) DEFAULT 0.00, February DECIMAL(20,2) DEFAULT 0.00, March DECIMAL(20,2) DEFAULT 0.00, April DECIMAL(20,2) DEFAULT 0.00, May DECIMAL(20,2) DEFAULT 0.00, June DECIMAL(20,2) DEFAULT 0.00, July DECIMAL(20,2) DEFAULT 0.00, August DECIMAL(20,2) DEFAULT 0.00, September DECIMAL(20,2) DEFAULT 0.00, October DECIMAL(20,2) DEFAULT 0.00, November DECIMAL(20,2) DEFAULT 0.00, December DECIMAL(20,2) DEFAULT 0.00 )'; PREPARE ResultStmt FROM 'SELECT * FROM MonthlySalesCrossTab'; OPEN ResultCursor; PREPARE SummaryStmt FROM 'SELECT ''#'' + CAST(SalesPerson.ID AS VARCHAR) + '' - '' + SalesPerson.Name AS SalesPerson, EXTRACT(MONTH FROM OrderDate) AS MonthNo, SUM(OrderTotal) AS TotalSales FROM Orders INNER JOIN Customer ON Customer.No=Orders.CustomerNo INNER JOIN SalesPerson ON SalesPerson.ID=Customer.SalesPersonID WHERE OrderDate BETWEEN ? AND ? GROUP BY SalesPerson, MonthNo'; OPEN SummaryCursor USING StartDate, EndDate; FETCH FIRST FROM SummaryCursor INTO SalesPerson, MonthNo, TotalSales; WHILE NOT EOF(SummaryCursor) DO SET CurrentSalesPerson = SalesPerson; INSERT INTO ResultCursor ('SalesPerson') VALUES (SalesPerson); WHILE NOT EOF(SummaryCursor) AND (SalesPerson = CurrentSalesPerson) DO CASE MonthNo WHEN 1 THEN SET MonthName='January'; WHEN 2 THEN SET MonthName='February'; WHEN 3 THEN SET MonthName='March'; WHEN 4 THEN SET MonthName='April'; WHEN 5 THEN SET MonthName='May'; WHEN 6 THEN SET MonthName='June'; WHEN 7 THEN SET MonthName='July'; WHEN 8 THEN SET MonthName='August'; WHEN 9 THEN SET MonthName='September'; WHEN 10 THEN SET MonthName='October'; WHEN 11 THEN SET MonthName='November'; WHEN 12 THEN SET MonthName='December'; END CASE; FETCH FROM ResultCursor (MonthName) INTO CurrentTotalSales; UPDATE ResultCursor SET MonthName = CurrentTotalSales + TotalSales; FETCH NEXT FROM SummaryCursor INTO SalesPerson, MonthNo, TotalSales; END WHILE; END WHILE; END
This web page was last updated on Wednesday, January 13, 2021 at 12:15 AM | Privacy PolicySite Map © 2025 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |