Select statment for two tables
I need a sql query which retrieves all the values from tblemp , tbldept, which have the same deptno effectively even if there are no rows in tblemp for an deptno
my statement is
select empno,ename,sal ,dname from emp,dept where e.deptno =d.deptno=10
works if deptno 10 exists in both tables .how to do it even if there are no rows in tblemp for this deptno keeping nulls in rest of the three.
outer join
SQL> SELECT empno, ename, sal, dname
2 FROM EMP e, DEPT d
3 WHERE e.dno(+) = d.deptno;
EMPNO ENAME SAL DNAME
7369 SMITH 800.2 RESEARCH
7499 ALLEN 1600 SALES
7521 WARD 1250 SALES
7566 JONES 2975 RESEARCH
7654 MARTIN 1250 SALES
7698 BLAKE 2850 SALES
7782 CLARK 2450 ACCOUNTING
7788 SCOTT 3000 RESEARCH
7839 KING 5000 ACCOUNTING
7844 TURNER 1500 SALES
7876 ADAMS 1100 RESEARCH
7900 JAMES 950 SALES
7902 FORD 3000 RESEARCH
7934 MILLER 1300 ACCOUNTING
OPERATIONS
LOGISTIC
Similar Messages
-
ODBC driver hangs for two table joining
Hi,
I was trying to access two tables with the following statements. I used Oracle 8 ODBC driver to access Oracle 9 database. Statement 1 and 2 are working fine but executing Statement 3 will hangs the program.
Statement1: SELECT (fields from two table) from (table1 and table 2) where table1.key=123
Statement2: SELECT (fields from two table) from (table1 and table 2) where table2.key=345
Statement3: SELECT (fields from two table) from (table1 and table 2) where table1.key=table2.key
In the tables 1 and 2 there was no match key, but it should return NULL or nothing.. instead it hangs...
Anyone encounter this problem before? Thanks a lot for replying me...
KeithHi,
I have updated driver to the latest 8.1.7.8.10 which is availiable at the oracle-> download... but the problem still exit... execute sql statment 3 still hangs... :(...
Thanks for your advice.. -
Select data from two tables...!
HI Experts...!
i m a beginner user and i want to select data from two tables proj and prps.....using joins.....and internal tables i have written a code...
SELECT prps~pspnr
prps~objnr
prps~psphi
proj~ernam
proj~erdat
proj~pspnr
INTO table itab -
itab is internal table
FROM prps inner join proj
WHERE pspnr in p_no and prpspsphi = projpspnr.
but there is error in from clause ..please help me....
Advance thanx....Hi,
check the sample code bellow above two reply will solve out your problem but one more extra line in your code pointed out bellow.
TABLES: prps, proj.
TYPES: BEGIN OF ty_test,
pspnr LIKE prps-pspnr,
objnr LIKE prps-objnr,
psphi LIKE prps-psphi,
ernam LIKE proj-ernam,
erdat LIKE proj-erdat,
END OF ty_test.
DATA: itab TYPE STANDARD TABLE OF ty_test WITH HEADER LINE.
SELECT-OPTIONS: p_no FOR prps-pspnr.
SELECT prps~pspnr
prps~objnr
prps~psphi
proj~ernam
proj~erdat
* proj~pspnr " No need for this you have selected this in
* the first line because it is commone so you only need to select from any one
INTO TABLE itab
FROM prps INNER JOIN proj ON ( prps~pspnr = proj~pspnr )
WHERE prps~pspnr IN p_no.
Best Regards,
Faisal
Edited by: Rob Burbank on Dec 24, 2009 12:24 PM -
Selecting data from two tables
I am trying to select data from two tables. The only problem that I am running into, is that i am only seeing results from my 'uploads' table. there is also a record in documents where user = 1 that should show up. here is my sql:
$userIDNum = 1;
$sql="Select *
from uploads, documents
WHERE uploads.user = documents.user AND uploads.user = $userIDNum
ORDER BY uploads.title ASC, documents.title ASC";You'll need to explain a little more about your data and what you are trying to accomplish. Your current sql will select all columns from both the uploads and documents tables but only rows where the user id in both tables match, AND the user id equals 1. Is that not what you are seeing? Where's the code that outputs the results?
-
How do you Select data from two tables with similar data amd merge the output together.
I have two Tables containing Sales Data. I want to read the Table a sort by date and accumulate dollars by order date. Then I want to read the second table and accumulate these dollar amounts by date and then merge the records together so that I gave 1 row
with amounts for type A and amounts for type b.
Here are the tables I am looking at.
Select Cast(J.Order_Date As Varchar(11))) As [Order Date]
,Sum(Case when Sales_Code like '%Comm%' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Comm]
,Sum(Case when Sales_Code = '5-Day' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
,Sum(Case when Sales_Code like '%Auto%" then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
,Sum(Case when Sales_Code = '' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Fixed]
,Sum(Case when Sales_Code = 'XX' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Comm)
,Sum(Case when Sales_Code = 'YY' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Auto)
,Sum(Case when Sales_Code = 'ZZ' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Fixed)
from [PRODUCTION].dbo.Job As J
union all
Select Cast(SH.Order_Date As Varchar(11))) As [Order Date]
,Sum(Case when Sales_Code like '%Comm%' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Comm]
,Sum(Case when Sales_Code = '5-Day' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
,Sum(Case when Sales_Code like '%Auto%" then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
,Sum(Case when Sales_Code = '' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Fixed]
,Sum(Case when Sales_Code = 'XX' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Comm)
,Sum(Case when Sales_Code = 'YY' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Auto)
,Sum(Case when Sales_Code = 'ZZ' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Fixed)
from [PRODUCTION].dbo.SO_Detail As SD
Inner Join [PRODUCTION].dbo.SO_Header As SH
on SD.Sales_Order = SH.Sales_Order
Group by J.Order_Date
Order by J.Order_Date Desc
Looking for output like
Order Date Job Comm Job AUto Job Fixed SO Comm SO AUto SO Fixed
Mar-11-2014 100.00 250.00 50.00 200.00 300.00 400.00
Mar-10-2014 500.00 340.00 0.00 110.00 400.00 500.00
Mar-09-2014 600.00 333.00 56.00 210.00 500.00 300.00
Thanks for your help
SWProductionSeeing the output it looks like what you need is this
select COALESCE(p.[Order Date],q.[Order Date]) AS [Order Date],
COALESCE([Job Comm],0) AS [Job Comm],
COALESCE([Job AUto],0) AS [Job AUto],COALESCE([Job Fixed],0) AS [Job Fixed],COALESCE([SO Comm],0) AS [SO Comm],COALESCE([SO AUto],0) AS [SO AUto],COALESCE([SO Fixed],0) AS [SO Fixed]
from
Select Cast(J.Order_Date As Varchar(11))) As [Order Date]
,Sum(Case when Sales_Code like '%Comm%' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Comm]
,Sum(Case when Sales_Code = '5-Day' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
,Sum(Case when Sales_Code like '%Auto%" then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
,Sum(Case when Sales_Code = '' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Fixed]
,Sum(Case when Sales_Code = 'XX' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Comm)
,Sum(Case when Sales_Code = 'YY' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Auto)
,Sum(Case when Sales_Code = 'ZZ' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Fixed)
from [PRODUCTION].dbo.Job As J
)p
full join
Select Cast(SH.Order_Date As Varchar(11))) As [Order Date]
,Sum(Case when Sales_Code like '%Comm%' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Comm]
,Sum(Case when Sales_Code = '5-Day' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
,Sum(Case when Sales_Code like '%Auto%" then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
,Sum(Case when Sales_Code = '' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Fixed]
,Sum(Case when Sales_Code = 'XX' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Comm)
,Sum(Case when Sales_Code = 'YY' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Auto)
,Sum(Case when Sales_Code = 'ZZ' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Fixed)
from [PRODUCTION].dbo.SO_Detail As SD
Inner Join [PRODUCTION].dbo.SO_Header As SH
on SD.Sales_Order = SH.Sales_Order
Group by J.Order_Date
)q
on p.[Order Date] = q.[Order Date]
Order by COALESCE(p.[Order Date],q.[Order Date]) Desc
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
Can i select Records from two tables into an itab.
Hi,
Suppose that i have two tables spfli and sflight.
Now i want the records from both the tables into a single internal table.
If so please let me know how this can be done.
itab should consist of all the columns in sflight and spfli.
Regards,
SaiSai,
Yes u can do it using JOINs.
Use this link for ref
http://www.sap-img.com/abap/inner-joins.htm
INNER JOIN results are an intersection of the tables being joined where in only if both the tables havethe data the result is pused onto the result set.
WHERE as in LEFT OUTR JOIN you can push the data of the LEF T table on the resultset even when the join condition is not met.
The use is that you wantto have all the data that is there in left table and also the right table if the join condition is a success then right table fileds will have data else they are initial.
OUTJOIN's are used in MAINTENANNCE VIEWS, HELP VIEWS .
INNER JOINS are used DATABSE VIEWS.
Inner Join:-
SELECT pcarrid pconnid ffldate bbookid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM ( ( spfli AS p
INNER JOIN sflight AS f ON pcarrid = fcarrid AND
pconnid = fconnid )
INNER JOIN sbook AS b ON bcarrid = fcarrid AND
bconnid = fconnid AND
bfldate = ffldate )
WHERE p~cityfrom = 'FRANKFURT' AND
p~cityto = 'NEW YORK' AND
fseatsmax > fseatsocc.
Left Outer Join
SELECT scarrid scarrname p~connid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM scarr AS s
LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND
p~cityfrom = 'FRANKFURT'.
Reward if helpful,
Karthik -
Selection screen for custom table
We have developed a custom table and user want to have a selection selection screen similar to SE16. There are 9 fields in the table and user can enter data to the selection screen with any combination. My question is how do I filter based on the user input ? DO I need to write multiple SQL for different combination of selection screen parameter within IF ELSEIF statement ? If so then there will be huge number of SQL within IF & ELSEIF statment for 9 selection parameter. Please suggest if you have any better ideas.
Even after using select-option along with IN statement in where clause , it did not work.
I used following selection screen defination :
SELECTION-SCREEN: BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.
SELECT-OPTIONS: P_PCBUS FOR MARA-ZZPC_BUSINESS NO-EXTENSION NO INTERVALS,
P_PCBUS1 FOR MARA-ZZPC_BUS_SUB1 NO-EXTENSION NO INTERVALS,
P_PLNFMT FOR MARA-ZZPLNG_FORMAT NO-EXTENSION NO INTERVALS,
P_PRDB FOR MARA-ZZPROD_BRAND NO-EXTENSION NO INTERVALS,
P_SEASCD FOR MARA-ZZSEASON_CODE NO-EXTENSION NO INTERVALS,
P_SNP FOR MARA-ZZSUP_NET_PATH NO-EXTENSION NO INTERVALS,
P_MAT_DC FOR MARA-MATNR NO-EXTENSION NO INTERVALS,
P_MAT_MG FOR MARA-MATNR NO-EXTENSION NO INTERVALS.
SELECTION-SCREEN: END OF BLOCK B2.
and SQL statement :
SELECT ZZPC_BUSINESS
FROM ZTB_RTL_TO_WHS
INTO TABLE TA_ZZPCBUS
WHERE ZZPC_BUSINESS = P_PCBUS
AND ZZPC_BUS_SUB1 IN P_PCBUS1
AND ZZPROD_BRAND IN P_PRDB
AND ZZPLNG_FORMAT IN P_PLNFMT
AND ZZSEASON_CODE IN P_SEASCD
AND ZZSUP_NET_PATH IN P_SNP
AND ZZD_MATNR IN P_MAT_DC
AND ZZM_MATNR IN P_MAT_MG.
While executing program, I only enterd value for the field P_PCBUS. Though record present for that value, I rcvd SY-SUBRC = 4.
ANy Idea?
Edited by: J. Bakshi on Jul 14, 2009 10:17 AM -
How to write select statement between two tables
hi,
i need to do comparision between two table for each records.ex:
table1:
regid regno ind
1 1001
1 1002
1 1003
1 1004
and table2:
regid regno
1 1002
1 1005
i need to select first row from table and loop for values in second table, if the values found first record , the record must update 'yes' to ind, if not 'No'.
please help with this.
thanks in advance
rajaHi Raja,
Do it like this. loop the first table and read the second table.
Use where condition to satisfy the conditions.
Or
In your select query use joins. Like this.
SELECT mara~matnr
marc~werks
INTO TABLE t_material
FROM mara AS mara INNER JOIN marc AS marc
ON maramatnr = marcmatnr
WHERE mara~mtart = p_mtart.
Instead of MARA and MARC here use your tables.
Much Regards,
Amuktha. -
Hi all,
I have two tables:
CRETE TABLE T1 (T1_COL1 NUMBER, T1_COL2 NUMBER)
CRETE TABLE T2 (T2_COL1 NUMBER, T2_COL2 NUMBER)
T1 may or may not have records. T2 always has records. There are two scenarios.
Scenario 1:
=======
SELECT * FROM T1 returns five rows, and SELECT * FROM T2 returns 10 rows.
Now I need the five rows from T1.
Scenario 2:
=======
SELECT * FROM T1 returns zero rows, and SELECT * FROM T2 returns 10 rows.
Now I need the 10 rows from T2.
In other words, if records present in T1, I need them all. If not, I need records from T2.
There are no common columns (for joins).
Now need a single query to achive this. I tried set operators, CASE and DECODE. But I'm unable to solve it.
Please help. Thanks in advance.Iniyavan wrote:
Yes, Justin. I'm sure that this is the way it's modelled. I also find it's tough and odd.Are you sure it's the right way for that data to be modeled? I understand that's the way it is being modeled, but a data model change may be the best option.
Is there any other way, which is simpler, without using RANK?That's the simplest option I can think of. You could also do something like
SELECT t1_col1, t1_col2
FROM (
SELECT t1_col1, t1_col2, rownum rn
FROM (
SELECT t1_col1, t1_col2
FROM (
SELECT t1_col1, t1_col2, 1 tbl
FROM t1
UNION ALL
SELECT t2_col2, t2_col2, 2 tbl
FROM t2
ORDER BY tbl
WHERE rn = 1I'm not sure that's any simpler...
Justin -
Selecting columns from two table is slow but same
I am selecting 27 columns from two tables
which running for more than 30 minutes. but
if I select count(*) with the same query
except the columns it is coming in seconds.
Where is the error?If you post
1) The table definitions for the underlying tables
2) The indexes that are on the tables
3) The two SQL statements you're running
4) The explain plan for both statements
we can probably be of some assistance.
My guess is that the count(*) is able to return much more quickly because the optimizer is able to use a significantly faster query plan that is based on an index which the longer-running query cannot utilize. Without the information I've requested, though, it's hard to do more than speculate.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Select statement on two tables
Hi Experts,
I'm working on HTTP to JDBC synchronous interface.JDBC is the receiver side.
I need to fetch data from two tables based on certain conditions, please help me out in getting an approach for this interface?
Thanks,
Pravin.Hi Jayarama,
I have this following select Query
SELECT count(*)
from kss_activity_stg kas, kss_consumer_hdr kca WHERE
kas.customer_status_flg='A' AND
kas.partner_type='2' AND
kas.segment_type_cd='BLDR' AND
kca.property_name ='CHELDAN HOMES'
based upon the above select query, i need to do a HTTP to JDBC synchronous interface. -
Unable to generate spool for two tables in report output
Hi,
I created report with two custom containers displaying two tables in output. When I execute the report in background spool is created only for one table in top custom container.
What should be done to generate spool for both the tables in two different custom containers.
Thanks,
Abhiram.Hi,
Check the bellow link for your requirement.
<<link removed>>
Regards,
Goutam Kolluru.
Edited by: kishan P on Feb 2, 2012 1:50 PM -
How to use the TableSorter for two tables at the same view?
Hello,
I am using the TableSorter object in order to sort Dynpro tables.
Suppose I have two tables at the same view, is it possible to use it seperatly for both of them?
I assume that at the wdModifyView method I will need to catch the table that the user clicked on, yet I don't have an idea of how to do it...Hi Roy,
Constructor of TableSorter
public TableSorter(IWDTable table, IWDAction sortAction, Comparator[] comparators)
So, you have to create instance of TableSorter class for each table on the view.
best regards, Maksim Rashchynski. -
Select statment - fill internal table
Hi,
I have following situation:
TYPES: BEGIN OF q_data,
i_lqua LIKE lqua,
v_xxx(20) type c,
END OF q_data.
DATA: a_data TYPE STANDARD TABLE OF q_data.
Now I would write a select statment and fill the table a_data.
select lqua~matnr lqua~werks... from lqua inner join mch1 into (i_lqua-matnr, i_lqua-werks.....)...
Is it possible to write a statement without writing the whole structure (all the names of columns) of lqua in the SQL-statement (lquamatnr lquawerks lqua~........)?
Thanks
Michael1. you can write that but i think you have to write that in SELECT ...ENDSELECT.
select lquamatnr lquawerks... from lqua inner join mch1 into (i_lqua-matnr, i_lqua-werks.....
append i_lqua.
endselect.
2. OR you can use INTO CORRESPONDING FIELDS OF TABLE
select lquamatnr lquawerks... from lqua inner join mch1 into corresponding fields of table i_lqua. -
Creating a generic selection listener for af:table
Hi,
I'm using JDeveloper 11.1.1.5.0, and I have a simple requirement to set up a custom selection listener for an af:table component.
So far, I was doing this by using a custom function to invoke the EL to perform the makeCurrent operation. However, I came across Frank Nimphius' PDF on how to build a generic selection listener, and this seemed like a better way to go about it. The problem is, the CollectionModel class used in the sample code is now deprecated. Is anyone aware of the currently supported way to implement a generic selection listener?
Thanks,
Gillthis snap shot of the code i write it to get the current employee id whcich is selected
public void select(SelectionEvent selectionEvent) {
// Add event code here...
RichTable _table = (RichTable)selectionEvent.getSource();
CollectionModel _tableModel = (CollectionModel)((RichTable)selectionEvent.getSource()).getValue();
JUCtrlHierBinding _adfTableBinding = (JUCtrlHierBinding)_tableModel.getWrappedData();
DCIteratorBinding _tableIteratorBinding = _adfTableBinding.getDCIteratorBinding();
//Acess the ADF iterator binding that is used with ADF table binding
Object _selectedRowData = _table.getSelectedRowData();
JUCtrlHierNodeBinding _nodeBinding =
(JUCtrlHierNodeBinding)_selectedRowData; //get the row key from the node binding and set it //as the current row in the iterator
Key _rwKey = _nodeBinding.getRowKey();
_tableIteratorBinding.setCurrentRowWithKey( _rwKey.toStringFormat(true));
DCIteratorBinding it = _adfTableBinding.getDCIteratorBinding();
System.out.println( it.getCurrentRow().getAttribute("EmployeeId"));
}this code i written 11.1.2
Maybe you are looking for
-
Since upgrading to Mavericks, my Time Machine is stuck on "Preparing Backup".
Since upgrading to Mavericks, every time I attempt to run a Time Machine backup, I can only get it to go as far as "Preparing Backup". I've left it like that for hours just to see if it's slow. I've also stopped and restarted Time Machine multiple
-
How to print text inside a box in sap script? please let me know the process
-
I can't log in the App store because am from Zambia which is not listed in Apple outlets. How do i go around this as apple products are used globally and not only in the listed countries. My product is a Mac book pro. i feel this limits how i can use
-
I am running the 9.0.3.2 JDev Maintenance Release and am a newbie to JDev. I'm trying to test a remote deployment by deploying to OC4J running outside of JDev. When I try to create the application server connection using a type of 'Standalone OC4J' I
-
Aggregation level on single level basis or multiprovider
Hello, we use IP 7.0 and have a planning cube for CO-PA data, as well as a multiprovider combining the actual cube and the planning cube for COPA data. We need to implement a planning tool in which planning simple planning functions are needed on the