473,602 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pivot or Invert row data

Folks,

Given a table that might contain the rows:

DATE ID
==== ==
1/1/2001 1A
1/1/2001 2B
1/1/2001 3C
1/1/2001 4D
1/2/2001 1A
1/2/2001 2B
1/2/2001 3C
1/2/2001 4D
1/3/2001 1A
1/3/2001 2B
1/3/2001 3C
1/3/2001 4D
1/4/2001 1A
1/4/2001 2B
1/4/2001 3C
1/4/2001 4D

How then do I get a single query, table function or otherwise to
display said table as:

1/1/2001 1/2/2001 1/3/2001 1/4/2001
======== ======== ======== ========
1A 1A 1A 1A
2B 2B 2B 2B
3C 3C 3C 3C
4D 4D 4D 4D
I can get the individual columns to display, but cannot combine to show
all.

Any bright ideas on how I can do this outside of the Data Warehouse
transformers ?

Many thanks

Nov 12 '05 #1
7 22651
> I can get the individual columns to display, but cannot combine to show all.

What do you mean combine?
Could you show us the combined result image?

Did you already got the following result? And do you want get another
result?
Or, do you ask how to get the following image?

1/1/2001 1/2/2001 1/3/2001 1/4/2001
======== ======== ======== ========
1A 1A 1A 1A
2B 2B 2B 2B
3C 3C 3C 3C
4D 4D 4D 4D

Nov 12 '05 #2


Tonkuma wrote:
I can get the individual columns to display, but cannot combine to show all.


What do you mean combine?
Could you show us the combined result image?

Did you already got the following result? And do you want get another
result?
Or, do you ask how to get the following image?

1/1/2001 1/2/2001 1/3/2001 1/4/2001
======== ======== ======== ========
1A 1A 1A 1A
2B 2B 2B 2B
3C 3C 3C 3C
4D 4D 4D 4D


Indeed I am asking how to display the above 4 column image in one sql
query at the sametime. Sorry for the confusion. I can never get more
than one dates column to display at a time with falling into full
scalar select problems or something similar.

Nov 12 '05 #3
I assumed date format is mm/dd/yyyy.
Although, following sample is very complex and it may be simplified. It
worked on DB2 UDB for Windows V8.

------------------------------ Commands Entered
------------------------------
WITH
Add_rn1 AS (
SELECT date
, ROWNUMBER() OVER(ORDER BY date) rn1
FROM (SELECT DISTINCT DATE FROM tdavi) Q(date)
)
,Add_rn2 AS (
SELECT t.*, rn1
, ROWNUMBER() OVER(PARTITION BY t.date ORDER BY t.id) rn2
FROM tdavi t
, Add_rn1 n
WHERE t.date = n.date
)
,Recurse (ln, rn1, id) AS (
SELECT 1 ln
, rn1
, id
FROM Add_rn2
WHERE rn2 = 1
UNION ALL
SELECT pre.ln + 1
, pre.rn1
, new.id
FROM Recurse pre
, Add_rn2 new
WHERE pre.ln < 10000
AND pre.rn1 = new.rn1
AND new.rn2 = pre.ln + 1
)
SELECT col1 AS " ", col2 AS " ", col3 AS " ", col4 AS " "
FROM (
SELECT -1 ln
, MAX(CASE rn1 WHEN 1 THEN CHAR(date,USA) END)
, MAX(CASE rn1 WHEN 2 THEN CHAR(date,USA) END)
, MAX(CASE rn1 WHEN 3 THEN CHAR(date,USA) END)
, MAX(CASE rn1 WHEN 4 THEN CHAR(date,USA) END)
FROM Add_rn1
UNION ALL
VALUES (0, '==========', '==========', '==========',
'==========')
UNION ALL
SELECT ln
, MAX(CASE rn1 WHEN 1 THEN id END)
, MAX(CASE rn1 WHEN 2 THEN id END)
, MAX(CASE rn1 WHEN 3 THEN id END)
, MAX(CASE rn1 WHEN 4 THEN id END)
FROM Recurse
GROUP BY ln
) Q (ln, col1, col2, col3, col4)
ORDER BY ln;
------------------------------------------------------------------------------

---------- ---------- ---------- ----------
01/01/2001 01/02/2001 01/03/2001 01/04/2001
========== ========== ========== ==========
1A 1A 1A 1A
2B 2B 2B 2B
3C 3C 3C 3C
4D 4D 4D 4D

6 record(s) selected.

Nov 12 '05 #4
I assumed date format is mm/dd/yyyy, and the values of DATE are not
known before access the table.

WITH
Add_rn AS (
SELECT date
, id
, DENSERANK() OVER(ORDER BY date) rn1
, ROWNUMBER() OVER(PARTITION BY date ORDER BY id) rn2
FROM tdavi
)
SELECT col1 AS " ", col2 AS " ", col3 AS " ", col4 AS " "
FROM (
SELECT -1
, MAX(CASE rn1 WHEN 1 THEN CHAR(date,USA) END)
, MAX(CASE rn1 WHEN 2 THEN CHAR(date,USA) END)
, MAX(CASE rn1 WHEN 3 THEN CHAR(date,USA) END)
, MAX(CASE rn1 WHEN 4 THEN CHAR(date,USA) END)
FROM (SELECT DISTINCT date
, rn1
FROM Add_rn) Q
UNION ALL
VALUES (0, '==========', '==========', '==========',
'==========')
UNION ALL
SELECT rn2
, MAX(CASE rn1 WHEN 1 THEN id END)
, MAX(CASE rn1 WHEN 2 THEN id END)
, MAX(CASE rn1 WHEN 3 THEN id END)
, MAX(CASE rn1 WHEN 4 THEN id END)
FROM Add_rn
GROUP BY rn2
) Q (ln, col1, col2, col3, col4)
ORDER BY ln;
------------------------------------------------------------------------------
---------- ---------- ---------- ----------
01/01/2001 01/02/2001 01/03/2001 01/04/2001
========== ========== ========== ==========
1A 1A 1A 1A
2B 2B 2B 2B
3C 3C 3C 3C
4D 4D 4D 4D

6 record(s) selected.

Nov 12 '05 #5
Thanks for that Tonkuma, that works very nicely when modified for my
tables. I appreciate the assistance.

One question, how well would this scale depending on how many distinct
dates are in the table, and what if I knew what they were prior to
needing to run the query ? I know I'm asking a lot, but would it be
able to scale to say an entire year ?

Again, my thanks for your help .. it's much appreciated.

Tim

Nov 12 '05 #6
Tim,

This is a rather strange table. All your IDs have the same values and
there is no real data. I'll take a gamble here with an extended example
hoping I don't lead you off-topic:

CREATE TABLE CHIPTRUCK_SALES
(LOCATION VARCHAR(20),
DATE DATE,
HOTDOGS_SOLD INT);

INSERT INTO CHIPTRUCK_SALES VALUES
('Claremont', '1/1/2001', 10),
('Brougham', '1/1/2001', 20),
('Greenwood', '1/1/2001', 8),
('Whitevale', '1/1/2001', 5),
('Claremont', '1/2/2001', 11),
('Brougham', '1/2/2001', 22),
('Greenwood', '1/2/2001', 15),
('Whitevale', '1/2/2001', 17),
('Claremont', '1/3/2001', 8),
('Brougham', '1/3/2001', 19),
('Greenwood', '1/3/2001', 12),
('Whitevale', '1/3/2001', 7);

SELECT
LOCATION,
MAX(CASE WHEN date = '1/1/2001' THEN hotdogs_sold END) AS "1/1/2001",
MAX(CASE WHEN date = '1/2/2001' THEN hotdogs_sold END) AS "1/2/2001",
MAX(CASE WHEN date = '1/3/2001' THEN hotdogs_sold END) AS "1/3/2001"
FROM CHIPTRUCK_SALES GROUP BY LOCATION ORDER BY LOCATION;

=>
LOCATION 1/1/2001 1/2/2001 1/3/2001
-------------------- ----------- ----------- -----------
Brougham 20 22 19
Claremont 10 11 8
Greenwood 8 15 12
Whitevale 5 17 7

4 record(s) selected.

A true PIVOT would flip columsn and rows I presume:

DROP TABLE CHIPTRUCK_SALES ;

CREATE TABLE CHIPTRUCK_SALES
(DATE DATE, BROUGHAM INT, CLAREMONT INT, GREENWOOD INT, WHITEVALE INT);

INSERT INTO CHIPTRUCK_SALES VALUES
('1/1/2001', 20, 10, 8, 5),
('1/2/2001', 22, 11, 15, 17),
('1/3/2001', 19, 8, 12, 7);

SELECT * FROM CHIPTRUCK_SALES ;

DATE BROUGHAM CLAREMONT GREENWOOD WHITEVALE
---------- ----------- ----------- ----------- -----------
01/01/2001 20 10 8 5
01/02/2001 22 11 15 17
01/03/2001 19 8 12 7

3 record(s) selected.

SELECT
LOCATION,
MAX(CASE WHEN date = '1/1/2001' THEN hotdogs_sold END) AS "1/1/2001",
MAX(CASE WHEN date = '1/2/2001' THEN hotdogs_sold END) AS "1/2/2001",
MAX(CASE WHEN date = '1/3/2001' THEN hotdogs_sold END) AS "1/3/2001"

FROM CHIPTRUCK_SALES ,
TABLE(VALUES('B rougham', BROUGHAM),
('Claremont', CLAREMONT),
('Greenwood', GREENWOOD),
('Whitevale', WHITEVALE))
AS SALES(LOCATION, HOTDOGS_SOLD)
GROUP BY LOCATION ORDER BY LOCATION;

LOCATION 1/1/2001 1/2/2001 1/3/2001
--------- ----------- ----------- -----------
Brougham 20 22 19
Claremont 10 11 8
Greenwood 8 15 12
Whitevale 5 17 7

4 record(s) selected.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7
Theoretically, the number of distinct dates is limited by "Most
elements in a select list"(=500(4Kpa ge) or =1012(>=8Kpage) ) according
to "SQL Reference Volume 1 Version 8.2, Appendix A. SQL limits Table
33".
But, I don't know the practical limit. I feel at least some tens of
columns would be not so problem.

A shortcoming of this way(or SQL syntax itself) is need to determine(or
know) the number of columns in a select list and code them in a SQL
before execute the SQL and access the table.
It might be better to code some more columns in the SQL than estimated
distinct dates. Data of extra columns may be NULL.

Nov 12 '05 #8

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

Similar topics

0
2092
by: Prasad Patil | last post by:
Hi, I have created a report in excel, it uses Pivot tables. The excel has two sheets 1: Pivot ( Contains the Pivot Table) 2: Data (Data Requred to populate the pivot table I create an instance of Excel populate the data sheet and load excel in the browser.
0
833
by: Rami | last post by:
Has any body tried using pivot tables in C# I am trying to achieve pivot table functionality ( Rendering Row fields / Column Fields / Data fields ) in C# with Excel 2003. I have complete data in one dataset and i would like to render it on Excel similar to pivot table Is anybody have idea how Excel renders and presents data in the form of pivot table?Are is there any articles which explain the rendering logic of pivot tables Thanks in...
1
3134
by: Grey | last post by:
I have created a asp.net form for user to input data. After input the data, user need to click a button to export the input data to excel for data analysis with excel pivot table function. is it possible to do that?? if so, any sample for me as i am a new in designing pivot table with asp.net million thanks
3
10253
by: Jerry K via DotNetMonster.com | last post by:
I'm creating a pivot table using vb.net and the data is from sqlserver (desktop). I have been successful at creating the pivot table, which includes a 'date' column field. I'd like to group the date column field by months and quarters, but can't come up with the correct code. Here is the code I've written to create the pivot table. Any help to code grouping the date column field would be appreciated. Thanks, Jerry
5
27717
by: JayDawg | last post by:
Is there any way to create a pivot table in a report? I have a query with the fields Date, Name, and Data, and I am trying to create a report that has the dates running across the top (like fields) grouped by month, with the names running down the left, and the data values under each appropriate month. I am totally stumped on how to do this, other than using a Pivot Table, but that will not suffice as this has to be created as a report.
9
6782
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury Bonds, the Prime rate, and so-forth. We associate a security with one of those rates. There are a set of rates for each calendar day, and the rates for that
5
4526
by: Pourya99 | last post by:
Hello, I have an Access Data Access Page which has a pivot table. The data source of the pivot table is a SQL database table. The data in the pivot table itself is not a problem. I have a text box below the pivot table. Let's say the pivot table has: First Name, Last Name, and Address. When I click on a record in the pivot table, I want the Address for that selected record to be displayed in the textbox below. What I have currently...
1
2623
benchpolo
by: benchpolo | last post by:
I have data extracted from Access db to Excel with a pivot table. Somehow, I am having issues with the pivot table were it doesnt update the totals. For example, the first extract i did in Access has 6000 data rows and pivot table in excel should refresh on open and reflect the 6000 data rows. 2nd extract in Access has 10000 data rows when pivot table opens it doesn't refresh the summary count on the pivot table based on the 10000 data...
3
2564
by: Thyag | last post by:
Hi All, I need to group multiple tables in to a pivot. Could some body help me. Thanks in Advance, Thyag
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8268
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.