SQL Help -- Need help with pivoting the columns to rows
I have a requierement to split the columns into multiple rows. For example:
EMP_DEPT
rowid empid1 ename1 dept1 empid2 ename2 dep2 empid2 ename2 dep3 empid4 ename4 dept4
100001 1 'SCOTT' 10 2 'DAVE' 20 3 'MILLER' 10 4 SMITH 20
100002 1 'SCOTT' 10 2 'DAVE' 20 3 'MILLER' 20
Note: EMP_DEPT may not always have all the 4 employee info populated for example in row 2 only 3 employees info is there
I need to convert it and insert into EMPLOYEE table as follows:
EMPLOYEE
empid ename dept
1 SCOTT 10
2 DAVE 20
3 MILLER 10
4 SMITH 20
1 SCOTT 10
2 DAVE 20
3 MILLER 20
Thanks
Kev
Frank Thank You for your response.
I am on oracle 10gR2.
Posting some sample DDL and data here as requested:
CREATE TABLE EMP
PK NUMBER(10),
EMP_NAME1 VARCHAR2(100 BYTE),
EMP_ID1 NUMBER(10),
EMP_NAME2 VARCHAR2(100 BYTE),
EMP_ID2 NUMBER(10),
DEPT_NAME1 VARCHAR2(200 BYTE),
DEPT_ID1 NUMBER(10),
DEPT_NAME2 VARCHAR2(200 BYTE),
DEPT_ID2 NUMBER(10)
CREATE TABLE EMP_DEPT
( PK NUMBER(10),
ENTY_TYPE VARCHAR2(100),
ENTY_NAME VARCHAR2(100),
ENTY_ID NUMBER(10)
Insert into EMP
(PK, EMP_NAME1, EMP_ID1, EMP_NAME2, EMP_ID2, DEPT_NAME1, DEPT_ID1, DEPT_NAME2, DEPT_ID2)
Values
(1, 'SCOTT', 10001, 'FRANK', 10002,
'MARKETING', 10, 'ACCOUNTING', 20);
Insert into EMP
(PK, EMP_NAME1, EMP_ID1, EMP_NAME2, EMP_ID2, DEPT_NAME1, DEPT_ID1)
Values
(2, 'SCOTT1', 10003, 'FRANK1', 10004,
'MARKETING1', 30);
COMMIT;
SELECT
FROM
EMP;
PK EMP_NAME1 EMP_ID1 EMP_NAME2 EMP_ID2 DEPT_NAME1 DEPT_ID1 DEPT_NAME2 DEPT_ID2
1 SCOTT 10001 FRANK 10002 MARKETING 10 ACCOUNTING 20
2 SCOTT1 10003 FRANK1 10004 MARKETING1 30 My requirement is to:
SELECT from emp and INSERT INTO EMP_DEPT so that columns are broken into rows as follows
PK ENTY_TYPE, ENTY_NAME ENTY_ID
1 EMPLOYEE SCOTT 10001
2 EMPLOYEE FRANK 10002
3 DEPARTMENT MARKETING 10
4 DEPARTMENT ACCOUNTING 20
5 EMPLOYEE SCOTT1 10003
6 EMPLOYEE FRANK1 10004
7 DEPARTMENT MARKETING1 30 Thanks
Kevin
Edited by: user10210466 on Dec 8, 2010 1:37 PM
Similar Messages
-
SQL Help with change multiple columns into a single column
I am wanting to create either a new View or a new Table based on the following original table:
Original Table:
Fiscal Year
Account
Budget_Amt_January
Budget_Amt_February
Budget_Amt_March
Budget_Amt_April
<etc. for each of the 12 months)
I want the new View or Table to instead be:
New:
Fiscal_Year
Month
Account
Budget_Amount
I can't simply drop the original table, it will have to always exist since it is part of the Finance package, but for ease of reporting against I am wanting to create this new View.
Any ideas on how to go about this?
Thanks!I had to do something very similar just this week - in my case a record read from a csv into a table, then needs loading into a destination table, and the rows are columns in the csv. I went for INSERT ALL as I also had some conditions.
INSERT ALL
WHEN low1 != -998 OR high1 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 1, low1, high1 )
WHEN low2 != -998 OR high2 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 2, low2, high2 )
WHEN low5 != -998 OR high5 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 5, low5, high5 )
WHEN low6 != -998 OR high6 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 6, low6, high6 )
WHEN low7 != -998 OR high7 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 7, low7, high7 )
WHEN low8 != -998 OR high8 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 8, low8, high8 )
WHEN low9 != -998 OR high9 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 9, low9, high9 )
WHEN low10 != -998 OR high10 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 10, low10, high10 )
WHEN low11 != -998 OR high11 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 11, low11, high11 )
WHEN low12 != -998 OR high12 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 12, low12, high12 )
WHEN low13 != -998 OR high13 != -998 THEN
INTO partial_profile_temp (search_id, locus_id, low_allele, high_allele)
VALUES ( search_id, 13, low13, high13 )
SELECT * FROM bulk_search_load bst, partial_details_temp pdt
WHERE id = case_number -- only retrieves details for those records that are found in bulk_search_load
AND filename like 'NETH\_%' escape '\'
AND processed = 'N'; -
Need help with Pivoting rows to columns
Hi,
I need help with pivoting rows to columns. I know there are other posts regarding this, but my requirement is more complex and harder. So, please give me a solution for this.
There are two tables say Table 1 and Table 2.
Table1
name address email identifier
x e g 1
f s d 2
h e n 3
k l b 4
Table2
identifier TRno zno bzid
1 T11 z11 b11
1 T12 z12 b12
1 T13 z13 b13
2 T21 z21 b21
2 T22 z22 b22
As you can see the identifier is the column that we use to map the two tables. The output should be like below
output
name address email identifier TRno1 zno1 bzid1 TRno2 zno2 bzid2 TRno3 zno3 bzid3
x e g 1 T11 z11 b11 T12 z12 b12 T13 z13 b13
f s d 2 T21 z21 b21 t22 z22 b22
Also we do not know exactly how many TRno's, zno's, etc each value in the identifier will have. There may be only 1 TRNO, zno and bzid, or there may be four.
All the values must be in separate columns, and not be just comma delimitted. There are also other conditions that i have to add to restrict the data.
So, can you please tell me what is should use to get the data in the required format? We are using Oracle 10g. Please let me know if u need any more informationSomething like this ?
SCOTT@orcl> ed
Wrote file afiedt.buf
1 select a.name,
2 a.address,
3 a.email,
4 b.* from (
5 select distinct identifier
6 ,max(trno1) trno1
7 ,max(zno1) zno1
8 ,max(bzid1) bzid1
9 ,max(trno2) trno2
10 ,max(zno2) zno2
11 ,max(bzid2) bzid2
12 ,max(trno3) trno3
13 ,max(zno3) zno3
14 ,max(bzid3) bzid3
15 ,max(trno4) trno4
16 ,max(zno4) zno4
17 ,max(bzid4) bzid4
18 from (select identifier
19 ,decode(rn,1,trno,null) trno1
20 ,decode(rn,1,zno,null) zno1
21 ,decode(rn,1,bzid,null) bzid1
22 ,decode(rn,2,trno,null) trno2
23 ,decode(rn,2,zno,null) zno2
24 ,decode(rn,2,bzid,null) bzid2
25 ,decode(rn,3,trno,null) trno3
26 ,decode(rn,3,zno,null) zno3
27 ,decode(rn,3,bzid,null) bzid3
28 ,decode(rn,4,trno,null) trno4
29 ,decode(rn,4,zno,null) zno4
30 ,decode(rn,4,bzid,null) bzid4
31 from (select identifier,
32 trno,bzid,zno,
33 dense_rank() over(partition by identifier order by trno,rownum) rn
34 from table2)
35 order by identifier)
36 group by identifier) b,table1 a
37* where a.identifier=b.identifier
SCOTT@orcl> /
NAME ADDRESS EMAIL IDENTIFIER TRNO1 ZNO1 BZID1 TRNO2 ZNO2 BZID2 TRNO3 ZNO3 BZID3 TRNO4 ZNO4 BZID4
x e g 1 T11 z11 b11 T12 z12 b12 T13 z13 b13
f s d 2 T21 z21 b21 T22 z22 b22
SCOTT@orcl> select * from table1;
NAME ADDRESS EMAIL IDENTIFIER
x e g 1
f s d 2
h e n 3
k l b 4
SCOTT@orcl> select * from table2;
IDENTIFIER TRNO ZNO BZID
1 T11 z11 b11
1 T12 z12 b12
1 T13 z13 b13
2 T21 z21 b21
2 T22 z22 b22
SCOTT@orcl>Regards
Girish Sharma -
Question: Need help with overcoming the following message: "Nothing was imported.
Need help with overcoming the following message: “Nothing was imported. The file(s) or folder(s) selection to import did not contain any supported file types, or the files are already in this catalogue”.
The photos being scanned are old film shots. They have NOT been previously scanned. I am using Photoshop Elements 9 software.
QUESTION: how do I override this STOP and or circumvent the photo comparison option????
Thanks for the help. Bob K --- [email protected]Are you scanning as jpeg, tiff or some other format?
Are you using continuous numbering for files names as by definition scanned files have no exif data.
-
Hello I need your help with installing the flash drive into my laptop
Hi
I am Rachel Friedman asking for your help with installing the flashdrive player into my laptop.
Thanks
RachelTo give you any useful advice, I'm going to need to know more about your computer and browser:
https://forums.adobe.com/message/5249945#5249945 -
Help with Resizing of column widths in a pivot
HI all,
I m working on resizing the pivot coulmn width in a dashboard. Could anyone please help with resizing the pivot width. Autosize() works but user doesnt want that.
Whats the code to do that?I read what was written but still am confused. I am completely lost at this pointWhat is confusing you? A TreeMap is just a HashMap where the key is stored is sorted order.
Read the Collections tutorial found [url http://java.sun.com/docs/books/tutorial/]here. -
Need help on getting the column names of Tabletype
I have a table Mapping with the following values: This Mapping table contains the table names (in Tabname) and the column names(colname) of various tables and values(ValuesTobeFilled) for the columns.
I have to insert into the tables present in the Tabname field with the values present in the ValuesTobeFilled in the columns present in the Colname field.
Note: The Mapping table need not contain all the columns of the base table. The columns that are not present can be filled with null. And this mapping table is not fixed i.e. rows can be inserted/deleted frequently.
Sample values in mapping table:
Tabname Colname ValuesTobeFilled
sample_items Eno Corresponding Expression to get the values from XML input
sample_items Ename Corresponding Expression to get the values from XML input
XXX YYY Corresponding Expression to get the values from XML input
Before filling in the actual tables, I have to store the entire data temporarily and I have used a tabletype declared as follows:
TYPE T_sample_items IS TABLE OF sample_items%ROWTYPE INDEX BY BINARY_INTEGER;
l_sample_items T_sample_items;
Where the table sample_items have the following columns:
• Eno
• Ename
• Eaddress
• Eemail
So, the tabletype should be filled as:
Eno Ename Eaddress EEmail
1 XXX - -
I have declared a cursor to select the values from mapping table and I need to fill in the ValuesTobeFilled values to the corresponding table.
CURSOR c_xpath (c_tname mapping.TABNAME%type)
IS
select * from mapping where tabname = c_tname;
CURSOR c_tables
IS
SELECT DISTINCT TABNAME FROM mapping;
FOR crsr IN c_tables
LOOP
p_tname := CRSR.TABNAME;
FOR csr IN c_xpath(p_tname)
LOOP
IF l_xml_doc.EXISTSNODE(CSR.XPATH_EXP) = 1
THEN
l_node_value := l_xml_doc.extract(CSR.XPATH_EXP).getStringVal(); -- This is the value to be stored in the corresponding column
ELSE
l_node_value := NULL;
END IF;
IF CSR.COLUMN_NAME = ‘eno’
THEN
l_sample_items(1).eno := l_node_value;
ELSIF CSR.COLUMN_NAME = ‘ename’
THEN
l_sample_items(1).name := l_node_value;
END IF;
END LOOP;
END LOOP;
And I need to eliminate hard coding while comparing the column names (in the following piece of code) as the Mapping table values are subject to insertion/deletion:
IF CSR.COLUMN_NAME = ‘eno’
THEN
l_sample_items(1).eno := l_node_value;
ELSIF CSR.COLUMN_NAME = ‘ename’
THEN
l_sample_items(1).ename := l_node_value;
END IF;
I need to insert the values directly into the tabletype without this hardcoding. Please suggest me ways to compare the mapping table values with the field (column) names of the tabletype. If it is not possible using tabletype, please suggest any other ways of fixing the problem.
Many thanks,
GopiI take it this isn't going to be a serious production system at the end of the day?
Storing metadata in tables for extraction and insertion of data is just wrong in so many ways. It smells heavily of Entity Attribute Value modelling, which is the most wrong way to use a relational database and is known to have major performance implications and be liable to bugs and issues. The idea that EAV modelling allows for 'generic' databases where new data items can be added flexibly later on without having to change code is usually justified with an excuse of "it means we don't have to update all our tables when we want a new column" which is easily countered with "if you're adding a single column a good relational design wouldn't require you to add it to more than one table anyway in most cases".
Just what exactly are you trying to do and why? There has to be a better way. -
Requesting help with pivot in Oracle 10g
Hey all,
I have this table, let's call it table GRID that has the following columns:
OBJECT_KEY DATE_KEY SEGMENT_ID COLUMN_NAME COMMITTEDVIRTUALMEMORYSIZE_AVG
619 3371 1 M1100 593589862.4
619 3371 1 M1105 593611434.67
620 3371 1 M1100 592354508.8
620 3371 1 M1105 592376263.11
621 3371 1 M1100 731433369.6
621 3371 1 M1105 731455943.11What would it take to pivot the column names ('M1100', 'M1105') with the metric value volumn: 'COMMITTEDVIRTUALMEMORYSIZE_AVG' so much that this query would result in an output like this:
OBJECT_KEY DATE_KEY SEGMENT_ID M1100 M1105
619 3371 1 593589862.4 593611434.67
620 3371 1 592354508.8 592376263.11
621 3371 1 731433369.6 731455943.11Any help would be much appreciated!
Again, Oracle 10g, not 11g so I don't have the PIVOT function to work with.
Regards,
TimSHi,
Search for Pivot:, or see the example below, for how to do a basic pivot. (This was called "pivot" long before Oracle 11.)
The example below uses COUNT as the aggregate function; you'll probably wnat SUM instead.
If you don't know how many pivoted columns there will be (two, in the example you posted), or what their unique values are ('M1100' and 'M1105') ahead of time, then you will need dynamic SQL. As dynamic SQL goes, this is pretty easy; you can do it in either SQL*Plus or PL/SQL.
-- How to Pivot a Result Set (Display Rows as Columns)
-- For Oracle 10, and earlier
-- Actually, this works in any version of Oracle, but the
-- "SELECT ... PIVOT" feature introduced in Oracle 11
-- is better. (See Query 2, below.)
-- This example uses the scott.emp table.
-- Given a query that produces three rows for every department,
-- how can we show the same data in a query that has one row
-- per department, and three separate columns?
-- For example, the query below counts the number of employess
-- in each departent that have one of three given jobs:
PROMPT ========== 0. Simple COUNT ... GROUP BY ==========
SELECT deptno
, job
, COUNT (*) AS cnt
FROM scott.emp
WHERE job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY deptno
, job;
Output:
DEPTNO JOB CNT
20 CLERK 2
20 MANAGER 1
30 CLERK 1
30 MANAGER 1
10 CLERK 1
10 MANAGER 1
20 ANALYST 2
PROMPT ========== 1. Pivot ==========
SELECT deptno
, COUNT (CASE WHEN job = 'ANALYST' THEN 1 END) AS analyst_cnt
, COUNT (CASE WHEN job = 'CLERK' THEN 1 END) AS clerk_cnt
, COUNT (CASE WHEN job = 'MANAGER' THEN 1 END) AS manager_cnt
FROM scott.emp
WHERE job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY deptno;
-- Output:
DEPTNO ANALYST_CNT CLERK_CNT MANAGER_CNT
30 0 1 1
20 2 2 1
10 0 1 1
-- Explanation
(1) Decide what you want the output to look like.
(E.g. "I want a row for each department,
and columns for deptno, analyst_cnt, clerk_cnt and manager_cnt)
(2) Get a result set where every row identifies which row
and which column of the output will be affected.
In the example above, deptno identifies the row, and
job identifies the column.
Both deptno and job happened to be in the original table.
That is not always the case; sometimes you have to
compute new columns based on the original data.
(3) Use aggregate functions and CASE (or DECODE) to produce
the pivoted columns.
The CASE statement will pick
only the rows of raw data that belong in the column.
If each cell in the output corresponds to (at most)
one row of input, then you can use MIN or MAX as the
aggregate function.
If many rows of input can be reflected in a single cell
of output, then use SUM, COUNT, AVG, STRAGG, or some other
aggregate function.
GROUP BY the column that identifies rows.
PROMPT ========== 2. Oracle 11 PIVOT ==========
WITH e AS
( -- Begin sub-query e to SELECT columns for PIVOT
SELECT deptno
, job
FROM scott.emp
) -- End sub-query e to SELECT columns for PIVOT
SELECT *
FROM e
PIVOT ( COUNT (*)
FOR job IN ( 'ANALYST' AS analyst
, 'CLERK' AS clerk
, 'MANAGER' AS manager
NOTES ON ORACLE 11 PIVOT:
(1) You must use a sub-query to select the raw columns.
An in-line view (not shown) is an example of a sub-query.
(2) GROUP BY is implied for all columns not in the PIVOT clause.
(3) Column aliases are optional.
If "AS analyst" is omitted above, the column will be called 'ANALYST' (single-quotes included).
{code} -
I have rolled out bunch of updates across few hundred workstation systems.
Few of the desktops have failed to install SOME of the updates and just can't get my head around as to why?
Looking at one of the workstations, I can see 5 updates are pending download at 0%.
Some of these fail after a while and with in Software Centre, I can see 'Help with updates - The software change returned error code 0x87D00215(-2016411115).'. Not exactly sure what this means, but I have tried clearing the cache and
rebooting the workstations few times.
(I have also tried to use Windows Update and install one of these manually, which worked.)Investigating this further, I have found the following in CAS.log:
ICcmContentTransferManager::ModifyJobPriority failed with error 0x87d00215 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
No need to change timeout settings ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Successfully created download request {ED6E9E5C-E806-43CA-9F93-49AC72D1DEAD} for content 53bf87a2-bedf-4def-b0ec-9637613c3429.1 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Requesting locations synchronously for content 99ddb078-b780-4605-8fac-9607fe56450d.1 with priority Foreground ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
The number of discovered DPs(including Branch DP and Multicast) is 1 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Calling back with the following distribution points ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Distribution Point='http://DPSERVER.com/SMS_DP_SMSPKG$/99ddb078-b780-4605-8fac-9607fe56450d', Locality='LOCAL' ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Requesting content 99ddb078-b780-4605-8fac-9607fe56450d.1, size(KB) 0, under context System with priority Foreground ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
ICcmContentTransferManager::ModifyJobPriority failed with error 0x87d00215 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
No need to change timeout settings ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Successfully created download request {67B7E59B-A386-4EA8-BF39-2EB64B108A6C} for content 99ddb078-b780-4605-8fac-9607fe56450d.1 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Requesting locations synchronously for content 61d33de7-9582-41e3-82fc-3e0f970b60f3.1 with priority Foreground ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
The number of discovered DPs(including Branch DP and Multicast) is 1 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Calling back with the following distribution points ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Distribution Point='http://DPSERVER.com/SMS_DP_SMSPKG$/61d33de7-9582-41e3-82fc-3e0f970b60f3', Locality='LOCAL' ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Requesting content 61d33de7-9582-41e3-82fc-3e0f970b60f3.1, size(KB) 0, under context System with priority Foreground ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
ICcmContentTransferManager::ModifyJobPriority failed with error 0x87d00215 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
No need to change timeout settings ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4)
Successfully created download request {12E48136-B545-4C42-9745-9FF97CE38D52} for content 61d33de7-9582-41e3-82fc-3e0f970b60f3.1 ContentAccess 01/06/2013 17:19:24 3316 (0x0CF4) -
Pivot Some of The Columns to Rows
Hi,
I need to pivot some of the columns to rows and have the remaining columns in the result.
At this point I have already done a subquery to gather data and to keep the run time down I'd rather not do it again like a UNION would require.
Single Row
CUST TEST_DATE TEST_TYPE1 TEST_TYPE2 TEST_TYPE3
1 20-MAR-09 AA BB CC
Result Rows
CUST TEST_DATE TEST_TYPE
1 20-MAR-09 AA
1 20-MAR-09 BB
1 20-MAR-09 BB
Suggestions please?
Thank You in Advance for Your Help,
Louwith t as (
select 1 cust,to_date('20-MAR-09','dd-mon-yy') test_date,'AA' test_type1,'BB' test_type2,'CC' test_type3 from dual
select cust,
test_date,
case lvl
when 1 then test_type1
when 3 then test_type2
else test_type3
end test_type
from t,
(select level lvl from dual connect by level <= 3)
order by cust,
test_date,
test_type
CUST TEST_DATE TE
1 20-MAR-09 AA
1 20-MAR-09 BB
1 20-MAR-09 CC
SQL> SY. -
Eliminating Null After Pivoting the Column
hello,
I have a request where we are suppoed to Pivot a columns to Rows, doing so we get nulls. My questions here is what is the best approach to Pivot and Eliminate Nulls. does anyone came across anything similar?
ID
Name
Typeof record
Data1
Data 2
Data3
1
John
Type A
100
400
700
2
John
Type b
200
500
800
3
John
Type c
300
600
900
4
Marry
Type A
1000
4000
7000
5
Marry
Type b
2000
5000
8000
6
Marry
Type c
3000
6000
9000
To
ID
Name
PersonsData
Type A
Type B
Type C
1
John
Data1
100
2
John
Data1
200
3
John
Data1
300
1
John
Data2
400
2
John
Data2
500
3
John
Data2
600
1
John
Data3
700
2
John
Data3
800
3
John
Data3
900
4
Marry
Data1
1000
5
Marry
Data1
2000
6
Marry
Data1
3000
4
Marry
Data2
4000
5
Marry
Data2
5000
6
Marry
Data2
6000
4
Marry
Data3
7000
5
Marry
Data3
8000
6
Marry
Data3
9000How are you doing it?
Based on what you are showing us, you are unpivoting the [data] columns and pivoting the [type of record]. Then you will need to group by [name] and [person_data] but you will need to get rid of the [id] column because it is represented by the new columns
derived from [type_of_record] or removing it previous to the pivoting (guessing they are tied (id, type_of_record)).
DECLARE @T table (
id int,
name varchar(35),
type_of_record varchar(15),
data1 int,
data2 int,
data3 int);
INSERT INTO @T
(id, name, type_of_record, data1, data2, data3)
VALUES
(1, 'John', 'Type a', 100, 400, 700),
(2, 'John', 'Type b', 200, 500, 800),
(3, 'John', 'Type c', 300, 600, 900),
(4, 'Marry', 'Type a', 1000, 4000, 7000),
(5, 'Marry', 'Type b', 2000, 5000, 8000),
(6, 'Marry', 'Type c', 3000, 6000, 9000);
SELECT
P.name,
p.person_data,
MAX([Type a]) AS [type a],
MAX([Type b]) AS [type b],
MAX([Type c]) AS [type c]
FROM
@T
UNPIVOT
(value FOR person_data IN ([data1], [data2], [data3])) AS U
PIVOT
(MAX(value) FOR type_of_record IN ([Type a], [Type b], [Type c])) AS P
GROUP BY
P.name,
p.person_data
ORDER BY
P.name,
p.person_data;
-- removing column id previous to the pivoting
WITH R1 AS (
SELECT
U.name,
U.type_of_record,
U.person_data,
U.value
FROM
@T
UNPIVOT
(value FOR person_data IN ([data1], [data2], [data3])) AS U
SELECT
P.*
FROM
R1
PIVOT
(MAX(value) FOR type_of_record IN ([Type a], [Type b], [Type c])) AS P;
GO
AMB
Some guidelines for posting questions...
AYÚDANOS A AYUDARTE, guía básica de consejos para formular preguntas -
Setting the ALV Hierarchy tree with grid line between the columns and rows
Hi Experts,
I would like to ask if there is any suggestion on setting the ALV hierarchy tree to be separated by grid line between the columns and rows just like how it is display the same way in normal ALV grid.
Thanks in advance.Hi Lin,
The requirement which you have stated is not possible.
Lin,
Also i have a query regarding BADI ZME_PROCESS_REQ_CUST, which you had raised on SDN. You have marked the question as solved/answered.
Changing the data of a customize field in purchase requisition
Could you please let me know, the steps you did to update the screen fields through the BADI.
I would really appreciate your reply, because i am facing exactly the same problem which you have mentioned.
Thanks,
Best regards,
Prashant -
Can we hide the lines between the columns and rows of an alv in wd abap
HI all ,
I know that we can colour cell/column/row in an alv in wd abap.
but, can we hide the lines between the columns and rows of an alv in wd abap.
i have checked this link [hiding lines b/n rows and columns of an alv|http://help.sap.com/saphelp_nw04/helpdata/en/91/e7eb40c4f8712ae10000000a155106/content.htm]
but didn't understand, can please anybody provide some example or any material..? it will be very helpful.
THANK YOU.
Edited by: arfat111 on Feb 15, 2010 7:05 AMCode some like this in the WDDOINIT method of your view which defines the ALV component as used component.
instansiate the ALV component
data lo_cmp_usage type ref to if_wd_component_usage.
lo_cmp_usage = wd_this->wd_cpuse_usage_alv().
if lo_cmp_usage->has_active_component() is initial.
lo_cmp_usage->create_component().
endif.
data lo_interfacecontroller type ref to iwci_salv_wd_table.
lo_interfacecontroller = wd_this->wd_cpifc_usage_alv().
data lo_value type ref to cl_salv_wd_config_table.
lo_value = lo_interfacecontroller->get_model().
hide the grid lines
lo_value->if_salv_wd_table_settings~SET_GRID_MODE( value = '01' ).
Thanks,
Abhishek -
Need help with a calculated column - is there any way to reference a value in the current row?
Hey guys,
I'm a bit of a DAX newbie, and I'm running into a block. I'm creating a Power View report about IT tickets. We are going to be creating a cube to automate the data soon, I'm currently working with a flat Excel Data Table of data to demonstrate the Power
View reporting capabilities to the team. I need the default display to show the top 4-5 items basked on the Ticket Count. The three applicable columns I'm using are the TicketID, the ContactReason, and the AssetCategory - all three are
text. One slide will show the top five Contact Reasons by Ticket Count, and the other will show the top five Categories by Ticket Count. The users will see this default view, but will be able to change it to see differently ranked items or can clear the
ranking slicer altogether.
What I've accomplished so far is to create the Calculated Field [Ticket Count] = COUNTA(Table1[TicketID])
And 2 other calculated fields:
[Contact Rank] = RANKX(ALL(Table1[ContactReason]),[Ticket Count],,,DENSE)
[Asset Rank] = RANKX(ALL(Table1[AssetCategory]),[Ticket Count],,,DENSE)
If I were creating a Pivot Table, this would be great. These fields calculate everything the right way. The problem is, I need to have a Rank slicer on each slide and the calculation by itself contains no data - with no data, there's nothing to slice. I
realized I need to actually have columns of data so I can create a slicer. I need each row of the table to show the same [Contact Rank] for every instance of a particular ContactReason (and the same for the [Asset Rank] and AssetCategory).
The RANKX formulas pasted into the Calculated Column section only show a value of 1 - with no Pivot table summarizing the fields, it's counting each row's ticket once, giving every line the tied Rank of #1.
I've solved the problem in Excel by creating 2 Pivot Tables on a separate sheet that have the data field and the calculated field for ContactRason and AssetCategory. Then on my Excel Data Table, I've added two columns that do a VLOOKUP and pull over a the
Calculated Rank from each Pivot Table that match the ContactReason and AssetCategory fields. This works on the flat Excel Data Table now, but will not be a solutions when we start pulling the data from the cube (and there is no flat table).
What I think I need is an Expression for the RANKX formula that can give me, for each row, the count of all of the times a ContactReason shows up in an entire column. There's only about 100,000 lines of data and each ContactReason or AssetCategory
may show up several thousand times. But if I can get the expression to return that count, then the RANKX formula should work in the Column. If it wasn't a DAX formula, I'd use a COUNTIF and say 'Count the entire ContactReason column anytime it's equal to the
ContactReason on THIS row', but in DAX I don't know how to reference a single value in a row. I've tried the CALCULATE() formula, but it seems like the filter needs a specific value, and doesn't work on a dynamic "cell" value.
Any help would be greatly appreciated! (I hope it all makes sense!)If I've understood you correctly then the ALLEXCEPT function may be what you're after and it could be applied in a similar way to the following...
=
RANKX(
ALL(Table1),
CALCULATE(
COUNTROWS(table1),
ALLEXCEPT(Table1, Table1[ContactReason])
DENSE
If this has missed the mark, would it be possible to clarify the requirement further?
Regards,
Michael Amadi
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to vote it as helpful :)
Website: http://www.nimblelearn.com
Blog: http://www.nimblelearn.com/blog
Twitter: @nimblelearn -
Need Help with Creating the SQl query
Hi,
SQL query gurus...
INFORMATION:
I have two table, CURRENT and PREVIOUS.(Table Defs below).
CURRENT:
Column1 - CURR_PARENT
Column2 - CURR_CHILD
Column3 - CURR_CHILD_ATTRIBUTE 1
Column4 - CURR_CHILD_ATTRIBUTE 2
Column5 - CURR_CHILD_ATTRIBUTE 3
PREVIOUS:
Column1 - PREV_PARENT
Column2 - PREV_CHILD
Column3 - PREV_CHILD_ATTRIBUTE 1
Column4 - PREV_CHILD_ATTRIBUTE 2
Column5 - PREV_CHILD_ATTRIBUTE 3
PROBLEM STATEMENT
Here the columns 3 to 5 are the attributes of the Child. Lets assume that I have two loads, One Today which goes to the CURRENT table and one yesterday which goes to the PREVIOUS table. Between these two loads there is a CHANGE in the value for Columns either 3/4/5 or all of them(doesnt matter if one or all).
I want to determine what properties for the child have changed with the help of a MOST efficient SQL query.(PARENT+CHILD is unique key). The Database is ofcourse ORACLE.
Please help.
Regards,
ParagHi,
The last message was not posted by the same user_name that started the thread.
Please don't do that: it's confusing.
Earlier replies give you the information you want, with one row of output (maximum) per row in current_tbl. There may be 1, 2 or 3 changes on a row.
You just have to unpivot that data to get one row for every change, like this:
WITH single_row AS
SELECT c.curr_parent
, c.curr_child
, c.curr_child_attribute1
, c.curr_child_attribute2
, c.curr_child_attribute3
, DECODE (c.curr_child_attribute1, p.prev_child_attribute1, 0, 1) AS diff1
, DECODE (c.curr_child_attribute2, p.prev_child_attribute2, 0, 2) AS diff2
, DECODE (c.curr_child_attribute3, p.prev_child_attribute3, 0, 3) AS diff3
FROM current_tbl c
JOIN previous_tbl p ON c.curr_parent = p.prev_parent
AND c.curr_child = p.prev_child
WHERE c.curr_child_attribute1 != p.prev_child_attribute1
OR c.curr_child_attribute2 != p.prev_child_attribute2
OR c.curr_child_attribute3 != p.prev_child_attribute3
, cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT s.curr_parent AS parent
, s.curr_child AS child
, CASE c.n
WHEN 1 THEN s.curr_child_attribute1
WHEN 2 THEN s.curr_child_attribute2
WHEN 3 THEN s.curr_child_attribute3
END AS attribute
, c.n AS attribute_value
FROM single_row s
JOIN cntr c ON c.n IN ( s.diff1
, s.diff2
, s.diff3
ORDER BY attribute_value
, parent
, child
;
Maybe you are looking for
-
Inserting an array of byte[ ] in a database
Hi, In my program I have to insert a bynary data in my database. To do that, I created in the database a column, called "data", and I set the type of the column to MEDIUMBLOB. I receive the data that I have to store from a serial port, so they are st
-
How to import all Support Packages simulteneily support Package Manager EBP
Can any body tel me how to apply all support package sumultanesoly by using support Package Manager EBP/CRM option in SPAM. I could not able to select all pactche at a time.It selecting one patch at a time only Regards G Sasikanth Reddy
-
Invalid Index Error when Report is run on Web
Post Author: mhamill CA Forum: Crystal Reports I have a report which is being published and viewed on the web via a CrystalReportViewer app to display the reports (.rpt files)All other reports are running fine. When I run this new report in Crystal o
-
How to make a trasnparent background in Motion
I am just making a Lower Third and I want to be able to bring it into FCP and place it on top of my existing timeline. I dont want to export FCP to Motion because I already chroma keyed my "stuff" in FCP. How do I make a transparent background for Mo
-
Can Labview be installed on a portable harddrive
I would like to be able to take my work home with me and would like to know if I can install my labview development software on my portable hard drive so that I can plug in (USB) and work where ever I want.