How to produce dynamic cross-tab query result on this data ?
Hi gurus,
I have sales (simplified) sales data as below :
create table sales_summ (area_code varchar2(3), sales_amt number, product varchar2(10) ) ;
insert into sales_summ values ('A01', 100, 'P01');
insert into sales_summ values ('A02', 200, 'P01');
insert into sales_summ values ('B01', 300, 'P02');
insert into sales_summ values ('A01', 400, 'P02');
insert into sales_summ values ('A02', 500, 'P01');
insert into sales_summ values ('A03', 600, 'P01');
insert into sales_summ values ('A01', 700, 'P02');
insert into sales_summ values ('A02', 800, 'P02');
insert into sales_summ values ('A03', 900, 'P01');
And I want to produce a cross-tab sales summary like below :
Product A01 A02 A03 B01
P01 100 700 1500 0
P02 1100 800 0 300
How is the query ?
Thank you for your help,
xtanto
Search this forum for "pivot". Plenty of examples.
Regards,
Rob.
Similar Messages
-
Dear users,
I have a query as below:
select a$acct$company company,
a$period_name "Period Name",
a$acct$location location,
a$acct$prime_sub "Prime Sub",
sum (balance) balance
from apps_rpt.us_gl_balances
where a$acct$prime_sub between '300001' and '313099'
and a$period_name = 'DEC-01'
and a$acct$company in ('5110')
group by a$period_name, a$acct$location, a$acct$company, a$acct$prime_sub
order by a$acct$prime_sub;
Sample output of the above query is:
COMPANY Period Name LOCATION Prime Sub BALANCE
5110 DEC-01 50008 300001 0.00
5110 DEC-01 52424 300001 0.00
5110 DEC-01 52513 300001 0.00
5110 DEC-01 50008 300008 201315.00
5110 DEC-01 50095 300008 10403.17
5110 DEC-01 50107 300008 0.00
5110 DEC-01 50108 300008 -1099236.04
5110 DEC-01 50180 300008 0.00
5110 DEC-01 51628 300008 -6396.02
5110 DEC-01 51734 300008 -5896.51
5110 DEC-01 51735 300008 -8525.78
5110 DEC-01 52423 300008 -7268.64
5110 DEC-01 52424 300008 -6945.65
5110 DEC-01 52428 300008 -7845.70
5110 DEC-01 52513 300008 -11309.44
5110 DEC-01 52514 300008 -10272.08
5110 DEC-01 52515 300008 -3861.72
5110 DEC-01 52516 300008 -6685.85I need to write a cross tab query whose output should be some thing as below:
300001 300008
50008 0 201315.00
52424 0 -6945.65
52513 0 -11309.44
50095 0 10403.17
50107 0 0
50108 0 -1099236.04
50180 0 0
51628 0 -6396.02
51734 0 -5896.51
51735 0 -8525.78
52423 0 -7268.64
52428 0 -7845.70
52514 0 -10272.08
52515 0 -3861.72
52516 0 -6685.85from the above cross tab results 300001 and 300008 are Prime Sub and the amount shown is Balance. The columns to the left side is Location. Company and Period Name remain the same for all the rows.
It would great if some one can assist me in writing a cross tab query to display results as shown above.
Thanks
SandeepFrank,
Thanks for your reply. I Tried your method of Dynamic Pivot, but its not working.
My dynamic_pivot_subscript.sql script is :
SELECT DISTINCT
', MAX(DECODE(PRIME_SUB,'|| PRIME_SUB||',' AS txt1,
'BALANCE,0)) AS '||prime_sub AS txt2
FROM
SELECT a$acct$location AS LOCATION
, a$acct$prime_sub AS PRIME_SUB
, SUM(BALANCE) AS BALANCE
FROM apps_rpt.us_gl_balances
WHERE a$acct$prime_sub BETWEEN '300001' AND '313099'
AND a$period_name = 'DEC-01'
AND a$acct$company IN ('5110')
GROUP BY a$acct$location,a$acct$prime_sub
);Sql Plus session is as follows :
-- Restore SQL*Plus features suppressed earlier
SET FEEDBACK ON
SET PAGESIZE 50
SPOOL p:\sql\cookbook\dynamic_pivot.lst
select LOCATION
@@dynamic_pivot_subscript
from
SELECT a$acct$location AS LOCATION
, a$acct$prime_sub AS PRIME_SUB
, SUM(BALANCE) AS BALANCE
FROM apps_rpt.us_gl_balances
WHERE a$acct$prime_sub BETWEEN '300001' AND '313099'
AND a$period_name = 'DEC-01'
AND a$acct$company IN ('5110')
GROUP BY a$acct$location,a$acct$prime_sub
GROUP BY LOCATION
SPOOL OFFWhen i use sql plus session then its giving me the following error:
@@dynamic_pivot_subscript
ERROR at line 2:
ORA-02019: connection description for remote database not found
(1) The version of Oracle (and any other relevant software) you're using
Ans :
Oracle8i Enterprise Edition Release 8.1.7.4.0 - 64bit Production
With the Partitioning option
JServer Release 8.1.7.4.0 - 64bit Production
I either use Toad or Sql Navigator -
Cross tab query dynamically.
Hi,
i want to do crosstab in my query. but the data was not static its dynamic.
CREATE TABLE ABTEST
SEC_ID NUMBER,
MONYEAR VARCHAR2(10 BYTE),
NUM NUMBER
SET DEFINE OFF;
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200802', 13);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200803', 25);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200804', 26);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200805', 13);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200806', 10);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200807', 11);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200808', 5);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200809', 14);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200810', 17);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200811', 12);
Insert into ABTEST
WHERE
(SEC_ID, MONYEAR, NUM)
Values
(91, '200812', 12);
COMMIT;
Currently it looks like below.
SEC_ID MONYEAR NUM
91 200802 13
91 200803 25
91 200804 26
91 200805 13
91 200806 10
91 200807 11
91 200808 5
91 200809 14
91 200810 17
91 200811 12
91 200812 12
I want to display the data like below.
sec_id 200802 ........200812
91 13 12is there any can we do that by sql query or procedure. The dates are not limited its dynamic.
Please help meout.
Thanks & Regards,
Venkat.Hi,
The number of columns has to be hard-coded.
If you don't know the number of columns until run-time, then you have to use dynamic SQL where, at run-time, you figure out how many columns are needed and write that many columns.
See the script below for one way of doing that in SQL*Plus.
How to Pivot a Table with a Dynamic Number of Columns
This works in any version of Oracle
The "SELECT ... PIVOT" feature introduced in Oracle 11
is much better for producing XML output.
Say you want to make a cross-tab output of
the scott.emp table.
Each row will represent a department.
There will be a separate column for each job.
Each cell will contain the number of employees in
a specific department having a specific job.
The exact same solution must work with any number
of departments and columns.
(Within reason: there's no guarantee this will work if you
want 2000 columns.)
Case 0 "Basic Pivot" shows how you might hard-code three
job types, which is exactly what you DON'T want to do.
Case 1 "Dynamic Pivot" shows how get the right results
dynamically, using SQL*Plus.
(This can be easily adapted to PL/SQL or other tools.)
PROMPT ========== 0. Basic 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
ORDER BY deptno
PROMPT ========== 1. Dynamic Pivot ==========
-- ***** Start of dynamic_pivot.sql *****
-- Suppress SQL*Plus features that interfere with raw output
SET FEEDBACK OFF
SET PAGESIZE 0
SPOOL p:\sql\cookbook\dynamic_pivot_subscript.sql
SELECT DISTINCT
', COUNT (CASE WHEN job = '''
|| job
|| ''' ' AS txt1
, 'THEN 1 END) AS '
|| job
|| '_CNT' AS txt2
FROM scott.emp
ORDER BY txt1;
SPOOL OFF
-- Restore SQL*Plus features suppressed earlier
SET FEEDBACK ON
SET PAGESIZE 50
SPOOL p:\sql\cookbook\dynamic_pivot.lst
SELECT deptno
@@dynamic_pivot_subscript
FROM scott.emp
GROUP BY deptno
ORDER BY deptno
SPOOL OFF
-- ***** End of dynamic_pivot.sql *****
EXPLANATION:
The basic pivot assumes you know the number of distinct jobs,
and the name of each one. If you do, then writing a pivot query
is simply a matter of writing the correct number of ", COUNT ... AS ..."\
lines, with the name entered in two places on each one. That is easily
done by a preliminary query, which uses SPOOL to write a sub-script
(called dynamic_pivot_subscript.sql in this example).
The main script invokes this sub-script at the proper point.
In practice, .SQL scripts usually contain one or more complete
statements, but there's nothing that says they have to.
This one contains just a fragment from the middle of a SELECT statement.
Before creating the sub-script, turn off SQL*Plus features that are
designed to help humans read the output (such as headings and
feedback messages like "7 rows selected.", since we do not want these
to appear in the sub-script.
Turn these features on again before running the main query.
*/ -
How to design customize Cross tab in Crystal 9
hi guys,
i'm having a big problem in designing this format in cross tab, coz it causes a lot of space that i wouldnt need in displaying the report in my application here's i want to show on my report,
this is just a sample data, but this is supposed the designed, or the output in crystal cross tab
SM gl orieta buyer and soon.....
sales TX PX sales TX PX sales TX PX
Mar-1 Mon 1000 1 1 200 2 3 400 4 5
Mar-2 Tue 200 10 11 300 4 8 220 5 8
Mar-31 Sun
Running Total 1200 500 620
Discount 200 300 400
Net sales 2000 3000 5000
Quota 100000 1200000 250000
TX 11 6 9
PX 12 11 13
the upper is the name of the stores which supposed to be dynamic, each store have 3 column which are the sales,TX and PX, on the left side is date which is my parameter is filtering sales by date, under the last date of the month is the running total followed by discount, net sales and soon.. Now my problem is how i'm going to design it in cross tab format in crystal report 9..any suggestion or other way to do it...Please re-post if this is still an issue
-
How to use bind variables in "query result" "validation rule"
I have created a validation rule on my entity object. rules=compareValidator attribute=InvoiceNumber operator=NotEquals queryResult=SELECT ...
In this query, I would like to reference entity attributes from the current instance of the entity when the value fires, for example SELECT invoice_number where invoice_id != :invoiceId
How can I reference a bind variable inside the query result compare validator validation rule?
THanks,
Jerry.bump
-
How to display query result like this
I got a query...gives list of names.
i want to display
11. Peter
12. Jessica
32. Leo
15. Jamie
36. Edward
how i get static number infront of my query result.
static number is not in order.Amm85 wrote:
I got a query...gives list of names.
i want to display
11. Peter
12. Jessica
32. Leo
15. Jamie
36. Edward
how i get static number infront of my query result.
static number is not in order.
You could use the function queryAddColumn to add the numbers as a separate column to the query.
Let us suppose the name of the query is "myQuery". Then you could proceed as follows
<!---Define your "static" list of numbers --->
<cfset staticList = "11,12,32,15,36,13,23,14,33,16,44,31,17,34,18">
<!--- Create an array from the static list. --->
<cfset staticNrArray = ArrayNew(1)>
<cfset staticNrArray = listToArray(staticList)>
<!--- Add the array as a column to the query. --->
<cfset nColumnNumber = QueryAddColumn(myQuery, "staticNr", "integer", staticNrArray)>
<!--- Verify whether it works as expected--->
<cfdump var="#myQuery#">
The list I have given here has 15 numbers. This will naturally instruct ColdFusion to add a new column named staticNr, having 15 rows. You might like to know that the queryAddColumn function enables padding.
Here is how it works. Suppose that the query originally had only 8 rows before you added the column(of 15 rows). Then, in the new query, ColdFusion would pad the last 7 rows of the other columns with null values. If the original query had 20 rows instead, then Coldfusion would pad the last 5 rows corresponding to the staticNr-column with null values. -
How create SQL for Cross tab in template
Hi,
I have been starting to build data templates using SQL to select the data I require. I noticed in the word template-building tool there is an option to create cross tab table. I have tried looking at the standard documentation. I have been unable to work out how I should structure the SQL in my data template to allow me to create a cross tab table in the template?
Thanks,
MarkPost Author: SunilKanta
CA Forum: General
Yes, I had done exactly the same but when tried to display the subreport in new page, some portion of the cross tab is displayed. The page width is not increased proportionately.
i have passed that sub report in footer of main report.
But when i overlap this sub report with other cross tabs already present in the main report i can see full width view of sub report. -
How to estimate the size of query results
i have 3 tables
table_h : 10,000 records and 50columns
table_l : 13,000 records and 75 columns
table_ld : 25,000 records and 120 columns
i wrote a query to fetch data based on the 3 tables, and fetching 25,00 records.
ie ( 5 coulms from table_h, 12 coulmns from table_l and 120 columns from table_ld)
how i have to get the size of this query results.What "size" are we talking about here? There are many different ways to interpret that-- it could be the size of the memory buffer that would be allocated on the client. It could be the size that would be required to store the result in a table in the database. It could be something else. But the answer may differ substantially.
Regardless, it's probably going to be something like (number of rows) * (sum of sizes of each column). But the size required for each column is a bit variable
- Client applications, in general, are going to allocate a memory buffer based on the maximum size of the column (LOB and LONG columns excepted). So if you declare a VARCHAR2(4000) but only store 1 byte in that column, most client applications are going to allocate 4k for that column for each row. It might be even worse than that-- if the client application happens to treat character data as Unicode, it probably uses the UTF-16 encoding in memory, which means allocating 2 bytes for every byte in the database (assuming the database character set supports single-byte characters primarily).
- The Oracle database, on the other hand, is not going to allocate more disk than is actually required to encode variable width data. So the 1 byte column only requires 1 byte of storage (plus a length). That's great for storage efficiency, but that makes computing the actual size a bit more complicated because you now need to know the actual size of data stored in each row (or at least the statistical distribution) rather than the table definition.
Justin -
Dynamic Sampling of query results
Is there a way to implement dynamic sampling of expected result set in a way that we could invoke as a percentage of expected
results?
We are using Web Intelligence XIR3SP2.I have tested Oracle only. Teradata has a "sample" command and I would hope that Web Intelligence would use that. The Oracle method involves the dbms_random package to assign each row a random number, then the query is sorted by that number and a limit is placed for the sample size.
I have not written about this on my blog yet, but it could be an interesting topic. Thanks for the suggestion. -
SQL query result with HTML Data in output
Hello,
I have a SQL table , in one column I store HTML data. I need to query the table and get the HTML data in the columns that have 'HREF'. The output shows as grid on the sql management studio, however when I export it to excel, the HTML data does not get copied
correctly, since there are HTML tags etc.
How can I export the report correctly from SQL ?Hello,
The HTML data is stored in a column with datatype as nvarchar(max). Sample data in the column is shown below. It is with formatting etc and is rendered as is on the web page. the business wants to generate a quick report so that they can see the pages that
have links displayed. I can do that by querying the columns that have a 'HREF' in the text.
Can I get the exact HREF values using just sql query? There can be more than one links on a page.
Also, If I just want to copy the whole column and paste it on excel, how can I do that? If I copy the data below and paste, it does not get copied in one cell.. it spreads across multiple cells, so the report does not make any sense.
<br />
<table border="0" cellpadding="0" cellspacing="0" style="width: 431pt; border-collapse: collapse;" width="574">
<tbody>
<tr height="19" style="height: 14.25pt; ">
<td height="19" style="border: 0px blue; width: 431pt; height: 14.25pt; background-color: transparent;" width="574"><a href="https:"><u><font color="#0066cc" face="Calibri">ax </font></u></a></td>
</tr>
</tbody>
<colgroup>
<col style="width: 431pt; " width="574" />
</colgroup>
</table> -
Cross-tab with a lot of data causes fault in crpe32.dll when exporting to Excel
Hello. I believe I found a bug in the Service Pack 9. After upgrading my production to runtime 9, I found that cross-tab reports with large datasets crashed the website. Per the Windows Application Log, a fault happened in the crpe32.dll. So I uninstalled the runtimes for 9 and re-installed 8 and the reports worked again. My production uses the 64 bit runtimes.
I created a test site which includes a two datasets one large and one small with two reports, one cross-tab and another non-cross-tab. The site has four pages, one to open the cross-tab report using the large dataset, another with the same cross-tab report using the small dataset, another for the non-cross-tab report using the large dataset and the last with the non-cross-tab report using the small dataset. Only the cross-tab report using the large dataset causes the fault. I’ve attached the test site for you use as needed.Hi Thomas,
What version of Visual Studio are you using?
What OS are you using?
What version of IIS?
I did a quick test using a simplied one liner WEB app and I see the same thing...
FYI, changes we've done is IIS recommends, actually it's a must, to load the report and viewer in the Page_Init section and not the Page_Load so you initialize all of the required dependencies, Page Load doesn't handle it properly...
I tried both, doesn't make any difference...
By the way your "large dataset" is not that big. I've seen people use a 10 meg xml files.
Try moving all of your code to the Page_Init section and test again to confirm...
I don't believe it has anything to do with the amount data directly, I believe it's the number of pages to render for a crosstab report.
I'll escalate this to DEV - ADAPT01726274
Set for SP 10 ( which means I set it to be fix in SP 10 but it does not mean it will be. All depends on DEV's work load )
Thanks
Don -
How to create dynamic bucket in Query Designer
Hi ,
Can anyone suggest me how to fill the data dynamically in bucket calculation in query without using program.?
Eg:
No age
1 5
2 7
Bucket:
0-5 6-10 11-15
5 7
In future if i get data more than 7 in age(like 10 in age column) it should dynamically create bucket with range 5 and fill it over(11-15 nbucket)
No age
1 5
2 7
3 10
0-5 6-10 11-15
5 7
Regards,
MohammeDHi,
As per my knowledge can you try by using of offset to get this requirement.
Regards
sivaraju -
how do you do the equivilent in a webi report?
I dont think case statements are supported
e.g.
Code:
SELECT
SUM(CASE WHEN purchase_date BETWEEN '2004-08-01' and '2004-08-31' THEN amount ELSE 0 END) As m2004_08,
SUM(CASE WHEN purchase_date BETWEEN '2004-09-01' and '2004-09-30' THEN amount ELSE 0 END) As m2004_09,
SUM(CASE WHEN purchase_date BETWEEN '2004-10-01' and '2004-10-31' THEN amount ELSE 0 END) As m2004_10
FROM purchasesyour split logic sounds like a great idea Dave thanks
I am trying to produce this statement:
SUM(case when mks_clienttype ='C' and mks_reason4fail in ("",'pri record found') then clients end) as Client_email
variable clientTypeFlag
code =If([Mks Clienttype] = "C") Then 1 Else 0
variable reason4failFlag1
code =If([Mks Reason4fail(trim)] = "pri record found" Or [Mks Reason4fail(trim)] = "") Then 1 Else 0
variable clientEmail
code ?
how to actually get the sum of both clientTypeFlag and reason4failFlag1? I am trying to figure it out with no luck I have tried this:
variable: ClientEmail1
code: =Sum([clientTypeFlag]) Where ([clientTypeFlag] = 1)
variable: ClientEmail2
code: =Sum([clientTypeFlag]) Where ([reason4failFlag1] = 1)
variable: clientEmail
code: =[ClientEmail1] + [ClientEmail2]
want the end result to look like this:
Name | clientemail
john | 5
bob | 3
jo | 2
Edited by: Dennis Alishah on Sep 21, 2010 5:58 AM -
Hi all
I need an help
SELECT * FROM TABLE_A
ID SCHEDULED MARK
1 01/18/2011 T01
2 01/18/2011 T02
3 01/18/2011 T03
4 01/19/2011 T04
5 01/20/2011 T05 I want Results like below for folllowing query
SELECT SCHEDULED,MARK FROM TABLE_A
WHERE SCHEDULED BETWEEN TO_CHAR(SYSDATE,'MM/DD/YYYY') AND TO_CHAR(SYSDATE +1,'MM/DD/YYYY')
1/18/2010 1/19/2011
T01 T04
T02
T03
CREATE TABLE "TABLE_A"
( "ID" NUMBER,
"SCHEDULED" DATE,
"MARK" VARCHAR2(50)
insert into table_a values(1, '01/18/2011', 'T01');
insert into table_a values( 2, ' 01/18/2011', 'T02');
insert into table_a values( 3, ' 01/18/2011', 'T03');
insert into table_a values( 4, ' 01/19/2011', 'T04');
commit;Thanks for your helpuser1849 wrote:
Hi all
I need an help
SELECT * FROM TABLE_A
ID SCHEDULED MARK
1 01/18/2011 T01
2 01/18/2011 T02
3 01/18/2011 T03
4 01/19/2011 T04
5 01/20/2011 T05 I want Results like below for folllowing query
SELECT SCHEDULED,MARK FROM TABLE_A
WHERE SCHEDULED BETWEEN TO_CHAR(SYSDATE,'MM/DD/YYYY') AND TO_CHAR(SYSDATE +1,'MM/DD/YYYY')
1/18/2010 1/19/2011
T01 T04
T02
T03
CREATE TABLE "TABLE_A"
( "ID" NUMBER,
"SCHEDULED" DATE,
"MARK" VARCHAR2(50)
insert into table_a values(1, '01/18/2011', 'T01');
insert into table_a values( 2, ' 01/18/2011', 'T02');
insert into table_a values( 3, ' 01/18/2011', 'T03');
insert into table_a values( 4, ' 01/19/2011', 'T04');
commit;Thanks for your helphttp://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:766825833740 -
How to forbid "switch to tab" completion results ?
Link completion is very useful feature, except one little thing - some links there have "switch to tab" attribute, which I consider useless and harmful.
There are many site models that do not reflect position in top URI (for example, using POST params, navigating in subframes, etc.), and such links only waste visible completion popup slots.
How can I disable such "switch to tab" links/attributes ?
Ctrl+click works (openning one excess tab), but it's half measure.
Safe mode doesn't stop such behavior, so it's core feature.Greetings, Curtis,
Thanks a lot! That's the exactly what I need.
Maybe you are looking for
-
Hi i have an iMac with 10.9.3 and when i tried to open a PDF link or a PDF document in Safari 7.0.4 this message appears: "Before proceeding you must first launch Adobe Acrobat and accept the End User License Agreement". What can i do to solve thi
-
When starting the computer, I get a grey screen and following status: your startup disk is full. How can I still enter (in order to free-up some space)?
-
The application has notes field where user can type a few hundred chars of data. Some times users do copy from Word and paste into these notes field - This is posting some junk characters. Is there any way to take care of these extra characters? Some
-
Regular Expressions / String Match
Hi Everyone, thankx for reading this I'm programing a oracle form which at one part read's the full path into a file (a text file) and place's it on a VARCHAR2 field. This is the path into the file and not the file data. I would like to match this st
-
In previous releases you could enable the Dashboard developer mode and move widgets off the dashboard onto your desktop. In Lion you still can, but with one difference: The widget only appears on *one* of you desktops, not all of them like it did be