How to do proper group by when joining two tables ?
Hi all,
Using the below join query, I would like to get output based on number of "Classes" ( For eg: If I want result for 15 classes then , it should return 16 rows from 0 to 16).
I n below query , I use group by as
"GROUP BY report_parameters.report_parameter_value ".
Error says "its not group by expression".
If I commented out group by , then it reurns 320 rows instead of 16 rows.
Could anyone help me?
SELECT 'SUM('
|| 'CASE '
|| 'WHEN edr_class_by_gvw_report_data.bin_id >= ' || report_range_parameters.report_parameter_min_value || '
AND edr_class_by_gvw_report_data.bin_id < ' || report_range_parameters.report_parameter_max_value || '
THEN edr_class_by_gvw_report_data.bin_value '
|| 'ELSE 0 '
|| 'END '
|| ') "Class ' || report_parameters.report_parameter_value || '" '
FROM report_parameters
JOIN report_range_parameters
ON report_parameters.report_parameter_id = report_range_parameters.report_parameter_id
WHERE report_range_parameters.report_parameter_id = 2316
AND report_range_parameters.report_parameter_group = 'GVW_GROUP'
AND report_range_parameters.report_parameter_name = 'GVW_NAME'
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS'
GROUP BY
report_parameters.report_parameter_value
ORDER BY report_range_parameters.report_parameter_min_value ASC;Thanks .
Edited by: user10641405 on Jun 11, 2009 12:23 PM
Edited by: user10641405 on Jun 11, 2009 12:30 PM
Hi Frank,
Thank you so much for your reply.
Yes it is the same query.
Okay, here I will explain what i actually want.
Below is my final output(No doubt)
veh weight class 0 class 1 class 2
0->5 0 5 10
5-> 10 0 22 32
10->15 0 12 67
In final dynamic sql(which have to generate the above final output), we use 2 functions(called my_class_select_text and my_class_sum_text) to genearte two types of sql strings as below:so the output that I asked is regards this functions' output not the final output.
"The below output( is the one I asked previously) is from the my_class_select_text function"
SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 0 AND edr_class_by_gvw_report_data.gvw < 5 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 0"
SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 5 AND edr_class_by_gvw_report_data.gvw < 10 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 1"
SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 10 AND edr_class_by_gvw_report_data.gvw < 15 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 2"
SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 15 AND edr_class_by_gvw_report_data.gvw < 20 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 3"
SUM(CASE WHEN edr_class_by_gvw_report_data.gvw >= 20 AND edr_class_by_gvw_report_data.gvw < 25 THEN edr_class_by_gvw_report_data.gvw_count ELSE 0 END ) "Class 4"
"The below output is from the function my_class_sum_text"
SUM("0->5") "0->5"
SUM("5->10") "5->10"
SUM("10->15") "10->15"
SUM("15->20") "15->20"
"here i have pasted those functions.so you could have some better idea"
FUNCTION edr_rpt_get_class_select_text
in_report_parameter_id IN NUMBER
RETURN VARCHAR2
IS
my_class_select_text VARCHAR2(32676);
my_class_select_value VARCHAR2(32676);
CURSOR class_select_text IS
SELECT 'SUM('
|| 'CASE '
|| 'WHEN edr_class_by_gvw_report_data.gvw >='|| report_range_parameters.report_parameter_min_value ||'
AND edr_class_by_gvw_report_data.gvw <'|| report_range_parameters.report_parameter_max_value ||'
THEN edr_class_by_gvw_report_data.gvw_count '
|| 'ELSE 0 '
|| 'END '
|| ') "Class ' || report_parameters.report_parameter_value || '" '
FROM report_parameters
JOIN report_range_parameters
ON report_parameters.report_parameter_id = report_range_parameters.report_parameter_id
WHERE report_parameters.report_parameter_id = in_report_parameter_id
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS'
AND report_range_parameters.report_parameter_group = 'GVW_GROUP'
AND report_range_parameters.report_parameter_name = 'GVW_NAME'
ORDER BY CAST(report_parameters.report_parameter_value AS NUMBER) ASC;
BEGIN
my_class_select_text := '';
OPEN class_select_text;
LOOP
FETCH class_select_text INTO my_class_select_value;
EXIT WHEN class_select_text%NOTFOUND;
my_class_select_text := my_class_select_text || ', ' || my_class_select_value;
END LOOP;
CLOSE class_select_text;
RETURN my_class_select_text;
END edr_rpt_get_class_select_text;
FUNCTION edr_rpt_get_class_sum_text
in_report_parameter_id IN NUMBER
RETURN VARCHAR2
IS
my_class_sum_text VARCHAR2(32676);
my_class_sum_value VARCHAR2(32676);
CURSOR class_sum_text IS
SELECT 'SUM("' || report_range_parameters.report_parameter_min_value || '->' || report_range_parameters.report_parameter_max_value || '")
"Class ' || report_parameters.report_parameter_value || '" '
FROM report_range_parameters
JOIN report_parameters
ON report_range_parameters.report_parameter_id = report_parameters.report_parameter_id
WHERE report_range_parameters.report_parameter_id = in_report_parameter_id
AND report_range_parameters.report_parameter_group = 'GVW_GROUP'
AND report_range_parameters.report_parameter_name = 'GVW_NAME'
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS'
ORDER BY report_range_parameters.report_parameter_min_value ASC;
BEGIN
my_class_sum_text := '';
OPEN class_sum_text;
LOOP
FETCH class_sum_text INTO my_class_sum_value;
EXIT WHEN class_sum_text%NOTFOUND;
my_class_sum_text := my_class_sum_text || ', ' || my_class_sum_value;
END LOOP;
CLOSE class_sum_text;
RETURN my_class_sum_text;
END edr_rpt_get_class_sum_text;Here I'm using one more table called edr_class_by_gvw_report_data.
gvw | gvw_count
(veh weight in kips) (count of veh )
5 3
12 10
13 5
sample insert statement
INSERT INTO EDR_CLASS_BY_REPORT_DATA(GVE,GVW_COUNT) VALUES(5,3)
INSERT INTO EDR_CLASS_BY_REPORT_DATA(GVE,GVW_COUNT) VALUES(12,10)
INSERT INTO EDR_CLASS_BY_REPORT_DATA(GVE,GVW_COUNT) VALUES(13,5)Below i have pasted portion of my dynamic sql (this is sample from "class by hour report" . It includes "Hour"(That I highlighted in red.It will generate output as
0-1
1-2
2-3
etc )
In this place, I need to replace by weight ranges as "Kips" because now the report that I'm working is called "class by gross veh weight".
|| ' SELECT 1 "Rank", '
|| ' ''Data Row'' "Row Type", '
|| ' TRUNC(edr_rpt_tmp_bin_periods.bin_start_date_time ) "Date", '
|| ' edr_rpt_tmp_bin_periods.bin_start_date_time "Date/Time", '
|| ' '''' "Lane", '
" || ' TO_CHAR(edr_rpt_tmp_bin_periods.bin_start_date_time, ''hh24'') '"
" || ' || '' - '' || ' "
" || ' DECODE(TO_CHAR(edr_rpt_tmp_bin_periods.bin_end_date_time, ''hh24''), ''00'', ''24'', TO_CHAR(edr_rpt_tmp_bin_periods.bin_end_date_time, ''hh24'')) "Hour" ' "
|| my_class_sum_text || ', ' "<------------- function generates sql string"
|| ' SUM(NVL(" ", 0)) "Total" '
|| ' FROM ( '
|| ' SELECT edr_class_by_gvw_report_data.bin_start_date_time start_date_time '
|| my_class_select_text || ', ' "<------------------function generates sql string"
|| ' SUM(NVL(edr_class_by_gvw_report_data.gvw_count, 0)) " " '
|| ' FROM edr_class_by_gvw_report_data '
|| ' GROUP BY edr_class_by_gvw_report_data.bin_start_date_time '
|| ' ) results '
|| ' RIGHT OUTER JOIN edr_rpt_tmp_bin_periods '
|| ' ON results.start_date_time >= edr_rpt_tmp_bin_periods.bin_start_date_time '
|| ' AND results.start_date_time < edr_rpt_tmp_bin_periods.bin_end_date_time '
|| ' GROUP BY edr_rpt_tmp_bin_periods.bin_start_date_time, '
|| ' edr_rpt_tmp_bin_periods.bin_end_date_time '
|| ' ORDER BY "Date/Time" ASC, '
|| ' "Rank" ASC 'I'm really thankful for your kindness and pain of reading all my blablablaaasss.
Hope you could have some better understnading by this reply.
I'm willing to answer all your questions.
Regards,
Indhu.
Edited by: user10641405 on Jun 15, 2009 4:55 PM
Edited by: user10641405 on Jun 15, 2009 4:59 PM
Edited by: user10641405 on Jun 15, 2009 5:01 PM
Edited by: user10641405 on Jun 15, 2009 5:02 PM
Edited by: user10641405 on Jun 16, 2009 8:29 AM
Edited by: user10641405 on Jun 16, 2009 8:50 AM
Edited by: user10641405 on Jun 16, 2009 11:55 AM
Similar Messages
-
How to prevent Oracle from using an index when joining two tables ...
How to prevent Oracle from using an index when joining two tables to get an inline view which is used in an update statement?
O.K. I think I have to explain what I mean:
When joining two tables which have many entries sometimes it es better not to use an index on the column used as join criteria.
I have two tables: table A and table B.
Table A has 4.000.000 entries and table B has 700.000 entries.
I have a join of both tables with a numeric column as join criteria.
There is an index on this column in table A.
So I instead of
where (A.col = B.col)I want to use
where (A.col+0 = B.col)in order to prevent Oracle from using the index.
When I use the join in a select statement it works.
But when I use the join as inline view in an update statement I get the error ORA-01779.
When I remove the "+0" the update statement works. (The column col is unique in table B).
Any ideas why this happens?
Thank you very much in advance for any help.
Regards HartmutI think you should post an properly formatted explain plan output using DBMS_XPLAN.DISPLAY including the "Predicate Information" section below the plan to provide more details regarding your query resp. update statement. Please use the \[code\] and \[code\] tags to enhance readability of the output provided:
In SQL*Plus:
SET LINESIZE 130
EXPLAIN PLAN FOR <your statement>;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Usually if you're using the CBO (cost based optimizer) and have reasonable statistics gathered on the database objects used the optimizer should be able to determine if it is better to use the existing index or not.
Things look different if you don't have statistics, you have outdated/wrong statistics or deliberately still use the RBO (rule based optimizer). In this case you would have to use other means to prevent the index usage, the most obvious would be the already mentioned NO_INDEX or FULL hint.
But I strongly recommend to check in first place why the optimizer apparently seems to choose an inappropriate index access path.
Regards,
Randolf
Oracle related stuff:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle:
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
DataType conflict in the R/3 when joining two tables
Hi Experts,
I have created a view with two table in the R/3 side by joining two common fields,but one of the field is getting error with different data type, so could any one suggest me how to resolve this issue without changing type.
Thanks,
VenugopalHi
Actually I have to pull one of the field from one table and place in other table and create a generic data source on it,Can any one suggest me with any solution or exists to solve this issue.
Thanks,
Venugopal -
Problem when joining two tables
hi,
in one table i have
matno mattype
00001 a
00002 b
00003 c
00004 d
in table two i have
vno matno
v001 1
v002 2
v003 3
v004 4
when i am trying to link these two tables with matno it is not happening
is it because it is showing '00001' in one table and '1' in other table
how to fix this
note:same material info object is their in both the tablesHi,
are your two tables attribute master data tables? Can you paste the names here?
If as you mention you ae using the same IObj 0MATERIAL, the data in should be the same, unless you didn't turn the conversion routine in the TRules of your second IObj (table vno/matno)
Please describe with more detail the infoObjects related to your two tables and the TRules associated.
As a very first check, verify if your material has MATN1 conversion routine and if you have activated it in both TRules; if not, do that, reload your master data, activate it and check it again.
hope this helps...
Olivier. -
Strange problem when joining two tables
Hi,
I have recently encountered a strange problem on an Oracle 11gR2 database which is optimized for Datawarehouse usage.
I am using two tables that have a relationship enforced by two fields of type NUMBER(10).
The problem is that when I am joining these two tables very often I get strange results and when I re-execute the query I get a different result. I saw in the explain plan that the Hash Join is used for joining these two tables.
select count(*)
from recharge_history rh, recharge_history_balance rhb
where rh.recharge_id = rhb.recharge_id
and rh.recharge_id2 = rhb.recharge_id2
and trunc(rh.recharge_date_time) between '30-Dec-2012' and '31-Dec-2012'
If I explicitly set the Join method to some other type through SQL Hints, or if I use to_number function when joining (even though the two fields used for join in both tables are of NUMBER(10) type), I get the correct result, like for example below:
select /*+ USE_MERGE (rh rhb) */
count(*)
from recharge_history rh, recharge_history_balance rhb
where rh.recharge_id = rhb.recharge_id
and rh.recharge_id2 = rhb.recharge_id2
and trunc(rh.recharge_date_time) between '30-Dec-2012' and '31-Dec-2012'
select
count(*)
from recharge_history rh, recharge_history_balance rhb
where to_number(rh.recharge_id) = t_number(rhb.recharge_id)
and to_number(rh.recharge_id2) = (to_number)rhb.recharge_id2)
and trunc(rh.recharge_date_time) between '30-Dec-2012' and '31-Dec-2012'
Thank you for your time.
EdrinHi, Edrin,
961841 wrote:
Hi,
I have recently encountered a strange problem on an Oracle 11gR2 database which is optimized for Datawarehouse usage.
I am using two tables that have a relationship enforced by two fields of type NUMBER(10).
The problem is that when I am joining these two tables very often I get strange results and when I re-execute the query I get a different result. I saw in the explain plan that the Hash Join is used for joining these two tables.
select count(*)
from recharge_history rh, recharge_history_balance rhb
where rh.recharge_id = rhb.recharge_id
and rh.recharge_id2 = rhb.recharge_id2
and trunc(rh.recharge_date_time) between '30-Dec-2012' and '31-Dec-2012'
Don't try to compare DATEs with VARCHAR2s, such as '30-Dec-2012'. The VARCHAR2 '31-Aug-2012' is between '30-Dec-2012' and '31-Dec-2012'; so are '31-Aug-2013' and '30-Mar-1999'.
That may not be your only problem, but it's still a problem.
If you're getting incosistent results, then it sounds like a bug. Start a service request with Oracle support. -
Using group by when joining 2 tables
hello
i am using hr schema 2 tables hr.employees and hr.departments
and my target table have following columns dept_id,dept_name,dept_manager full name and dept_total_salary
In interface i have join these 2 tables using employees.employee_id=departments.department_id
also i have use sum function at the target salary column.
But i am not getting the result,its just printing that manager's salary and not sum
Please help
thank youthank u thank u for the reply
i was about to paste that
here it goes
insert into STUDENT.I$_DEPT_INFO2
DID,
DNAME,
D_MGR_NM,
TOT_SAL,
IND_UPDATE
select
DEPARTMENTS.DEPARTMENT_ID,
DEPARTMENTS.DEPARTMENT_NAME,
EMPLOYEES.FIRST_NAME||' '||EMPLOYEES.LAST_NAME,
SUM(EMPLOYEES.SALARY) ,
'I' IND_UPDATE
from HR.DEPARTMENTS DEPARTMENTS, HR.EMPLOYEES EMPLOYEES
where (1=1)
And (DEPARTMENTS.MANAGER_ID=EMPLOYEES.EMPLOYEE_ID)
Group By DEPARTMENTS.DEPARTMENT_ID,
DEPARTMENTS.DEPARTMENT_NAME,
EMPLOYEES.FIRST_NAME||' '||EMPLOYEES.LAST_NAME
now i know the where the problem lies
its becouse it doing group by EMPLOYEES.FIRST_NAME||' '||EMPLOYEES.LAST_NAME
thats the reason i am not getting correct output
but i dont know how to solve this
please help
thanks again -
hi
how to join two tables using inner join if the first table has two primary keys and second table has 3 primary keysWould describe type of joins in ABAP, which might differ with other joins.
The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of INNER JOIN or LEFT OUTER JOIN. Depending on the type of join, a join expression can be either an inner (INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.
On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.
AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.
The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:
At least one comparison must be specified after ON.
Individual comparisons may be joined using AND only.
All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.
The following additions not be used: NOT, LIKE, IN.
No sub-queries may be used.
For outer joins, only equality comparisons (=, EQ) are possible.
If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.
In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.
Resulting set for inner join
The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.
Resulting set for outer join
The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.
Note
If the same column name occurs in several database tables in a join expression, they have to be identified in all remaining additions of the SELECT statement by using the column selector ~.
Example
Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.
PARAMETERS: p_cityfr TYPE spfli-cityfrom,
p_cityto TYPE spfli-cityto.
DATA: BEGIN OF wa,
fldate TYPE sflight-fldate,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa.
DATA itab LIKE SORTED TABLE OF wa
WITH UNIQUE KEY fldate carrname connid.
SELECT ccarrname pconnid f~fldate
INTO CORRESPONDING FIELDS OF TABLE itab
FROM ( ( scarr AS c
INNER JOIN spfli AS p ON pcarrid = ccarrid
AND p~cityfrom = p_cityfr
AND p~cityto = p_cityto )
INNER JOIN sflight AS f ON fcarrid = pcarrid
AND fconnid = pconnid ).
LOOP AT itab INTO wa.
WRITE: / wa-fldate, wa-carrname, wa-connid.
ENDLOOP.
Example
Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.
PARAMETERS p_cityfr TYPE spfli-cityfrom.
DATA: BEGIN OF wa,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa,
itab LIKE SORTED TABLE OF wa
WITH NON-UNIQUE KEY carrid.
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 = p_cityfr.
LOOP AT itab INTO wa.
IF wa-connid = '0000'.
WRITE: / wa-carrid, wa-carrname.
ENDIF.
ENDLOOP. -
Problem encountered when join two remote tables in a materialized view
I'm using oracle 9.2.0.6
1> I have two tables:
CREATE TABLE TEST
A VARCHAR2(100 BYTE),
C DATE
CREATE TABLE TEST1
A VARCHAR2(100 BYTE),
B TIMESTAMP
2>. I defined a prebuild table:
CREATE TABLE MV_TEST1
ID1 ROWID,
A VARCHAR2(100 BYTE),
ID2 ROWID,
B TIMESTAMP(6),
C DATE
3> I created mview logs:
CREATE MATERIALIZED VIEW LOG ON PSI_TEST.TEST
WITH ROWID
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON PSI_TEST.TEST1
WITH ROWID
INCLUDING NEW VALUES;
4> when I create mview:
CREATE MATERIALIZED VIEW PSI_TEST.MV_TEST1
ON PREBUILT TABLE WITH REDUCED PRECISION
REFRESH FAST ON DEMAND
WITH PRIMARY KEY
AS
select
test.rowid id1,
test.a,
test1.rowid id2,
test1.b,
cast(null as date) c
from test , test1
where test.a = test1.a(+);
It is created successfully.
5> problem:
when I use remote tables to do the same thing, say test and test1 are in another instance and are connected by a dbLink, I couldn't create the mview successfully:
CREATE MATERIALIZED VIEW PSI_TEST.MV_TEST1
ON PREBUILT TABLE WITH REDUCED PRECISION
REFRESH FAST ON DEMAND
WITH PRIMARY KEY
AS
select
a.rowid id1,
a.a,
b.rowid id2,
b.b,
cast(null as date) c
from test@dbl a, test1@dbl b
where a.a = b.a(+);
when run above statement, I got:
ORA-12015: cannot create a fast refresh materialized view from a complex query
Any ideas? Or joining two table through a dblink for a mview is not allowed at all?
Thanks in advance.No one has a clue?
Message was edited by:
lzhwxy -
How do you join two tables from different Oracle schemas using a subquery
I am trying to join two tables from different Oracle schemas using a subquery. I can extract data from each of the tables without a problem. However, when I combine the select statements using a subquery I get the Oracle error *'ORA-00936: missing expression'*. Since each SELECT statement executes on its own without error I don't understand what is missing. The result set I am trying to get is to match up the LINE_ID from PDTABLE_12_1 in schema DD_12809 with the MAT_DESCRIPTION from table PDTABLE_201 in schema RA_12809.
The query is as follows:
sql = "SELECT [DD_12809].[PDTABLE_12_1].LINE_ID FROM [DD_12809].[PDTABLE_12_1] JOIN " _
+ "(SELECT [RA_12809].[PDTABLE_201].MAT_DESCRIPTION " _
+ "FROM [RA_12809].[PDTABLE_201]) AS FAB " _
+ "ON [DD_12809].[PDTABLE_12_1].PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS"
The format of the query is copied from a SQL programming manual.
I also tried executing the query using a straight JOIN on the two tables but got the same results. Any insight would be helpful. Thanks!
Edited by: user11338343 on Oct 19, 2009 6:55 AMI believe you are receiving the error because you are trying to JOIN on a column that doesn't exist. For example you are trying to join on FAB.PIPING_MATER_CLASS but that column does not exist in the subquery.
If you want to do a straight join without a subquery you could do the following
SELECT DD_12809.PDTABLE_12_1.LINE_ID
, FAB.MAT_DESCRIPTION
FROM DD_12809.PDTABLE_12_1
JOIN RA_12809.PDTABLE_201 AS FAB ON DD_12809.PDTABLE_12_1.PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS HTH! -
Hi,
I have a query that joins two tables in the same database, the result needs to be loaded in a destination DB table. How do I do this in SSIS package?
thank you !
Thank You Warmest Fanny PiedPlease take a look at these links related to your query.
http://stackoverflow.com/questions/5145637/querying-data-by-joining-two-tables-in-two-database-on-different-servers
http://stackoverflow.com/questions/7037228/joining-two-tables-together-in-one-database -
How to join two tables such as union all
We have two tables ,there has the same structure,one is the recent Table,the other is a history Table。How can i get the reasult from the two tables .
I know we can create the table using table type=select and write the SQL.
I want to know another way such as fragmentaion content,But i don`t know how to implement.
Who can give the step by step .
Thank you very much.I set the wrong case .my question has been answered..
Thank you ~
Edited by: Richard 1982 on 2009-12-27 下午9:55 -
Using a view to join two tables
Thank you in advance for any advice you can lend.
I am using this code in my MySQL db to create a view.
select
job.id as job_id,
umr_cost_calculation.plant_name,
max(umr_cost_calculation.id) as max_id
from
job,
umr_cost_calculation
where
job.id = umr_cost_calculation.job_id
group by job.id , umr_cost_calculation.plant_name
I did this so I can join two tables and pull in the most current cost data for a specific plant. The report will, at times, show the wrong (older) data. I can re-run the report, filter to just the one job and see again the wrong data. When I add the max_id to the report, it display the id and updates the report with the correct data. It appears that the view was stale and by adding the ID to the report this fixed the issue.
1) Is this the best way to make this join? I don't see how Crystal supports a subquery to make a join (this is why I used the view).
2) If I leave the max_id on the report, will this force the view to always update?Try:
Select
D1.EmpLoginID,
Count(D1.ID),
Count(D1.AlarmCode),
D1.EmpName,
D1.EmpAddress,
D2.Db2Count
FROM DB1.Data D1
LEFT JOIN (SELECT
empLoginID, Count(*) as Db2Count
FROM DB2.ALL_Database
WHERE site = 'Atlanta'
GROUP BY empLoginID
) D2
ON D1.EmpLoginID = D2.EmpLoginID
GROUP BY D1.empLoginID, D1.EmpName, D2.EmpAddress, D2.Db2Count
Order BY D1.empLoginID ASC
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
Joining two tables, sql query
This is a newbie question! I would like to join two tables. Table_1 contains xml stylesheets:
id stylesheet doc
1 <xml stylesheet doc A>
2 <xml stylesheet doc B>
And Table_2 contains the XML documents that the stylesheets will transform:
id XML doc
1 <XML document 1>
1 <XML document 2>
1 <XML document 3>
2 <XML document 4>
2 <XML document 5>
I would like <xml stylesheet doc A> to transform only XML doc that have an id of 1, so I tried this sql statement:
select a.stylesheet_doc ,b.xml_doc from Table_1 a, Table_2 b where a.id=b.id and a.id=1;
This statement returns the rows I want (stylesheet doc with id equals 1, and xml_doc with id equals 1), but it pairs each xml document with a style sheet.
stylesheet doc A <XML document 1>
stylesheet doc A <XML document 2>
stylesheet doc A <XML document 3>
My question is, is there a way to have a result that looks like this?
stylesheet doc A
<XML document 1>
<XML document 2>
<XML document 3>
That is, is there a way in sql to get rid of duplicate stylesheet doc A?
I have tried group by and rollup and xmlagg.
Thank you very, very much for your help.
JimHi, Jim,
Welcome to the forum!
You just want to display the XML, not actually transform it, right?
GROUP BY ROLLUP should work, but I find it easier with GROUP BY GROUPING SETS. Here's an example from tables in the scott schema:
SELECT CASE
WHEN GROUPING (ename) = 1
THEN d.dname
END AS dname
, e.ename
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
GROUP BY GROUPING SETS ( (d.dname, e.ename)
, (d.dname)
ORDER BY d.dname
, ename NULLS FIRST
;Output:
DNAME ENAME
ACCOUNTING
CLARK
KING
MILLER
RESEARCH
ADAMS
FORD
JONES
SCOTT
SMITH
SALES
ALLEN
BLAKE
JAMES
MARTIN
TURNER
WARDYou may have noticed that this site noramlly compresses whitespace.
Whenever you post formatted text (such as query results) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing. -
I need a report that will join two tables BKPF and VBRK in QuickViewer. The only field with the right character length is XBLNR but when creating the join no records are dispalyed beacuse the values of this field in both tables are not the same.
There are also other fields available in BKPF but the char length is not the appropriate.
Does anyone have any idea how I can link these two tables?
Thank youNeither of these combinations is possible with QuickViewer because the fields need to have the same length of characters in order to create the join.
VBRK - VBLEN char(10) and BKPF - AWKEY char(20)
VBRK -ZUONR char(18) BKPF- BELNR char (10)
Any other idea?
Thank you, JP -
Join two table (Inner Join)
how to join two tables using inner join.
Tariq,
Pretty vague question. You can create joins in an ABAP program, or while creating a view, or when creating a SAP query of one type or another. Some people download tables and then join them using desktop software. If you can elaborate your question I may be able to give you a better answer.
I recently joined two wooden tables at a picnic. I used 24 gauge galvanized wire combined with duct tape, so I guess you couldn't really call that an 'inner join'.
Best Regards,
DB49
Maybe you are looking for
-
Data vanishing in new fields after request activation in ODS
Hi, I have added some new fields in ODS and updated it by end routine .Now when i run the dtp and check the data by pressing NEW DATA tab then i am able to see data updated in the new fields. When i tried to activate that request i
-
Whenever I sign into safari the webpage will show a static blue, black, and white screen for a few seconds. What is this? They said it was because I needed to update my software, well I have done that and it is still doing it. Any recommendations?
-
My mac book keeps dropping the WEP password
My mac book keeps dropping the WEP password. I have a netgear wireless router and all other computers are running fine on the network just the macbook seems to be dropping the WEP. any thoughts?
-
How to generate the hash value of an user password? thanks.
same as subject
-
Why does my Apple wireless mouse suddenly need batteries changed every week?
I'm getting "running low on batteries" messages regularly now. This never happened before. The mouse is about five years old.