Matrix query
I had an excel sheet that I loaded into Oracle that was design like a matrix. The excel sheet had about 25 columns starting with a number and then a 1 in the column that is relevant:
For example:
Name Male Female Trans Both Unknown
Bob 1
Lisa 1
Lola 1
BobbySue 1
Dana 1
So I have it in Oracle like:
Name Varchar2(50)
Male Varchar2(1)
Female Varchar2(1)
Trans Varchar2(1)
Both Varchar2(1)
Unkown Varchar2(1)
So I want a query that puts all the different options in one column with the column title so:
Query Output:
Bob Male
Lisa Female
Lola Trans
BobbySue Both
Dana Unkown
Any help on writing a query that can produce this would be greatly appreciated.
Use "UNPIVOT":
SQL> WITH t1 (name, male, female, trans, both, unknown) 2 AS (SELECT 'Bob ', 1, 0, 0, 0, 0 FROM DUAL UNION ALL 3 SELECT 'Lisa ', 0, 1, 0, 0, 0 FROM DUAL UNION ALL 4 SELECT 'Lola ', 0, 0, 1, 0, 0 FROM DUAL UNION ALL 5 SELECT 'BobbySue ', 0, 0, 0, 1, 0 FROM DUAL UNION ALL 6 SELECT 'Dana ', 0, 0, 0, 0, 1 FROM DUAL) 7 SELECT * 8 FROM t1 9 UNPIVOT (flag FOR xsex IN (male, female, trans, both, unknown)) 10 WHERE flag = 1 11 / NAME XSEX FLAG-Bob MALE 1Lisa FEMALE 1Lola TRANS 1BobbySue BOTH 1Dana UNKNOWN 1 SQL>
Similar Messages
-
Hi ,experts
i just need a matrix query report on sales order. It contains quanity ana monthHi,
Welcome you post on the forum.
You may check this first: Monthly Sales By Geography
Please do not post more than one thread with similar question. Make sure your question is easily understandable.
You could also get my book from Packtpub for knowing more about query in B1.
Thanks,
Gordon -
How To Write A Matrix Query in Oracle 10g
Hi All,
I need to write a query displaying total of each Month and each quarter total. Here is example:
CREATE TABLE T_CUST_REG(
CUST_ID NUMBER,
CUST_NAME VARCHAR2(255),
REGDATE DATE);
INSERT INTO T_CUST_REG VALUES (1, 'A','01-JAN-2012');
INSERT INTO T_CUST_REG VALUES (2, 'B','01-FEB-2012');
INSERT INTO T_CUST_REG VALUES (3, 'C','01-MAR-2012');
INSERT INTO T_CUST_REG VALUES (4, 'D','01-APR-2012');
INSERT INTO T_CUST_REG VALUES (5, 'E','01-MAY-2012');
INSERT INTO T_CUST_REG VALUES (6, 'F','01-JUN-2012');
INSERT INTO T_CUST_REG VALUES (7, 'G','01-JUL-2012');
INSERT INTO T_CUST_REG VALUES (8, 'H','01-AUG-2012');
INSERT INTO T_CUST_REG VALUES (9, 'I','01-SEP-2012');
INSERT INTO T_CUST_REG VALUES (10, 'J','01-OCT-2012');
INSERT INTO T_CUST_REG VALUES (11, 'K','01-NOV-2012');
INSERT INTO T_CUST_REG VALUES (12, 'L','01-DEC-2012');
Output REQUIRED:
JAN
FEB
MAR
Q1
APR
MAY
JUN
Q2
JUL
AUG
SEP
Q3
OCT
NOV
DEC
Q4
1
1
1
3
1
1
1
3
1
1
1
3
1
1
1
3
I am able to create matrix with following query, but issue is how to put Quarter(total) in between.
Select Count(1), TO_CHAR(REGDATE, 'MON') MON
FROM T_CUST_REG T
GROUP BY TO_CHAR(REGDATE, 'MON');
RegardsHi,
The query you posted will produce a separate row for each month, not a separate column. Is that what you want?
If so, you can add the quarterly totals with GROUPING SETS, like this:
SELECT COUNT (*) AS cnt
, CASE
WHEN GROUPING (TRUNC (regdate, 'MONTH')) = 0
THEN TO_CHAR (TRUNC (regdate, 'MONTH'), 'YYYY MON')
ELSE TO_CHAR (TRUNC (regdate, 'Q'), 'YYYY "Q"Q')
END AS label
FROM t_cust_reg
GROUP BY GROUPING SETS ( (TRUNC (regdate, 'MONTH'))
, (TRUNC (regdate, 'Q'))
Instead of calling TRUNC over and over, you might want to do it just 2 times, in a sub-query, giving aliases to the results, and then use the aliases over and over in the main query.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the exact results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
Need help to write matrix query
Hi all,
I have the query . I'm getting output like this
chargetype amount start_date
DA1 170 04/01/2005
DA2 1170 04/01/2005
DA3 1730 04/01/2005
DA4 17 04/01/2005
DA5 -120 04/01/2005
DA6 0 04/01/2005
DA1 170 04/02/2005
DA2 2005 04/02/2005
DA3 590 04/02/2005
DA4 201 04/02/2005
DA5 340 04/02/2005
DA6 120 04/02/2005
I need my output like this
chargetype 04/01/2005 04/02/2005 04/03/2005 ......
DA1 170 170
DA2 1170 2005
DA3 1730 590
DA4 17 201
DA5 -120 340
DA6 0 120
pls help me out. your help is greatly appreciated.
the real query is .
SELECT st . sttl_item_nme charge_type,
SUM ( NVL ( sd . sttl_item_amt , 0 )) Adjustment ,
trunc(s . START_DT_GMT) start_date_gmt
from nm_sttl_item_dtl_type st ,
nm_sttl_item_dtl sd ,
nm_settlement s
where st . sttl_item_num = sd . sttl_item_num
and sd . sttl_id = s . sttl_id
and trunc(s . START_DT_GMT) between trunc(to_date('04/01/2005', 'mm/dd/yyyy')) and trunc(to_date('04/02/2005', 'mm/dd/yyyy'))
and s . ptcpt_cd = 'DEMO'
and s . sttl_pub_cd = ( select sttl_pub_cd
from nm_settlement c
where trunc(c.master_rpt_version_dt_sys) = (select trunc(max(ss.master_rpt_version_dt_sys))
from nm_settlement ss
where ss.ptcpt_cd = 'DEMO'
and ss.source_cd = 'ISO'
and trunc(ss.start_dt_gmt) = trunc(s.start_dt_gmt)
and sttl_id = s.sttl_id)
group by st . sttl_item_nme, trunc(s . START_DT_GMT)
order by trunc(s . START_DT_GMT), sttl_item_nme
Thanks & Regards,
Ramana.Search this site for "pivot" or "cross tab."
-
Primavera P6 R8.1 functionality matrix query.
Hi Can you please help me?
I am currently looking at the P6 R8.1 functionality matrix and it contains the following new features:
1. View user sessions screen
2. Draw bars based on UDF dates
3. Create Hierarchical Custom Risk Categories
4. Monitor Projects for over allocation
I am trying to find out the details of each element. Is there any details anywhere on these new features?
Thank youuse web client for admin features.
-
hello grp,thanks for you help.................
i have again some doubts to solve can anyone plz solve these..........
1) Create a query that will display the total number of employees and, of that total, the number of employees hired in 1995, 1996, 1997 and 1998.Create appropriate column headings as "Total", "1995", "1996" etc...
2) Create a matrix query to display the job, the salary for that job based on department number, and the total salary for that job, for department 20, 50, 80 and 90, giving each column an appropriate heading.
till now i have learnt use of multiple row functions and not subqueries,n solutions to above queries r not given.so plz help me out guys
thanx again in advance............Ok, as you're obviously still struggling, I'll give you a pointer for the first question without actually doing it with your data...
SQL> create table mydata (val number);
Table created.
SQL> insert into mydata values (1);
1 row created.
SQL> insert into mydata values (2);
1 row created.
SQL> insert into mydata values (3);
1 row created.
SQL> insert into mydata values (1);
1 row created.
SQL> insert into mydata values (2);
1 row created.
SQL> insert into mydata values (4);
1 row created.
SQL> ed
Wrote file afiedt.buf
1 select cnt.total,
2 sum(decode(mydata.val,1,1)) as val1,
3 sum(decode(mydata.val,2,1)) as val2,
4 sum(decode(mydata.val,3,1)) as val3,
5 sum(decode(mydata.val,4,1)) as val4
6 from mydata,
7 (select count(*) as total from mydata) cnt
8* group by cnt.total
SQL> /
TOTAL VAL1 VAL2 VAL3 VAL4
6 2 2 1 1
SQL>Now see if you can adapt that for your own data and let us know how you get on.
Then have an attempt yourself at the second question and come back to us with whatever you have done and we can help to correct it for you.
You'll only learn if you understand what it's doing yourself. -
Please reply to this query asap
Create a matrix query to display the job,salary for that job based on department number and the total salary for that job for all departments,giving each column an app: heading
JOB DEPTNO(10) 20 30 SUM(SAL)
ANALYST 0 100 0 100
SALARY AND JOB AND DEPTNO ARE IN EMP TABLEIf you want to pivot your emp table, use the forum's search function for some examples, such as this thread:
Re: pivot query
C. -
create a matrix query to display a job, the salary for that job based on department number, and the total salary for that job, for departments 20,50,80,90 giving each column an appropriate heading. this is the excise query available in oracle in ebook. I have solved it by using inline view.. given below.. Is ther any other way to solve the same quey.. Please help..
SELECT job_id,
CASE
WHEN department_id=20
THEN salary
ELSE NULL
END AS dept20,
CASE WHEN department_id=50 THEN salary ELSE NULL
END AS dept50,
CASE WHEN department_id=80 THEN salary ELSE NULL
END AS dept80,
CASE WHEN department_id=90 THEN salary ELSE NULL
END AS dept90,
salary
FROM
(SELECT job_id,
SUM(salary) salary,
department_id
FROM employees
WHERE department_id IN (20,50,80,90)
GROUP BY job_id,
department_id
) order by job_id;Hi,
you can try something like this:
*not tested
select distinct job_id,
(select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 20) sum_sal_20,
(select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 50) sum_sal_50,
(select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 80) sum_sal_80,
(select nvl(sum(salary), 0) from employees where job_id = e.job_id and department_id = 90) sum_sal_90
from employees e
where department_id IN (20,50,80,90)
group by job_id, department_idRegards
Imran -
Matrix report with Multiple queries
I have created one cross product group containing 4 subgroups under a single query. Now I have another one query seperately to be joined with the matrix query. Also I have one formula column and summary column with this the cross product group. The logic is if formula condition is true the it should take the 2nd query value in the place holder column otherwise it should take the first query value. is it possible?
If any one knows about this update me ASAP to [email protected]Isn't there a way for you to do this via a Package/Procedure versus having multiple queries?
Per the BI Publisher guide,
Following are recommended guidelines for building data models:
Reduce the number of data sets or queries in your data model as much as possible. In general, the fewer data sets and queries you have, the faster your data model will run. While multiquery data models are often easier to understand, single-query data models tend to execute more quickly. It is important to understand that in parent-child queries, for every parent, the child query is executed.
You should only use multiquery data models in the following scenarios:
To perform functions that the query type, such as a SQL query, does not support directly.
To support complex views (for example, distributed queries or GROUP BY queries).
To simulate a view when you do not have or want to use a view.
Thanks,
Bipuser -
I have been trying to add a graph to a matrix report (v. 9.0.2.0.5), with Region (char value) on the x-axis and numerics on the y-axis, but in the picklists, none of the char fields show up. Is there a solution/workaround?
Thanks in advance,
Martin HoerchnerMy work around was to copy my matrix query and put a graph-friendly version(ie: one without all the grouping) of the query in my report along with the matrix query.
-
Matrix + DataTable re-SELECT problem(s)
Hi,
I try to do something like this:
ScreenPainter -> Simple form with matrix and DataTable as data source for it. All Matrix columns are bounded by aliases to data source. In the Matrix and DT I have got one DOUBLE (price) field and two DATE fields - my sources of problems.
At the begining everything is OK but next I try to do something like this:
dt -> DataTable
mx -> Matrix
query = "select * from [dbo].[@MY_TABLE] where my_col1 = 'foo'"; // original SELECT statement is without WHERE clause
dt.Clear();
dt.ExecuteQuery(query);
mx.Clear();
mx.LoadFromDataSource();
And there are surprises (for me ) because after that, in the DOUBLE field I've got "hash" value and DATE fields are in STRING format eg. 20081103 instead of 08/11/03
I checked out what values are in DataTable after ExecuteQuery and they are OK.
Any suggestion(s)?
BR and Thx,Before requery you must unbind the columns inmatrix and bind it again. I wrote the sample code today to Lars, look there.
-
Query displaying diffrence between highest and lowest salarys?
I need to write a query that will display the difference between the highest and lowest salaries. Label the column DIFFERENCE.
It should look something like so. I have no idea what to type in SQL plus to get this.
DIFFERENCE
4200Sweetness, thanks for this help, I just also started at this company called avid technologies
www.avid.com and I Just learned SAP.
Heres another problem if you can help:
Create a matrix query to display the job, the salary for that job based on department number, and the total salary for that job for all departments, giving each column an appropriate heading
and again the formatting is messed up.
Job Dept 10 Dept 20 Dept 30 Total
ANALYST 6000 6000
CLERK 1300 1900 950 4150
MANAGER 2450 2975 2850 8275
PRESIDENT 5000 5000
SALESMAN 5600 5600 -
Averaging Columns (by months of the year)
Running Oracle 9i
I have this query which takes fields: ID, Date, and Data and performs a crosstab (matrix) query which has the day column, the 12 months, and the total for each day.
I have been trying to add averaging for each month at the bottom of the result and total after that so that the result looks like:
Day Jan Feb Mar ... Oct Nov Dec Total
1 1.1 2.2 3.3 4.4 5.5 6.6 999.99
2
30
31 3.3 6.6
(currently looks like the above.. Need to change query so that the next two lines are displayed)
Average 2.2 4.4 ....... etc.. 5.5 (blank)
Total 4.4 8.8........ etc... 11.0 999.99
I tried to use an example of the ROLLUP and just got different errors..
Here is the current query:
select to_char(piDate,'dd') as day
,sum(decode(to_char(piDate,'mm'),'01',piValue)) as Jan
,sum(decode(to_char(piDate,'mm'),'02',piValue)) as Feb
,sum(decode(to_char(piDate,'mm'),'03',piValue)) as Mar
,sum(decode(to_char(piDate,'mm'),'04',piValue)) as Apr
,sum(decode(to_char(piDate,'mm'),'05',piValue)) as May
,sum(decode(to_char(piDate,'mm'),'06',piValue)) as Jun
,sum(decode(to_char(piDate,'mm'),'07',piValue)) as Jul
,sum(decode(to_char(piDate,'mm'),'08',piValue)) as Aug
,sum(decode(to_char(piDate,'mm'),'09',piValue)) as Sep
,sum(decode(to_char(piDate,'mm'),'10',piValue)) as Oct
,sum(decode(to_char(piDate,'mm'),'11',piValue)) as Nov
,sum(decode(to_char(pidate,'mm'),'12',piValue)) as Dec
,sum(piValue) as Total
,max(reportid) as ReportID
,max(waterdatatypeid) as WaterDataTypeID
from LPInflow
where reportid=14 and pidate >=to_date('2006/01/01','yyyy/mm/dd') and
pidate <= to_date('2006/12/31','yyyy/mm/dd')
group by to_char(piDate,'dd'),to_char(piDate,'yyyy');
Also, the reportid is the same for all, but just using reportid instead of min/max reportid causes the query to fail..
If I could get the correct query systax it would be great..
Thanks .Very Much appreciated..Hi Swaroop,
I have a issue with allocation logic i.e is my scenario require allocation logic or i can write general logic,if yes,can you give a suggestion how to write..
Scenario is..in my plan..
I prepared a schedule for SALES planing...for which i have prepared a input scedule..
My input schedule works like..COLs-SalesAccountits memberset isfiltered using propety i.e test3="C"
ROWs-ENTITY--its memberset is filtered using propety i.e Plants="A"
Time--Taking from CV.
When i take time 2009.INPUT from CV and enter the data for entity for respect salesaccounts and send the data.NEXT,
I want to copy the same data to all the months of 2009...for that iam using allocation like
*RUNALLOCATION
*FACTOR
//*USING
*DIM ENTITY WHAT=Entity; WHERE=<<<: USING=Entity.PLANTS
*DIM SALESAccount WHAT=SALESACCOUNT; WHERE=<<<: USING<>SALESACCOUNT.TEST3
*DIM TIME WHAT=BAS(%YEAR%.TOTAL); WHERE=<<<; USING=%TIME_SET%;
*ENDALLOCATION
will it work my scenario does i need to add any other logic ...
Suggest me sol..
regards,
Naresh. -
Taking a long time to retrieve 3400 values..plzz help??
I have a page wch displays 3400 values from the database in the matrix form which looks like this..........
..................................role1 role2 role3 role4 role5 role6..................................................role34
competency1 1 0 3
competency2 0 2 1
competency3 0 1 4
.... 3 0 5
competency100.............2 5
Each role will be given a value(0 to 5) against a competency.I use the following code in the jspx to display them in the matrix form.....problem is its taking abt 4-5mins for retrieving the rows.
I want the rows to retrieve fastly....can any one plzzz...help....
im using ADFBC
jdev 10.1.3
<h:form>
<af:panelHorizontal>
<af:objectSpacer width="237" height="10" />
<af:panelForm maxColumns="#{bindings.RatingRoleMasterVO1Iterator.estimatedRowCount}" rows="1">
<af:forEach items="#{bindings.RatingRoleMasterVO2.rangeSet}" var="paras">
<af:inputText value="#{paras.Role}" readOnly="false" styleClass="txt" />
<af:objectSpacer width="40" height="10" />
</af:forEach>
</af:panelForm>
</af:panelHorizontal>
<af:objectSeparator />
<af:panelHorizontal>
<af:panelForm>
<af:forEach items="#{bindings.RatingCompetencyMasterVO2.rangeSet}" var="parm">
<af:inputText value="#{parm.Competency}" readOnly="false" />
</af:forEach>
</af:panelForm>
<af:panelForm maxColumns="#{bindings.RatingRoleMasterVO1Iterator.estimatedRowCount}"
rows="#{bindings.RatingCompetencyMasterVO1Iterator.estimatedRowCount}">
<af:forEach items="#{bindings.matrixVO2.rangeSet}" var="parms">
<af:inputText value="#{parms.LevelRequired}" readOnly="false" columns="1"
inlineStyle="text-align:center;"/>
</af:forEach>
</af:panelForm>
</af:panelHorizontal>
</h:form>
</afh:body>
</afh:html>
</f:view>
</jsp:root>Edited by: ADFexplorer on Mar 16, 2009 8:40 AM
Edited by: ADFexplorer on Mar 16, 2009 8:42 AM
Edited by: ADFexplorer on Mar 16, 2009 8:44 AM
Edited by: ADFexplorer on Mar 16, 2009 8:46 AMWhat database server do you use? if it is Oracle and in newer version (e.g. 10g or above) you should consider to handle your query at database side. For newer Oracle database version, it provides very good support on matrix query.
-
Different layouts in one single report
Hi,
My report consists of 6 different spreasheets, 2 has Landscape layout and 4 has Portrait layout.. so i m trying to create 2 portrait in Header section, 2 Landscapes in Main section, and 2 portrait again in the trailer section.. i have six diff queries for six sheets.. i created one Lanscape in main section with the matrix query.. i m trying to create 1 portrait in trailer section (tabular ) ..... but when i finish this tabular and try to run it, it changes the layout for matrix report in the MAIN section ???? does it overwrite the original layout, which was set for diff query ?
How to solve this ???? I tried with report block as well...
Pleaseeeeeeee help..Hi,
bring your two different layouts in seperate frames and write format triggers on this frames to show or suppress them (depending on your input). Connect the two frames with an collabsing anchor.
Regards
Rainer
Maybe you are looking for
-
Breaking up xml configurations?
Hi, I'm wondering if there's an easy way to break up xml cache configurations into different (partial) xml files so that they can be easily included into other (master) xml files. That way one can easily reuse definitions without requiring to apply a
-
Hi everyone, Making an array for a calculator, and am stuck and have 57 similar errors. If someone can check what's wrong and show it to me that'll be great... Thanks PART 1 import javax.swing.*; public class TestMySwing{ * @param args publ
-
New PC lost origional install disk and unable to install using download
I got a new PC and have lost my install disk forr my Tunston T2. I tried to install using the download of version 4.1.4 from the website but no luck. When I try to open the program I get an error message "Palm Desktop Application has encurred a pro
-
Data takeover (Data Migration )of FI/CO
Hi all, please ,can anyone send me the material or steps to implemented for a FI/CO data migration project . Thanks. srinivas.
-
Exit codes from the Java Virtual machine
Hi, I've been asked to write a C wrapper program on UNIX and Windows which launches java (or java.exe) with the appropriate command line options and jar files to run an application. So far so good. However, I can't find any documentation saying how t