Sql query to generate this output
Hi,
I have a table as follows;
SET_MASTER
ID set_id attribute_id attribute_value
1 1 2 10
2 1 3 20
3 1 4 50
4 1 5 60
5 2 2 10
6 2 3 30
7 2 4 40
8 2 5 50
I want to generate an output as folllows;
Attr_id Set1_attr_value Set2_attr_value min_attr_value max_attr_value
2 10 10 10 10
3 20 30 20 30
4 50 40 40 50
5 60 50 50 60
Please provide the query to generate this output.
Thanks.
SQL> create table set_master (id,set_id,attribute_id,attribute_value)
2 as
3 select 1, 1, 2, 10 from dual union all
4 select 2, 1, 3, 20 from dual union all
5 select 3, 1, 4, 50 from dual union all
6 select 4, 1, 5, 60 from dual union all
7 select 5, 2, 2, 10 from dual union all
8 select 6, 2, 3, 30 from dual union all
9 select 7, 2, 4, 40 from dual union all
10 select 8, 2, 5, 50 from dual
11 /
Tabel is aangemaakt.
SQL> select attribute_id attr_id
2 , max(decode(set_id,1,attribute_value)) set1_attr_value
3 , max(decode(set_id,2,attribute_value)) set2_attr_value
4 , least(max(decode(set_id,1,attribute_value)),max(decode(set_id,2,attribute_value))) min_attr_value
5 , greatest(max(decode(set_id,1,attribute_value)),max(decode(set_id,2,attribute_value))) max_attr_value
6 from set_master
7 group by attribute_id
8 order by attribute_id
9 /
ATTR_ID SET1_ATTR_VALUE SET2_ATTR_VALUE MIN_ATTR_VALUE MAX_ATTR_VALUE
2 10 10 10 10
3 20 30 20 30
4 50 40 40 50
5 60 50 50 60
4 rijen zijn geselecteerd.Regards,
Rob.
Similar Messages
-
SQL query to generate Nested XML
Hello,
I use Oracle 11g R2 SOE....
I have two main tables
COMMERCIALS_PROPERTIES (com_id number PK , com_size number, project_id number, com_type number)
COM_PHOTOS (ID number PK , com_id number FK, content blob, mimetype varchar2)
Please, note the following has nothing to do with my problem:
CONTENT and MIMETYPE columns. Also, the lookup tables: PROJECTS , COM_TYPE
In APEX ( Application Express ) we can expose a report as RESTful web service in XML format:
I am using this query to generate the XML 1 feed, but I need to tweak the query to generate XML 2 feed.
Is it possible, how to do it ???
Select
"COM"."COM_ID" as "COM_ID",
"COM"."COM_SIZE" as "SIZE",
"PROJECTS"."PROJECT_NAME_EN" as "PROJECT",
"COM_TYPES"."COM_TYPE" as "COM_TYPE",
'http://fam-erp.com/apex/erp/fateh/'||IMG.ID as "ImgURL"
FROM
COM_PHOTOS IMG inner join COMMERCIALS_PROPERTIES "COM"
on IMG.COM_ID = COM.COM_ID
inner join "PROJECTS" "PROJECTS"
on "PROJECTS"."PROJECT_ID"="COM"."PROJECT_ID"
inner join "COM_TYPE_LOOKUP" "COM_TYPES"
on "COM_TYPES"."TYPE_ID"="COM"."COM_TYPE"
WHERE
COM.COM_ID < 80 order by 1h1. XML 1
h2. Please look only at <COM_ID> and <ImgURL>
<ROWSET>
<ROW>
<COM_ID>77</COM_ID>
<SIZE>842</SIZE>
<PROJECT>Bayswater Tower</PROJECT>
<COM_TYPE>Office</COM_TYPE>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1410</ImgURL>
</ROW>
<ROW>
<COM_ID>77</COM_ID>
<SIZE>842</SIZE>
<PROJECT>Bayswater Tower</PROJECT>
<COM_TYPE>Office</COM_TYPE>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1412</ImgURL>
</ROW>
<ROW>
<COM_ID>78</COM_ID>
<SIZE>756</SIZE>
<PROJECT>Bayswater Tower</PROJECT>
<COM_TYPE>Office</COM_TYPE>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1425</ImgURL>
</ROW>
<ROW>
<COM_ID>78</COM_ID>
<SIZE>756</SIZE>
<PROJECT>Bayswater Tower</PROJECT>
<COM_TYPE>Office</COM_TYPE>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1429</ImgURL>
</ROW>
</ROWSET>---------------------------
h1. XML 2
h2. Please look only at <COM_ID> and <Images> and <ImgURL>
<ROWSET>
<ROW>
<COM_ID>77</COM_ID>
<SIZE>842</SIZE>
<PROJECT>Bayswater Tower</PROJECT>
<COM_TYPE>Office</COM_TYPE>
<Images>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1410</ImgURL>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1412</ImgURL>
</Images>
</ROW>
<ROW>
<COM_ID>78</COM_ID>
<SIZE>756</SIZE>
<PROJECT>Bayswater Tower</PROJECT>
<COM_TYPE>Office</COM_TYPE>
<Images>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1425</ImgURL>
<ImgURL>http://fam-erp.com/apex/erp/fateh/1429</ImgURL>
</Images>
</ROW>
</ROWSET>Hi, Fateh
One possible way is to use XML functions to create your XML.
Using XML functions you can do the IMAGES as an XMLAGG subquery rather than join to the image table.
Here's an example using SCOTT schema:
SQL> select xmlelement(
2 "ROWSET"
3 , xmlagg(
4 xmlelement(
5 "ROW"
6 , xmlforest(
7 d.deptno as "ID"
8 , d.dname as "NAME"
9 , (
10 select xmlagg(
11 xmlelement(
12 "ImgUrl"
13 , 'http://the.server.com/'||e.empno
14 )
15 order by e.empno
16 )
17 from scott.emp e
18 where e.deptno = d.deptno
19 ) as "Images"
20 )
21 )
22 order by d.deptno
23 )
24 ) the_xml
25 from scott.dept d
26 /* joins to the other tables EXCEPT image table */
27 /
THE_XML
<ROWSET><ROW><ID>10</ID><NAME>ACCOUNTING</NAME><Images><ImgUrl>http://the.serverThat output is an XMLTYPE column (think of it as a CLOB with added functionality ;-) )
My SQL*PLUS cuts the output, but believe me, it is all there.
Just to show it, here's the same example wrapped in an XMLSERIALIZE function to pretty-print the XML:
SQL> select xmlserialize(
2 content
3 xmlelement(
4 "ROWSET"
5 , xmlagg(
6 xmlelement(
7 "ROW"
8 , xmlforest(
9 d.deptno as "ID"
10 , d.dname as "NAME"
11 , (
12 select xmlagg(
13 xmlelement(
14 "ImgUrl"
15 , 'http://the.server.com/'||e.empno
16 )
17 order by e.empno
18 )
19 from scott.emp e
20 where e.deptno = d.deptno
21 ) as "Images"
22 )
23 )
24 order by d.deptno
25 )
26 )
27 as varchar2(4000)
28 indent size=2
29 ) the_xml
30 from scott.dept d
31 /* joins to the other tables EXCEPT image table */
32 /
THE_XML
<ROWSET>
<ROW>
<ID>10</ID>
<NAME>ACCOUNTING</NAME>
<Images>
<ImgUrl>http://the.server.com/7782</ImgUrl>
<ImgUrl>http://the.server.com/7839</ImgUrl>
<ImgUrl>http://the.server.com/7934</ImgUrl>
</Images>
</ROW>
<ROW>
<ID>20</ID>
<NAME>RESEARCH</NAME>
<Images>
<ImgUrl>http://the.server.com/7369</ImgUrl>
<ImgUrl>http://the.server.com/7566</ImgUrl>
<ImgUrl>http://the.server.com/7788</ImgUrl>
<ImgUrl>http://the.server.com/7876</ImgUrl>
<ImgUrl>http://the.server.com/7902</ImgUrl>
</Images>
</ROW>
<ROW>
<ID>30</ID>
<NAME>SALES</NAME>
<Images>
<ImgUrl>http://the.server.com/7499</ImgUrl>
<ImgUrl>http://the.server.com/7521</ImgUrl>
<ImgUrl>http://the.server.com/7654</ImgUrl>
<ImgUrl>http://the.server.com/7698</ImgUrl>
<ImgUrl>http://the.server.com/7844</ImgUrl>
<ImgUrl>http://the.server.com/7900</ImgUrl>
</Images>
</ROW>
<ROW>
<ID>40</ID>
<NAME>OPERATIONS</NAME>
</ROW>
</ROWSET>For a webservice you do not need to pretty-print the XML that is returned by the webservice.
I do not know APEX, so I do not know if APEX supports exposing an allready built piece of XML rather than exposing a query result.
But my guess is that it should do it very nicely if you query an XMLTYPE datatype (that is - use the first of my examples rather than the pretty-printed one.)
If you can't get APEX to do it this way, then I suggest you try asking in the APEX forum rather than the SQL forum ;-) -
SQL Query Help - Is this possible or impossible????
Hi guys,
I need help with an SQL query that I'm trying to develop. It's very easy to explain but when trying to implement it, I'm struggling to achieve the results that I want.....
For example,
I have 2 tables
The first table is:
1) COMPANY create table company (manufacturer varchar2(25),
date_established date,
location varchar2(25) );My sample test date is:
insert into company values ('Ford', 1902, 'USA');
insert into company values ('BMW', 1910, 'Germany');
insert into company values ('Tata', 1922, 'India');The second table is:
2) MODELS create table models (manufacturer varchar(25),
model varchar2(25),
price number(10),
year date,
current_production_status varchar2(1) ) ;My sample test data is:
insert into models values ('Ford', 'Mondeo', 10000, 2010, 0);
insert into models values ('Ford', 'Galaxy', 12000, 2008, 0);
insert into models values ('Ford', 'Escort', 10000, 1992, 1);
insert into models values ('BMW', '318', 17500, 2010, 0);
insert into models values ('BMW', '535d', 32000, 2006, 0);
insert into models values ('BMW', 'Z4', 10000, 1992, 0);
insert into models values ('Tata', 'Safari', 4000, 1999, 0);
insert into models values ('Tata', 'Sumo', 5500, 1996, 1);
insert into models values ('Tata', 'Maruti', 3500, 1998, 0);And this is my query:
SELECT
com.manufacturer,
com.date_established,
com.location,
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
mod.current_production_status
FROM
company com,
models mod
WHERE
mod.manufacturer = com.manufacturer
and com.manufacturer IN ('Ford', 'BMW', 'Tata')
and mod.current_production_status IN (1,0)
ORDER BY
mod.current_production_status DESCWhat I want the query to output is this:
com.manufacturer com.date_established com.location mod.model mod.price mod.year mod.current_production_status
Ford 1902 USA Escort 10000 1992 1
BMW 1910 Germany - - - 0
Tata 1922 India Sumo 5500 1998 1If current_production_status is 1 it means this particular model has been discontinued
If current_production_status is 0 it means the manufacturer does not have any discontinued models and all are in procuction.
The rule is only one record per manufacturer is allowed to have a current_production_status of 1 (so only one model from the selection the manufactuer offers is allowed to be discontinued).
So the query should output the one row where current_production_status is 1 for each manufacturer.
If for a given manufacturer there are no discontinued models and all have a current_production_status of 0 then ouput a SINGLE row that only includes the data from the COMPANY table (as above). The rest of the columns from the MODELS table should be populated with a '-' (hyphen).
My query as it is above will output all the records where current status is 1 or 0 like this
com.manufacturer com.date_established com.location mod.model mod.price mod.year mod.current_production_status
Ford 1902 USA Escort 10000 1992 1
Tata 1922 India Sumo 5500 1998 1
Ford 1902 USA - - - 0
Ford 1902 USA - - - 0
BMW 1910 Germany - - - 0
BMW 1910 Germany - - - 0
BMW 1910 Germany - - - 0
Tata 1922 India - - - 0
Tata 1922 India - - - 0However this is not what I want.
Any ideas how I can achieve the result I need?
Thanks!
P.S. Database version is '10.2.0.1.0'Hi Vishnu,
Karthiks query helped...
But this is the problem I am facing...
SELECT
com.manufacturer,
com.date_established,
com.location,
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
mod.current_production_status
FROM
company com,
models mod
WHERE
mod.manufacturer = com.manufacturer
and com.manufacturer = 'Ford'
and mod.current_production_status IN (1,0)
ORDER BY
mod.current_production_status DESCThe value of:
and com.manufacturer = 'Ford'will be dependent on front end user input....
When I run the query above I get all the rows where current_production_status is either 1 or 0.
I only require the rows where current_production_status is 1.
So if I amend it to look like this:
and mod.current_production_status = 1This works....
BUT if a user now passes in more than one manufacturer EG:
and com.manufacturer IN ('Ford', 'BMW')The query will only return the one row for Ford where current_production_status is 1. However because BMW has no models where current_production_status is 1 (all 3 are 0), I still want this to be output - as one row....
So like this:
com.manufacturer com.date_established com.location mod.model mod.price mod.year mod.current_production_status
Ford 1902 USA Escort 10000 1992 1
BMW 1910 Germany - - - 0So (hopefully you understand), I want both cases to be catered for.....whether a user enters one manufacturer or more than one...
Thanks you so much!
This is really driving me insane :-( -
How to generate this output?URGENT
how to generate using DOM or SAX to get this output?
<project>
<process name = " Process1 " >
</project>
i am stuck here ... may i know how to continued coding from here.. must add in wat ? can any one tell me thank....
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import org.w3c.dom.DOMException;
import java.io.*;
import java.util.*;
public class TestVector
private Vector m_vProcess;
String strProjectName;
public static void main(String[] args)
TestVector pThis = new TestVector();
pThis -> WriteToXML(m_vProcess);
public void TestVector {
strProjectName ="Project1";
m_vProcess = new Vector();
m_vProcess.add("Process1");
m_vProcess.add("Process2");
public void WriteToXML(Vector vProcess)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = facory.newDocumentBuilder();
Document builder = builder.parse("Project.xml");
Node rootNode = document.getDocumentElement();
NodeList list = document.getElementsByTagName("Project");
// Loop through the list.
for (int i=0; i < list.getLength(); i++) {
thisProjectNode = list.item(i);
Node thisName1Node = thisProjectNode.getFirstChild();
if (thisName1Node == null) continue;
if (thisName1Node.getFirstChild() == null) continue;
if (! thisName1Node.getFirstChild() instanceof
org.w3c.dom.Text) continue;
String data = thisName1Node.getFirstChild().getNodeValue();
if (! data.equals("Process1")) continue;
//We're at the Mocha Java node. Create and insert the new
//element.
Node newCoffeeNode = document.createElement("Project");
Node newName1Node = document.createElement("Process");
Text tnNode = document.createTextNode("Process1");
newName1Node.appendChild(tnNode);
Node newName2Node = document.createElement("Process");
Text tpNode = document.createTextNode("Process2");
newName2Node.appendChild(tpNode);
newProjectNode.appendChild(newName1Node);
newProjectNode.appendChild(newName2Node);
rootNode.insertBefore(newProjectNode, thisProjectNode);
break;i am not good in programming..wat ever i read b4 ,i wlll forget easily ..and y the output nv come out after i type these...
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import org.w3c.dom.DOMException;
import java.io.*;
import java.util.*;
public class TestVector
private Vector m_vProcess;
String strProjectName;
public static void main(String[] args)
TestVector pThis = new TestVector();
pThis -> WriteToXML(m_vProcess);
public void TestVector {
strProjectName ="Project1";
m_vProcess = new Vector();
m_vProcess.add("Process1");
m_vProcess.add("Process2");
public void WriteToXML(Vector vProcess)
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = facory.newDocumentBuilder();
Document builder = builder.parse("Project.xml");
TransformerFactory tfac = TransformerFactory.newInstance();
FileWriter fileWtr = new FileWriter("output.xml");
StreamResult strResult = new StreamResult(fileWtr);
Transformer trans = tfac.newTransformer();
trans.transform(new DOMSource(document.getDocumentElement()),strResult);
Node rootNode = document.getDocumentElement();
NodeList list = document.getElementsByTagName("Project");
// Loop through the list.
for (int i=0; i < list.getLength(); i++) {
thisProjectNode = list.item(i);
Node thisName1Node = thisProjectNode.getFirstChild();
if (thisName1Node == null) continue;
if (thisName1Node.getFirstChild() == null) continue;
if (! thisName1Node.getFirstChild() instanceof
org.w3c.dom.Text) continue;
String data = thisName1Node.getFirstChild().getNodeValue();
if (! data.equals("Process1")) continue;
Node newCoffeeNode = document.createElement("Project");
Node newName1Node = document.createElement("Process");
Text tnNode = document.createTextNode("Process1");
newName1Node.appendChild(tnNode);
Node newName2Node = document.createElement("Process");
Text tpNode = document.createTextNode("Process2");
newName2Node.appendChild(tpNode);
newProjectNode.appendChild(newName1Node);
newProjectNode.appendChild(newName2Node);
rootNode.insertBefore(newProjectNode, thisProjectNode);
break; -
Need generic dynamic sql query to generate nodes depending on dealer levels
Input table:
create table #test(dealerid integer ,dealerlvl integer)
insert into #test values(1,1)
insert into #test values(1,2)
insert into #test values(1,3)
insert into #test values(1,4)
insert into #test values(2,1)
insert into #test values(2,2)
insert into #test values(2,3)
insert into #test values(2,4)
insert into #test values(2,5)
insert into #test values(2,6)
go
create table #test2(dealerid integer,node integer,prntnode integer,dealerlvl integer)
insert into #test2 values (1,234,124,2)
insert into #test2 values (1,123,234,1)
insert into #test2 values (1,238,123,2)
insert into #test2 values (1,235,238,3)
insert into #test2 values (1,253,235,4)
insert into #test2 values (2,21674,124,3)
insert into #test2 values (2,1233,21674,1)
insert into #test2 values (2,2144,1233,2)
insert into #test2 values (2,2354,2144,3)
insert into #test2 values (2,24353,2354,4)
insert into #test2 values (2,245213,24353,5)
insert into #test2 values (2,2213,245213,6)
Expected result :
I have two test case here with dealerID1 and dealerID 2
Result for DealerID1
Result needed for DealerID2:
the levels for dealers might change (Dealer1 has 4 levels, and Dealer 2 has 6 levels) so i need to create an dynamic sql query which lists each node as separate columns depending on the levels.
I have hacked the query to give the result I need
select a.dealerid,a.node as Lvl1,b.node as lvl2,c.node as lvl3,d.node as lvl4
from #test2 a
join #test2 b on a.node=b.prntnode
join #test2 c on b.node=c.prntnode
join #test2 d on c.node=d.prntnode
where a.dealerid=1 and a.dealerlvl=2
select a.dealerid,a.node asLvl1,
b.node as lvl2,c.node as lvl3,d.node as lvl4,e.node as lvl5,f.node as lvl6--,a.dealerlvl,a.dealerid
from #test2 a
join #test2 b on a.node=b.prntnode
join #test2 c on b.node=c.prntnode
join #test2 d on c.node=d.prntnode
join #test2 e on d.node=e.prntnode
join #test2 f on e.node=f.prntnode
where a.dealerid=2 and a.dealerlvl=3
I am sure there is a better way to do this with dynamic sql. please help.
Thanks-- Dynamic PIVOT
DECLARE @T AS TABLE(y INT NOT NULL PRIMARY KEY);
DECLARE
@cols AS NVARCHAR(MAX),
@y AS INT,
@sql AS NVARCHAR(MAX);
-- Construct the column list for the IN clause
SET @cols = STUFF(
(SELECT N',' + QUOTENAME(y) AS [text()]
FROM (SELECT DISTINCT dealerlvl AS y FROM dbo.test2) AS Y
ORDER BY y
FOR XML PATH('')),
1, 1, N'');
-- Construct the full T-SQL statement
-- and execute dynamically
SET @sql = N'SELECT *
FROM (SELECT dealerid, dealerlvl, node
FROM dbo.Test2) AS D
PIVOT(MAX(node) FOR dealerlvl IN(' + @cols + N')) AS P;';
EXEC sp_executesql @sql;
GO -
Hi all,
Query to achieve the below output:
source table :
empno,_deptno_
110,10
110,20
110,30
120,10
130,20
130,30
130,30
110,40
I need the output in below format:
empno,deptno
110,10$20$30$40
120,10
130,20$30$30or:
SQL> -- generating sample data:
SQL> with t as (
2 select 110 empno,10 deptno from dual union
3 select 110,20 from dual union
4 select 110,30 from dual union
5 select 120,10 from dual union
6 select 130,20 from dual union
7 select 130,30 from dual union
8 select 130,30 from dual union
9 select 110,40 from dual
10 )
11 --
12 -- actual query:
13 --
14 select empno
15 , replace(sys_connect_by_path(deptno, ','),',') deptno
16 from ( select empno
17 , deptno
18 , row_number() over (partition by empno order by deptno) rn
19 from t
20 )
21 where connect_by_isleaf=1
22 start with rn=1
23 connect by empno=prior empno
24 and rn = prior rn+1;
EMPNO DEPTNO
110 10203040
120 10
130 2030
3 rows selected. -
Convert Oracle SQL query to single column output
Hello All,
I need to build the query to have multiple columns in a single column with multiple rows.
select a.customer_trx_id,a.previous_customer_trx_id
from ra_customer_trx_all a
where a.customer_trx_id = :customer_trx_id
here, a.customer_trx_id and a.previous_customer_trx_id are in two columns. I need to bring them into a single column.
Say: the above output is
a.customer_trx_id a.previous_customer_trx_id
123456 87654
Need to have single column
As
123456
87654
Please do the needful.
Thanks,
AbdulHi,
Post your question in [SQL and PL/SQL|http://forums.oracle.com/forums/forum.jspa?forumID=75] forum, you would probably get a better/faster response.
Regards,
Hussein -
What will be the query to generate this reports ?
Dear Exparts,
Here is my banner
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
"CORE 10.2.0.3.0 Production"
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - ProductionHere is the sample data
TId TDate TName TCol1 TCol2 TCol3 TCol4 TCol55 TCol56 TCol N
1 01-aug-2012 Abc Ab Bc aa bb TT
2 05-aug-2012 abc A A aa bb TT
3 20-aug-2012 bcd AA AA BB bb C D TTI want to produce the following report
Revision Date Revision Field Previous Value Revised Value
05-aug-2012 TCol1 Ab A
TCol2 Bc A
20-aug-2012 TName Abc Bcd
TCol1 A AA
TCol2 A AA
TCol3 Aa BB
TCol55 C
TCol56 DHow can i do this... ?
Here is the script for create table
CREATE TABLE TB_TEST
TID NUMBER(5)
, TDATE DATE
, TNAME VARCHAR2(20)
, TCOL1 VARCHAR2(20)
, TCOL2 VARCHAR2(20)
, TCOL3 VARCHAR2(20)
, TCOL4 VARCHAR2(20)
, TCOL55 VARCHAR2(20)
, TCOL56 VARCHAR2(20)
, TCOLN VARCHAR2(20)
);Script for sample data
INSERT INTO TB_TEST (TID,TDATE,TNAME,TCOL1,TCOL2,TCOL3,TCOL4,TCOLN)
VALUES (1,TO_DATE('01-AUG-2012','DD-MON-RRRR'),'ABC','AB','BC','AA','BB','TT');
INSERT INTO TB_TEST (TID,TDATE,TNAME,TCOL1,TCOL2,TCOL3,TCOL4,TCOLN)
VALUES (2,TO_DATE('05-AUG-2012','DD-MON-RRRR'),'ABC','A','A','AA','BB','TT');
INSERT INTO TB_TEST (TID,TDATE,TNAME,TCOL1,TCOL2,TCOL3,TCOL4,TCOL55,TCOL56,TCOLN)
VALUES (3,TO_DATE('20-AUG-2012','DD-MON-RRRR'),'BCD','AA','AA','BB','BB','C','D','TT');Thanks..
Edited by: Asked to Learn on Sep 9, 2012 6:57 PMHi,
Asked to Learn wrote:
Dear Exparts,
Here is my banner ...Thanks; that's helpful.
Here is the sample data
Id Date Name Col1 Col2 Col3 Col4 Col55 Col56 Col N
1 01-aug-2012 Abc Ab Bc aa bb TT
2 05-aug-2012 abc A A aa bb TT
3 20-aug-2012 bcd AA AA BB bb C D TT
That's not so helpful. Whenever you have a question, post CREATE TABLE and INSERT statements for the sample data. See {message:id=9360002}
I want to produce the following report
Revision Date Revision Field Previous Value Revised Value
05-aug-2012 Col1 Ab A
Col2 Bc A
20-aug-2012 Name Abc Bcd
Col1 A AA
Col2 A AA
Col3 Aa BB
Col55 C
Col56 DHow can i do this... ?First, Unpivot the data so that, instead on having 8 columns of comparison data on 1 row, you have 1 column on 8 rows. Since you're using Oracle 10.2, you can't use the SELECT ... UNPIVOT feature, but there are other ways to unpivot. See {message:id=10562807} for a technique that works in Oracle 9.1 or higher.
Then, use the anlytic LAG function to compare each vlaue to its previous value. You can do the same sort of thing with the revision_date column, to display NULL when it's the same as the previous row.
If you'd like help, post CREATE TABLE and INSERT statmements for the sample data, and your best attempt. Simplify the problem at first. Just unpivot name and col1: forget about col2 through coln for now. Once you have a solution for 2 columns, modifying it for 8 (or 9, or 89) columns will be easy. When you can unpivot the 2 columns, see if you can use LAG to compare them. -
How to write the SQL query for generating triangular numbers
Hi,
I have a table ..which stores the sequence number like this
Seq :
1000
1200
1300
1500
1800
1900
Now i want to get a result like this
1000 1000
1200 2200
1300 3500
1500 5000
1800 6800
1900 8700
how can it be achieved. I tried using Lead and lag. but only I can add the n+1 or n-1 results. Please help.I've never heard it called 'triangular numbers' before but I think you're looking for a 'running total':
SQL> WITH data AS
2 (
3 SELECT 1000 AS num FROM dual UNION ALL
4 SELECT 1200 AS num FROM dual UNION ALL
5 SELECT 1300 AS num FROM dual UNION ALL
6 SELECT 1500 AS num FROM dual UNION ALL
7 SELECT 1800 AS num FROM dual UNION ALL
8 SELECT 1900 AS num FROM dual
9 )
10 /* END SAMPLE DATA */
11 SELECT num, SUM(num) OVER (ORDER BY num) AS running_total
12 FROM data
13 ORDER BY 1
14 ;
NUM RUNNING_TOTAL
1000 1000
1200 2200
1300 3500
1500 5000
1800 6800
1900 8700
6 rows selected. -
Sql query to display this result
AccountNo
Name
Total Credit
Total Debit
Balance
100010
NEEM
5000
4000
1000
100011
Kamran
50000
49000
1000
100012
Asim
3000
2000
1000Please follow basic Netiquette and post the DDL we need to answer this. Do you know how to follow industry and ANSI/ISO standards. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data.
Avoid dialect in favor of ANSI/ISO Standard SQL. We need to know the data types, keys and constraints on the table.
Why did you think anyone could do anything with what you posted???
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Need help in framing an SQL query - Sample data and output required is mentioned.
Sample data :
ID Region State
1 a A1
2 b A1
3 c B1
4 d B1
Result should be :
State Region1 Region2
A1 a b
B1 c dcreate table #t (id int, region char(1),state char(2))
insert into #t values (1,'a','a1'),(2,'b','a1'),(3,'c','b1'),(4,'d','b1')
select state,
max(case when region in ('a','c') then region end) region1,
max(case when region in ('b','d') then region end) region2
from #t
group by state
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Send mail weekly with spreadsheet generated from SQL query
I have a requirement which is as follows
1 - Run an SQL query to generate a report (lets say select * from emp;)
2 - Save the report output in a excel (csv/xls/xlsx) and send as a mail attachment.
3 - Schedule this procedure as a weekly job.
#3 Job scheduling is simple and can be done but i am stuck with #1 and #2. How can I go about this?Hello,
the FAQ of this forum space has a whole section about Re: 5. How do I read or write an Excel file?
And as Karthik said: you can search this forum, all of your question where asked and answered many times.
Regards
Marcus -
Query to generate an xml output hierarchically
Hi I need to write a sql query to generate an xml hierarchically for a tree menu. I have one self-joined table that holds menu hierarchy.
Following is a sample data in a table.
MenuID MenuName ParentID href
1 File
12 Open 1 Open.jsp
11 New 1
21 Program 11
61 Program2 11 Program2.jsp
31 Blank 21 Blank.jsp
2 View
22 Printer
28 Printer2 22
30 Printer3 28 test.jsp
23 Doc Doc.jsp
4 Tools Tools.jsp
5 Win 1 Win.jspFollowing is the sample output want to generate:
<?xml version = '1.0' encoding = 'windows-1252'?>
<menus>
<menu name="File" id="1">
<menu name="Open" id="12" href="Open.jsp"/>
<menu name="New" id="11">
<menu name="Program" id="21">
<menu name="Blank" id="31" href="Blank.jsp"/>
</menu>
<menu name="Program2" id="61" href="Program2.jsp"/>
</menu>
<menu name="Win" id="5" href="Win.jsp"/>
</menu>
<menu name="View" id="2">
<menu name="Printer" id="22">
<menu name="Printer2" id="28">
<menu name="Printer3" id="30" href="test.jsp">
</menu>
</menu>
</menu>
<menu name="Doc" id="3" href="Doc.jsp"/>
<menu name="Tools" id="4" href="Tools.jsp"/>
</menus>ThanksYou can use SYS_CONNECT_BY_PATH to get the complete hierarchical path to an object, and then order by that. Here is an example:
with menutbl as
(select 1 as MenuId,'File' as MenuName, null as ParentId, null as href from dual
union all
select 12,'Open',1,'Open.jsp' from dual
union all
select 11,'New',1,null from dual
union all
select 21,'Program',11,null from dual
union all
select 61,'Program2',11,'Program2.jsp' from dual
union all
select 31,'Blank',21,'Blank.jsp' from dual
union all
select 2,'View',null,null from dual
union all
select 22,'Printer',null,null from dual
union all
select 28,'Printer2',22,null from dual
union all
select 30,'Printer3',28,'test.jsp' from dual
union all
select 23,'Doc',null,'Doc.jsp' from dual
union all
select 4,'Tools',null,'Tools.jsp' from dual
union all
select 5,'Win',1,'Win.jsp' from dual)
select * from (
select menutbl.*, SYS_CONNECT_BY_PATH(MenuId, '/') as path from menutbl start with parentid is null connect by
NOCYCLE PRIOR MenuId = ParentId) order by path;this prints
1 File /1
11 New 1 /1/11
21 Program 11 /1/11/21
31 Blank 21 Blank.jsp /1/11/21/31
61 Program2 11 Program2.jsp /1/11/61
12 Open 1 Open.jsp /1/12
5 Win 1 Win.jsp /1/5
2 View /2
22 Printer /22
28 Printer2 22 /22/28
30 Printer3 28 test.jsp /22/28/30
23 Doc Doc.jsp /23
4 Tools Tools.jsp /4I guess that you can take over from here and add html elements around the text...
gojko adzic
http://gojko.net -
How to create a Matrix table using this data in SQL Query Analyzer
Hello all,
I have a problem while I am trying to represent my Sql Table namely table1 in Matrix form
my table Format is
city1 city2 Distance--------------------------------------------------------
Mumbai Delhi 100
Delhi Banaras 50
Mumbai Rajasthan 70
Banaras haryana 40
Mumbai Mumbai 0
784 entries
there are 784 cities each having link to other
Now i want my output as
Mumbai Delhi Banaras haryana
Mumbai 0 100 -- --
Delhi 100 0 50 --
Banaras
haryana
respective distance from one city to other should be shown
final Matrix would be 784*784
I am using SQL Query Analyser for this
Please help me in this regardI'm pretty much certain that you don't want to do this in pure SQL. So that means that you want to do it with a reporting tool. I'm not familiar with SQL Query Analyzer, but if it is in fact a reporting tool you'll want to consult its documentation looking for the terms "pivot" or perhaps "cross tab."
-
Can we change/Modify BI server generated Sql query and run to fetch data
Hi,
My client is saying that there is an option to modify bi server generated sql query to fetch data from source.
question:As a request is made in presentation services, A dynamic sql query is generated and fetches data from source. all this is loggedin Nqlquery log..well can we change/modify the sql query generated and run modified sql query to fetch data from source. ., if so how? if not why?
Thanks in advance
Edited by: user10794468 on Jun 16, 2009 6:29 PM
Edited by: user10794468 on Aug 12, 2009 6:58 PMThank you so much for your reply..
..Can we also modify sql query generated by bi server to fetech data. the query's which we see in query log file..
Maybe you are looking for
-
Help with CDOSYS, and insert into DB
I'm using Dreamweaver 8, SQL Server 2000, Windows 2003 Server. This is what so far I was able to achieve. Have a form that inserts records into the database, a confirmation page of the insert, and a simple CDOSYS message to notify me that a record ha
-
Distorted stills in a slideshow created in IMovie
Hello everyone. I have created a 3 minute slideshow with stills from IPhoto, transitions, and music and I have noticed when I view the project in full screen mode there is very noticeable distortion in the video. It looks like a bad tv image, wavy li
-
Simple Way to Track Photos Between Multiple Macs?
Hello, everyone. I hope you are doing well. My wife and I take photos and video with 2 different iPhones, a Nikon SLR, a Panasonic compact digital, and a Flip HD video recorder. I put the photos I take on my Mac Pro. She puts the photos she takes on
-
Is there a sub-forum for HPS? I have one simple question for Performance Scorecard. How to change the trend indicator? I know the performance indicator can be add or edit in the object view, performance indicator list. But for the trend indicator, an
-
When saving sales order Error determining local currency: ER type 'M' date
Hi experts we have created new sales area for intercompany billing process. we are doing testing for this. Update was terminated after saviing sales order express document update was terminated received from author sales order Error Info... M2