473,396 Members | 1,995 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.

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 22608
> 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('Brougham', 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(4Kpage) 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
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...
0
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...
1
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...
3
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...
5
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...
9
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...
5
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...
1
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...
3
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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
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,...

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.