How to combine query
I run a query to extract a sum amount from a Table based on ZIP codes and another column that keeps netspend records for sales. So I run one query for one region and then another for another. Is there a way to combine this sample query into one ?
I used TOAD for Oracle to write my queries with:
Basically each query returns two columns, one using the sum function and one using the count function based on the zip codes in and the status code in account_status column.
How can this be accomplished using sql ?
Thanks!
select sum(netspend), count(id_custkey)
from Table
where zip in ('98275',98777','12345')
and
account_status = ('Current')
select sum(netspend), count(id_custkey)
from Table
where zip in ('03457',23456','34532')
and
account_status = ('Current')
Message was edited by:
user587191
Message was edited by:
user587191
SQL> with t as (select '98275' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
2 select '98777' zip, 'Current' account_status, 2 id_custkey, 200 netspend from dual union all
3 select '98777' zip, 'Current' account_status, 3 id_custkey, 300 netspend from dual union all
4 select '12345' zip, 'Current' account_status, 4 id_custkey, 400 netspend from dual union all
5 select '03457' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
6 select '23456' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
7 select '34532' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual)
8 select sum(netspend), count(id_custkey)
9 from T
10 where zip in ('98275', '98777', '12345')
11 and account_status = ('Current');
SUM(NETSPEND) COUNT(ID_CUSTKEY)
1000 4
SQL> with t as (select '98275' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
2 select '98777' zip, 'Current' account_status, 2 id_custkey, 200 netspend from dual union all
3 select '98777' zip, 'Current' account_status, 3 id_custkey, 300 netspend from dual union all
4 select '12345' zip, 'Current' account_status, 4 id_custkey, 400 netspend from dual union all
5 select '03457' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
6 select '23456' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
7 select '34532' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual)
8 select sum(netspend), count(id_custkey)
9 from T
10 where zip in ('03457', '23456', '34532')
11 and account_status = ('Current');
SUM(NETSPEND) COUNT(ID_CUSTKEY)
300 3
SQL> with t as (select '98275' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
2 select '98777' zip, 'Current' account_status, 2 id_custkey, 200 netspend from dual union all
3 select '98777' zip, 'Current' account_status, 3 id_custkey, 300 netspend from dual union all
4 select '12345' zip, 'Current' account_status, 4 id_custkey, 400 netspend from dual union all
5 select '03457' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
6 select '23456' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual union all
7 select '34532' zip, 'Current' account_status, 1 id_custkey, 100 netspend from dual)
8 select sum(netspend), count(id_custkey)
9 from t
10 group by (case when zip in ('98275','98777','12345') and account_status = ('Current') then 1
11 when zip in ('03457','23456','34532') and account_status = ('Current') then 2
12 else 3
13 end)
14 /
SUM(NETSPEND) COUNT(ID_CUSTKEY)
1000 4
300 3
SQL>
Similar Messages
-
How to combine query outputs?
How do we use the XSU to take the xml output of one query and append that to another's output, so that the xml is well-formed and will be handled by an xsl transformation?
We use OracleXMLDocGenString and OracleXMLQuery. We worked it once, yet the xml output contains two "<?xml version = '1.0'?>" and therefore won't get transformed via xsl.
You can do this easily using the XSQL servlet by putting all queries within a single .xsql file.
Thanks.How do we use the XSU to take the xml output of one query and append that to another's output, so that the xml is well-formed and will be handled by an xsl transformation?
We use OracleXMLDocGenString and OracleXMLQuery. We worked it once, yet the xml output contains two "<?xml version = '1.0'?>" and therefore won't get transformed via xsl.
You can do this easily using the XSQL servlet by putting all queries within a single .xsql file.
Thanks. -
How to combine this query so that i can display the ouput together
I have no idea how to combine this query together.Someone please help.I want the ouput to display oni 1 result combining all together.
select facility, route, operation, script_id
from F_ROUTEOPER
where facility = 'A01' and operation in ('6910','7976') AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
select facility, route, operation, script_id
from F_ROUTEOPER
where facility = 'A01' and operation in ('6912','7976') AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
select facility, route, operation, script_id
from F_ROUTEOPER
where facility = 'A01' and operation in ('7344','7976') AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
select facility, route, operation, script_id
from F_ROUTEOPER
where facility = 'A01' and operation in ('8344','7976') AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
Edited by: 965547 on Nov 5, 2012 12:55 AMThe only difference which i am seeing in your queries is the Operation ('6910', '6912','7344','8344')
Then why don't you put all values in One like this
Select facility, route, operation, script_id
from F_ROUTEOPER
where facility = 'A01' and operation in *('6910', '6912','7344','8344','7976')* AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
Hope this will resolve your problem.
Oracle-911 -
How to combine insert and where query ?
hi..
could somebody guide me how to combine insert and where query ?
insert into tdc_imarspc (ANUMBER,EMF_ID,PRODUCT) values ('1234567','65','ITFS')
where anumber not like '1234567';
Thank you,
BaharinHi, Baharin,
As Mahesh said, WHERE only works on a query.
You can always base a query on dual, like this:
INSERT INTO
tdc_imarspc (ANUMBER, EMF_ID, PRODUCT)
SELECT '1234567', '65', 'ITFS'
FROM dual
WHERE anumber NOT LIKE '1234567';Of course, you can use a literal, like '1234567' in both places where I used the bind variable, but if you're not using a variable, why would you want to?
You could also put all the literals in a sub-query (based on dual), and reference column names in the main WHERE clause, LIKE THIS:
INSERT INTO
tdc_imarspc ( ANUMBER, EMF_ID, PRODUCT)
WITH sub_q
AS (
SELECT '1234567' AS an, '65' AS ei, 'ITFS' AS pr
FROM dual
SELECT an, ei, pr
FROM sub_q
WHERE an NOT LIKE '1234567';
I assume you're not really saying
an NOT LIKE '1234567'If you're not using any wild cards after LIKE, you might as well use !=
an != '1234567' -
Dataretrieval in hr abap (how to combine both pa and om in hr abap)
Hi Experts?
My requirement is i wants to retrieve the data from HRP1000 table i.e get the long text from the field sc_text ,for this they have provided me some field values like PLVAR='01' , OTYPE='O' , OBJID -
(not given) ,ENDDA =9999.12.31. and also they have mentioned table PA0001-BUKRS(field).
how to get objid field?
Please provide me query ,how to combine Organizational data(hrp1000) with Personnel administration data(pa0001-bukrs)
Thanks in advance,
mohanHi mohan,
1. Its very impractical,
especially in SAP HR Tables,
to directly get data from tables using joins.
2. In your case,
first read from infotype 0001
using direct select statement,
(or u can use HR_READ_INFOTYPE)
3. After wards, LOOP / FOR ALL ENTRIES IN
and then read from the other required tables HRP1000.
regards,
amit m. -
How to combine the same variables from 2 queries in one webi report
HI ALL,
I created two BW queries/universe and put them into one web intelligence report. Both queries have the same mandatory variable. I combined the relevant dimensions but can't find how to combine the two variables, which resulted in pop-up for 2 variables.
Any idea?Hey Dick Zheng,
Just to close it,
Instead of working on existing reports.
Create new report(s) on top of these changed Universes(2).
1. New WebI report on top of Univ1 (With Prompt)
2. New WebI report on top of Univ2 (Without Prompt)
If both are working as expected. Do follow below:
1. Open any Report (just created in above steps)
2. Edit Query
3. Add Query
4. Select another Universe
5. Drag few objects
6. Run Queries
Now see it is working as expected or not.??
Still any issues post here...,,
Gracias...!! -
How to combine count() queries to tables in different environments?
Hi all,
I've got two tables that have the same schema, but slightly different data. One is a test db, and the other is a model office db (i.e. neither is a production db).
I run these two queries, individually, from within the test db:
select count(cdeSource), cdeSource
from schema1.SomeTable
group by cdeSource
order by cdeSource
select count(cdeSource), cdeSource
from schema1.SomeTable@TheModelOfficeDB
group by cdeSource
order by cdeSource
The column cdeSource can contain a 1 to 1 relationship with another field in the tables, but it can also contain a 1 to many with the other field.
Right now, I need to export each result into a file and then do a compare between the two to find the differences, between the two tables, out of the big results.
Question: How can I combine those two queires into 1, and show only the differences from the "count(cdeSource)" results column?
Thanks.
Edited by: we5inelgr on Jan 29, 2013 5:29 PMwe5inelgr wrote:
Example snippet of results:
Test environment
Count(cdeSource) | cdeSource
2 | "00.01" <---Same as other environment, would like to exclude
6 | "00.09" <---Same as other environment, would like to exclude
16 | "00.50" <---Different count, for same cdeSource. need to include
2 | "00.95" <---cdeSource only found in Test. need to include
2 | "72.1" <---Different count, for same cdeSource. need to include
Model Office environment
Count(cdeSource) | cdeSource
2 | "00.01"
6 | "00.09"
10 | "00.50"
3 | "72.1"
6 | "98.1" <---cdeSource only found in Model Office. need to include
I tried putting "MINUS" in between both queries, and it gives me an error: ORA-00933: SQL command not properly ended.
I tried wrapping that whole query, within a select * from () but gave me a different error: ORA-00907: missing right parenthesis
Other ideas how to combine the two?
Thanks.if & when you use valid syntax, then no errors get thrown.
We can not tell you what you do wrong, since you don't use COPY & PASTE to show us what exactly you do.
SELECT cdeSource, COUNT(cdeSource) FROM TABLE1
MINUS
SELECT cdeSource, COUNT(cdeSource) FROM TABLE1@remote_db; -
How to combine multiple queries!
I know this is pretty basic, but I'm confused. Basically, I need to extract all data for a given entity from a highly normalized database. There are probably 2 dozen tables which contain data for a given entity that must be queried. However, rather than constructing one single query that merges all of the tables together to come up with one huge rowset/row format, how could I query each table seperately and combine the results of each query into one XML file? Can I create a clob with the first query and then append subsequent queries to that clob? Not sure what to do, and I can't find any good examples.
Thanks in advance!Thanks, but I don't think that's quite what I'm looking for. In a nutshell, this is what I'm doing and this is the problem I'm having:
1. I set a string equal to a query.
2. I use xmlgen.getxml to fetch the xml into a clob.
3. I parse the clob into a DOMDocument.
The problem occurs with multiple queries. If I follow this same process for 5 different queries, then I have 5 instances of a DOMDocument, each holding a different queries XML. Now, can I put them together? I tried xmldom.makeNode for each DOMDocument, but the nodes are associated with the Document from which they came. I tried cloning each node to disassociate from it's Document, but that didn't seem to work. Even if it did, and I got all of the nodes together, I kept getting a java.lang.ClassCastException for trying xmldom.makeDocument for a node. I'm lost. How can I combine the results of multiple queries into a single DOMDocument instance? It was suggested that use xmlgen.getXML to get multiple queries into multiple CLOBS, then essentially combine the CLOBS together, and finally parse into a DOMDocument as a last step, but that doesn't really leverage DOMs capabilities. At this point, I may have to go down that road... -
Reasons for Add a combined query being grayed out?
I want to create a combined query but the option is grayed out. This report is a duplicate of an already existing document. I have checked that all the queries in this document are based on the same universe. Are there any other reasons that this may be the case? I have tried creating a document from scratch and the option to combine the queries is available to me.
Do you perhaps have "scope of analysis" set on the data provider? You can check by counting the number of objects visible on the query panel and then open the SQL and see how many select values there are. I have seen cases where the scope of analysis (which adds extra dimensions to the query in an invisible fashion) interferes with the combine queries option.
Note that normally you can check the scope by clicking the appropriate button on the query panel toolbar and invoking the scope panel. However, I have seen some cases where objects are in the SQL even without showing up on the scope panel... it's worth a check. -
Combined Query Output as Filter Criteria
Post Author: imzdo1
CA Forum: WebIntelligence Reporting
I have the need to run a combined query to produce a list of document numbers (I have that query working) and then use the output from that query as the filter criteria in an expanded query in the same Universe. The output from the first query is 6500 document numbers so it would be very time consuming to cut and paste these into a filter 15 or 20 at a time and then pasting the result of the 300 queries needed to get one report.
I have both reports built, but can not figure out how to link the first report output to the filtering criteria on the second report. I am using Webi, not full client version.
Is is possible and if so how?Post Author: RobotSlave
CA Forum: WebIntelligence Reporting
Hello imzdo1, what type of datasource? the following applies to SQL:
sounds like you need to create a temporary table in your SQL stored procedure, like so:
CREATE TABLE #TempDocumentNumbers
documentNumber int,
documentName Varchar(20)
or however many columns you need, then insert into the temporary table using your existing query like so:
INSERT INTO #TempDocumentNumbers
SELECT (whatever your select statement is)
just remember that you will need to have coresponding columns in your temporary table for each column your select statement returns, once you have the values stored in your temporary table you can join on it just like you would on any other table in the database,
I like to explicitly drop temp tables at the end of my query like so:
DROP TABLE #TempDocumentNumbers -
BOXIR2: Use of Minus Combined Query - DeskI vs. WebI
We have a BOXIR2 DeskI report that is a very simple combined query using MINUS. Creating the exact same combined query in WebI displays a significantly higher row count in the report table. However, both report formats bring back the exact same number of rows into the cube. It as is WebI is not evaluating the MINUS at all.
Anyone have issues with MINUS combined queries when creating WebI docs vs. DeskI?
Edited by: Kevin Smolkowicz on Feb 11, 2009 8:55 PMThanks Sarbhjeet!
However, I am trying to get my head around how the 2 report formates handle the data.
In DeskI, the combined query brings all the data back from the database and then performs the MINUS comparison at the application level? I think this based on...
DESKI
Combined Query: Returns back 38,457 rows to the data cube
Combined Query #1 (run by itself): Returns 37,927 rows
Combined Query #2 (run by itself): Returns 530 rows
The report displays the 13 distinct rows that satisfy the MINUS rules
WEBI: With 'Retrieve duplicate rows unchecked
Combined Query: Returns only the 107 rows that satisfy the MINUS rules and correctly displays the 13 distinct rows in the report
WEBI: With 'Retrieve duplicate rows checked
Combined Query: Returns back 38,457 rows to the data cube
Combined Query #1 (run by itself): Returns 37,927 rows
Combined Query #2 (run by itself): Returns 530 rows
The report displays the distinct rows of the 38,457 some of which do NOT satisfy the MINUS. It is like it is not evaluating at all.
Anyway, the resolution worked...just trying to understand the mechanics behind it a bit.
Thanks again. -
How to combine several measures into 1 pie chart
Hello everyone,
I have a requirement to create a design studio application with a pie chart to show several measures.
It means that I have several different measures and I want to let each piece of the pie chart present 1 measure.
For example, I need to combine measure1 ,measure2, measure3(which correspond KeyFigures in Bex query) into 1 pie chart and there should have 3 pieces.I tried change swap axis option and put all measures into row, not works.
I am very appreciate if anyone could help. Thanks a lot.Hi !
There is a tricky way to do that.
Read the following document How to combine several measures into 1 Pie chart
hope it helps,
Roman -
How to combine two datarows (business component data) in BI Publisher
Hi ,
We are using BI Publisher in Siebel Environment.
We have data coming from two business components (like from 2 diff tables)
a) <?for-each:ssTest1?>
b) <?for-each:ssTest2?>
ssTest1 and ssTest2 are the business components
We need to combine these 2 datarows (a&b) and show the data into a single combined data row for ex like <?for-each:ssTest1ssTest2?> and show all the fields in that.
I'm not sure how we can combine these two data rows into a single combined data row and show the data.
Any help from any one would be apprecated.
Thanks
PV
Edited by: user8633002 on Oct 21, 2010 4:05 PMHi sajid
There was nothing more description about your issue in this site and I found an issue below is mostly like yours
http://www.codeproject.com/Questions/855487/how-to-combine-two-table-value-in-rdlc-report
In the issue above, if you want to show the two other tables in the report, I think you could combine the tables into one datatable joining on key. The link below show an example of a DataSet Helper from Microsoft about combine DataSets. Take note of
the related content for other DataSet Helper examples. And then you could use the datatable in your RDLC.
# HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET
http://support.microsoft.com/kb/326080/en-us
In an alternative way, I think you could create a view in the database which combine your tables and use it in your rdlc.
In addition, your issue is about asp.net and you could get more support in the asp.net forum whose link as below.
http://forums.asp.net
Best Regards
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a
href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey. -
"how to use Query include one month "
Hi all expert,
How to make a request how to add table "QTYSALES one month yesterday"
so that I can compare with this month.
Query me this:
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
SELECT T0.ItemCode, T0.Dscription,T1.OnHand, convert(varchar,SUM(T0.BaseQty*T1.NumInSale)) AS 'Qty Sales',DAY(GetDate()) as 'Days',
Case WHEN Month(GetDate()) in (1,3,5,7,8,10,12) THEN 31 WHEN Month(GetDate()) in (2,4,6,9,11) THEN 30 ELSE 28 END AS 'Total days'
,convert (varchar,(t1.onhand/(SUM(T0.BaseQtyT1.NumInSale)/DAY(GetDate()) Case WHEN Month(GetDate()) in (1,3,5,7,8,10,12) THEN 31 WHEN Month(GetDate()) in (2,4,6,9,11) THEN 30 ELSE 28 END)))AS 'TURN OVER IN MONTH'
,round(convert (varchar,(t1.onhand/(SUM(T0.BaseQtyT1.NumInSale)/DAY(GetDate()) Case WHEN Month(GetDate()) in (1,3,5,7,8,10,12) THEN 31 WHEN Month(GetDate()) in (2,4,6,9,11) THEN 30 ELSE 28 END))),4)* 30 AS 'Turn Over Days'
FROM DBO.INV1 T0 INNER JOIN DBO.OITM T1 ON T0.ItemCode = T1.ItemCode
WHERE Month(T0.DocDate) = Month(GetDate()) AND Year(T0.DocDate) = Year(GetDate())
GROUP BY T0.ItemCode,T0.Dscription,T1.OnHand
union
SELECT T1.ItemCode, T1.itemname,T1.OnHand, '' AS 'Qty Sales',DAY(GetDate()) as 'Days',
Case WHEN Month(GetDate()) in (1,3,5,7,8,10,12) THEN 31 WHEN Month(GetDate()) in (2,4,6,9,11) THEN 30 ELSE 28 END AS 'Total days'
,'No Sales' AS 'TURN OVER IN MONTH'
,NULL
FROM DBO.OITM T1
WHERE T1.ITEMCODE NOT IN (SELECT ISNULL(ITEMCODE,0) FROM INV1)
GROUP BY T1.ItemCode,T1.itemname,T1.OnHand
ORDER BY 1
thanks for all
Edited by: rachelhel on May 20, 2009 8:06 AMhaiiii allll pls help...thankss
Hi consulatant B1
How to use Query
My goal is to create a table to see * The number of sales in a month yesterday *
I want to make a request showing the amount of sales at the end of the month.
For example this month is on May 25, so I want to show the Quantity of one month April 25
I have tried several times, but still just wrong.
I need expert help to solve the problem for me.
thanks -
Does anyone know how to combine two apple ID's together?
I have found out that I have two Apple ID's! One is for my apps on my iphone and the other is for iTunes. Somehow they are set to different email addresses but are being sent to the same email address. Does anyone know how to combine them, so I use just one Apple ID?
This is my solution too.
You see, what I also downloaded all the free apps I have on iPhone to this account I choose to use, so this way I didn't need to change accounts (of course it was on the iOS 5 times)
Maybe you are looking for
-
Help with XHTML columns in IE6
I'm having some problems with my columns in IE6. The math is all correct and it lays out perfectly fine in firefox as expected. For some reason IE6 seems to be doubling the numbers I assign as my margins. Can anyone give me a tip as to what's wrong h
-
Tracking of information in a Purchase Order
Hi, Can anyone explain how to put and track the below mentioned information in a Purchase Order? ETS (Expected Time of Shipment by Vendor) ETA (Expected Time of Arrival of goods at Customer location) Advance Payment made to Vendor Thanks in advance
-
PI 7.0 Support Pack 16 Release Note
hi We are starting up with SAP NetWeaver 2004s Support Pack 16 deployment. Our environment is, Windows 2003, MS SQL Server and PI 7.0. Would appriciate if you help me with release note if any and any helpe/tips around this stack deployment. Thanks Se
-
do they have a program where you can go on your home computer from work or any place else. if the do what is the name of it
-
Trouble running Premier pro 2.0 on windows vista
I have recently bought a new laptop running windows vista home premium edition, my XP laptop finally having died. I installed Premier Pro 2.0, which I had been running on XP. During the installation I got the message 'currently installed soundcard