473,405 Members | 2,300 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,405 software developers and data experts.

How to create pivot tables?

Hi All,

How to convert rows to columns

the table is structured like:

NO_ID DATE_TRANS SHF
------ ---------- ---
080003 2010-08-21 11
080003 2010-08-22 99
080003 2010-08-23 88
080004 2010-08-21 11
080004 2010-08-22 99
080004 2010-08-23 88

This is the result I want to see :

NO_ID DATE_TRANS21 DATE_TRANS22 DATE_TRANS23
------ ------------ ------------ ------------
080003 11 99 88
080004 11 99 88

can you help me


Regard,
Rusli
Oct 21 '10 #1
1 2976
You could do it like this
SELECT NO_ID,
(SELECT B.SHF FROM TABLE B WHERE B.DATE_TRANS = '2010-08-21') DATE_TRANS_21,
(SELECT B.SHF FROM TABLE B WHERE B.DATE_TRANS = '2010-08-22') DATE_TRANS_22,
(SELECT B.SHF FROM TABLE B WHERE B.DATE_TRANS = '2010-08-23') DATE_TRANS_23
FROM TABLE A
GROUP BY NO_ID

or

SELECT NO_ID,
MAX((CASE WHEN DATE_TRANS = '2010-08-21' THEN SHF ELSE 0 END)) DATE_TRANS_21,
MAX((CASE WHEN DATE_TRANS = '2010-08-22' THEN SHF ELSE 0 END)) DATE_TRANS_22,
MAX((CASE WHEN DATE_TRANS = '2010-08-23' THEN SHF ELSE 0 END)) DATE_TRANS_23,
FROM TABLE
GROUP BY NO_ID


This is the general format for pivoting data.

In your case i'm assuming you'd want to generate this for different dates hence specifying the dates won't work. Let's say you apply the filters and all, you could still pivot it from least to max date as follows:

consider c1 and no_id, c2 as date and c3 as shf..

with temp(c1, c2, c3) as
(values (1,2, 11),(1,3,23),(1,4,45),(2,2,13),(2,3,132),(2,4,32)
),
temp2 as (
select row_number() over(partition by c1 order by c2) r1,c1, c2, c3
from temp
)
select o.c1,min(c2) start,
(select i.c3 from temp2 i where i.r1 = 1 and o.c1 = i.c1) val1,
(select i.c3 from temp2 i where i.r1 = 2 and o.c1 = i.c1) val2,
(select i.c3 from temp2 i where i.r1 = 3 and o.c1 = i.c1) val3
from temp2 o
group by c1


This would ofcourse work if there is a record for each date for each no_id.. if there's any missing, the values won't be displayed in the proper column.

But i hope you've gotten the basic idea of how data can be transposed. if you want to solve the problem of missing dates, then you could take a temp3 table with all distinct dates across all no_ids and then left join your table with temp3 so you'll be guaranteed a row for each..
Oct 22 '10 #2

Sign in to post your reply or Sign up for a free account.

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...
4
by: Del | last post by:
I need to create Pivot table in Excel from Access. Currently I run a query and output the data to an excel worksheet and create the pivots via automation. The issue I face is that the query may...
1
by: Johnny Meredith | last post by:
Dear All, I have an Access database that tracks the progress of income tax audits. When the taxing authorities make a change (an "Adjustment"), I record the pertinent information in the...
4
by: Ajay | last post by:
Hello all, I used to display reports in Excel earlier on my website. Now the client has requested that he would like see reports in Pivot table on the web . The backend is Sql2000. Can you please...
0
by: Zlatko Matić | last post by:
I have experienced some problems with total operations (sum, min, max, avg etc) in pivot tables nad pivot charts in .mde. In .mdb I can activate any totals operation. on both notebook and desktop...
3
by: nikila | last post by:
Hi, I have to create excel pivot tables from vb.net. Already I am creating excel file using oledb connection. I want to use the same to create the excel pivot tables. Can anyone please help me...
3
by: George Sakkis | last post by:
After a brief search, I didn't find any python package related to OLAP and pivot tables. Did I miss anything ? To be more precise, I'm not so interested in a full-blown OLAP server with an RDBMS...
1
by: huntress | last post by:
I am trying to create pivot tables using VBA in Access 2003. Is this possible? How do I go about it?
1
by: STUFIX | last post by:
Hi all, I created a pivot table that allowed users to select certain customers from a drop down list and display the relevant data - it worked fine but has now stopped allowing them to do that. ...
1
by: mld01s | last post by:
I really need help!!! I dont know if its possible to share pivot tables, or see pivot tables in other machines that the one where the tables were created. This is what happens: I created a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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,...
0
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...

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.