SQL Query to Join by Comma - Help an assistance
Hi,
Thanks for your help in advance.
Requirement is as follows.
Table1
=======
Col1 Col2
============
1 John
2 Jocky
3 Silk
Table2
========
Col1 Col2
=========
1 John, Marry, Joseph
2 Silk, David
3 Jocky, Prem
I need an sql query where the join condition has to satisfy as follows
Table1.col2=Table2.Col2 (If any of the col2 in table1 matches with Table2.Col2 i need to return the record).
How to do this.
Please guide me on this.
nazzu wrote:
Thanks a lot for your response ...
But if a table1 and table2 had different numbers of rows then ?
I may be behaving like a fool ... but my requrirement is such a fashion.
Please help ...
Thanks again for your help.
Edited by: nazzu on Mar 14, 2013 8:22 AMHi,
try to be clear in your requirement. You mentioned in your initial question:
I need an sql query where the join condition has to satisfy as followsWhat happen if you have different number of rows in table1 and table2?
Rows from table2 will be returned for any match in table1 if you are using a normal join. However if you have 2 rows in table1 that are matching table2 then that row in table2 will return twice.
Let me just show you an example:
WITH table1(col1, col2) AS
SELECT 1, 'John' FROM DUAL UNION ALL
SELECT 2, 'Jocky' FROM DUAL UNION ALL
SELECT 3, 'Silk' FROM DUAL UNION ALL
SELECT 4, 'Marry' FROM DUAL
, table2(col1, col2) AS
SELECT 1, 'John, Marry, Joseph' FROM DUAL UNION ALL
SELECT 2, 'Silk, David' FROM DUAL UNION ALL
SELECT 3, 'Jocky, Prem' FROM DUAL
SELECT t2.col1, t2.col2, t1.col2
FROM table2 t2
JOIN table1 t1
ON(INSTR(', '||t2.col2||',', ', '||t1.col2||',')>0);
COL1 COL2 COL2_1
1 John, Marry, Joseph John
1 John, Marry, Joseph Marry
2 Silk, David Silk
3 Jocky, Prem Jocky As you can see the first row from table2 is returned twice because it matches 2 records on table1.
In case you want to display only rows from table2 which has a match in table1 you can either do a distinct:
SELECT DISTINCT t2.col1, t2.col2
FROM table2 t2
JOIN table1 t1
ON(INSTR(', '||t2.col2||',', ', '||t1.col2||',')>0);
COL1 COL2
1 John, Marry, Joseph
2 Silk, David
3 Jocky, Prem or use EXISTS operator:
SELECT t2.col1, t2.col2
FROM table2 t2
WHERE EXISTS (SELECT 1
FROM table1 t1
WHERE INSTR(', '||t2.col2||',', ', '||t1.col2||',')>0
COL1 COL2
1 John, Marry, Joseph
2 Silk, David
3 Jocky, Prem Regards.
Al
Similar Messages
-
Hi,
I would like to konw how I can make multiple parameters to sql query string seperated by ,(comma) ..
For example, this parameters can be printed like 'abc,dde,ggf,eeg' ,once I use "join(Parameters!rpCode.Value,",")" with report builder ,
By the way, when I test this multiple parameters by Query Designer of report builder there was no problem,.(using Define query parameters, I put abc,dde,ggf,eeg)
however, when I run this report , it won't be executing , with (rsErrorExecutingCommand)
Plz, help me....If its sql query then it should like this
Select t.*
from table t
inner join dbo.ParseValues(@Parameters,',')f
on f.val = t.ID
ParseValues can be found him
http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html
or easier way is
Select t.*
from table t
where ',' + @Parameters + ',' LIKE '%,' + CAST(t.ID AS varchar(10)) + ',%'
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
SQL query -- self-join question?
SQL> l
1* select originator,destination,oaddress,daddress from (select * from activity where rownum<=3)
SQL> /
10099 10004 16196344392 16199375530
10064 10002 18454644069 18456563415
10065 10006 18302650166 16416609306
looking at the above query, i am just performing a simple select from one of my tables. Now i require the carriername for both originator and destination columns, and the names for these are found on another table carrier.
so, i am re-write the above query as: (join with carrier table)
SQL>
SQL> select originator,destination,oaddress,daddress,carriername from (select * from activity,carrier where originator=carrier_code and rownum<=3);
10006 10099 19182772772 19189553062 USA1
10004 10311 15096701636 15096692171 USA2
10000 10003 15125898141 15122930569 USA3
Now, i got the carrier name for my originator, how would i find the carriername for my destination also (in the same query). One way of doing it is joining the carrier table twice, but is there any other better approach.
Hope i am clear, any help will be greatly appreciated. Thanks.select
A.originator, A.destination, A.oaddress, A.daddress
, B.carrier_name name_originator
, C.carrier_name name_destination
from
activity A
,carrier B
,carrier C
where
A.originator = B.carrier_code
and A.destination = C.carrier_code -
XML Generation using a sql query in an efficient way -Help needed urgently
Hi
I am facing the following issue while generating xml using an sql query. I get the below given table using a query.
CODE ID MARK
==================================
1 4 2331 809
2 4 1772 802
3 4 2331 845
4 5 2331 804
5 5 2331 800
6 5 2210 801
I need to generate the below given xml using a query
<data>
<CODE>4</CODE>
<IDS>
<ID>2331</ID>
<ID>1772</ID>
</IDS>
<MARKS>
<MARK>809</MARK>
<MARK>802</MARK>
<MARK>845</MARK>
</MARKS>
</data>
<data>
<CODE>5</CODE>
<IDS>
<ID>2331</ID>
<ID>2210</ID>
</IDS>
<MARKS>
<MARK>804</MARK>
<MARK>800</MARK>
<MARK>801</MARK>
</MARKS>
</data>
Can anyone help me with some idea to generate the above given CLOB messagenot sure if this is the right way to do it but
/* Formatted on 10/12/2011 12:52:28 PM (QP5 v5.149.1003.31008) */
WITH data AS (SELECT 4 code, 2331 id, 809 mark FROM DUAL
UNION
SELECT 4, 1772, 802 FROM DUAL
UNION
SELECT 4, 2331, 845 FROM DUAL
UNION
SELECT 5, 2331, 804 FROM DUAL
UNION
SELECT 5, 2331, 800 FROM DUAL
UNION
SELECT 5, 2210, 801 FROM DUAL)
SELECT TO_CLOB (
'<DATA>'
|| listagg (xml, '</DATA><DATA>') WITHIN GROUP (ORDER BY xml)
|| '</DATA>')
xml
FROM ( SELECT '<CODE>'
|| code
|| '</CODE><IDS><ID>'
|| LISTAGG (id, '</ID><ID>') WITHIN GROUP (ORDER BY id)
|| '</ID><IDS><MARKS><MARK>'
|| LISTAGG (mark, '</MARK><MARK>') WITHIN GROUP (ORDER BY id)
|| '</MARK></MARKS>'
xml
FROM data
GROUP BY code) -
Get column names into a VB program, for an SQL query that joins tables
use [mydatabase]
select * from InvNum inum
left join _btblInvoiceLines ilin
on inum.autoindex = ilin.iinvoiceid
left join stkitem s
on ilin.istockcodeid = s.stocklink where InvNumber = 'IVQ0834' or InvNumber = 'IVQ0835'
I need to get out the column names in a VB program in the same order they would appear in the SQL query above...
I want to use something like:
select column_name from information_schema.columns where TABLE_NAME = ....
except there are several tables involved above so I dont know the format!If you execute the query in your program using the SqlDataReader.ExecuteReader method, the column names will be available via the GetName method of the returned reader. But note that your query may return multiple columns with the same name.
Dim reader As SqlDataReader = command.ExecuteReader()
For x = 0 To reader.FieldCount - 1
Console.WriteLine(reader.GetName(x))
Next x
Dan Guzman, SQL Server MVP, http://www.dbdelta.com -
Hi,
I am trying to join two tables using joining.
SQL> select * from test10;
ID
1
2
SQL> select * from test11;
ID
1
3
4
My expected output is
ID
1
2
3
4
I am able to do this using "Union"
SQL> select * from test10
2 union
3 select * from test11;
ID
1
2
3
4
But i want to achieve this using join. Please help..
Thanks & Regards,
ArijitHi, Arijit,
The way to do that with a join is FULL OUTER JOIN, like this:
SELECT NVL (t10.id, t11.id) AS id
FROM test10 t10
FULL OUTER JOIN test11 t11 ON t10.id = t11.id
ORDER BY 1
;If that's exactly what you're trying to do, you should stick with the UNION.
FULL OUTER JOIN is good when there are other columns involved. -
Hi,
I want to write a SQL statement using Joins. The base table "A" has about 100 fields and the join table "B" a few. I would like to fetch all fields from table "A" and a few from table "B".
Is there a way to write a query wherein I can use "Select * " for table "A" rather than writing all 100 field name in the query? Please keep in mind that I want to use a Join.
Thanks.Hi Thomas,
but if there are some fields which are in both tables, SELECT * will be not sufficient. E.g. fields ERNAM, ERDAT, AENAM, AEDAT are such fields which can occur in more tables. In case that a target area will contain these fields, by * with addition INTO CORRESPONDING FIELDS OF, you are not able to specify from which table these values should be taken. In such case, it can be solved by specifying field list dynamically. If it is not that case SELECT * is sufficient. You should check intersection of common fields for tables you are joining. Thereafter you should decide if you can use SELECT *.
Maybe for better understanding, what exactly I am talking about, look at this example:
TYPES: BEGIN OF ts_data,
ernam TYPE mara-ernam,
END OF ts_data.
DATA: ls_data TYPE ts_data,
ls_mara TYPE mara,
ls_mchb TYPE mchb.
START-OF-SELECTION.
ls_mara-matnr = '1'.
ls_mchb-matnr = ls_mara-matnr.
ls_mara-ernam = 'A'.
ls_mchb-ernam = 'B'.
INSERT mara FROM ls_mara.
INSERT mchb FROM ls_mchb.
SELECT SINGLE * FROM mara INNER JOIN mchb ON mara~matnr = mchb~matnr INTO CORRESPONDING FIELDS OF ls_data.
ROLLBACK WORK.
You as a developer are not able to tell from which table common fields should be selected. But maybe this is really too specific case...
Regards,
Adrian -
SQL query....need help again
Actor (Aname: varchar(40), Ano: varchar(6)) Ano is pk
Movie (Mname: varchar(40),mno: varchar(8)) Mno is pk
PlayIn (Ano, Mno, Pay: Integer) Ano and Mno are fk referencing Acotr and Movie respectively.
Actor(Aname, Ano)
A Bingo, A1
B Castro, A2
C Katie, A3
S Hommy, A4
J Tammy, A5
K loren, A6
Movie(Mname, Mno)
Gladiator, M1
Cast, M2
Dog, M3
Jilters, M4
PlayIn(Ano,Mno,Pay)
A1 M1 800
A1 M2 1500
A2 M2 78
A2 M3 1750
A2 M4 2301
A3 M2 904
A3 M3 629
A4 M2 565
A4 M3 5695
A4 M4 1255
A5 M1 989
A5 M4 238
A6 M2 137
A6 M3 236
A6 M4 545
write a query for this QUESTION:
for each movie, list the movie number, the average pay and the total number of actors in the movie.....hi - didn't you read my response to your other nearly identical thread?
SQL query...pls help ASAP
may I suggest you get a basic book on oracle SQL and immerse yourself for a few hours before comming to this forum for answers to your homework questions? -
SQL Query running really slow, any help in improving will be Great!
Hi,
I am really new to this performance tuning and optimization techniques. Explain plan also, I only have theoretical knowledge, no clues on how to find out the real issue which is making the query slow..So if anyone can give me a good direction on where to start even, it will be great..
Now, my issue is, I have a query which runs really really slow. If I run this query for a small subset of data, it runs fast(Its flying, actually..) but if I give the same query for everything(Full required data), its running for ages..(Actually it is running for 2 days now and still running.)
I am pasting my query here, the output shows that the query stucks after "Table created"
SQL> @routinginfo
Table dropped.
Table created.
Please please help!
I also ran explain plan for this query and there are a number of rows in the plan_table now..
SORRY!IS there a way to insert a file here, as I want to attach my explain plan also?
My query -Routinginfo.sql
set trimspool on
set heading on
set verify on
set serveroutput on
drop table routinginfo;
CREATE TABLE routinginfo
( POST_TOWN_NAME VARCHAR2(22 BYTE),
DELIVERY_OFFICE_NAME VARCHAR2(40 BYTE),
ROUTE_ID NUMBER(10),
ROUTE_NAME VARCHAR2(40 BYTE),
BUILDING_ID NUMBER(10),
SUB_BUILDING_ID NUMBER(10),
SEQUENCE_NO NUMBER(4),
PERSONAL_NAME VARCHAR2(60 BYTE),
ADDRESS VARCHAR2(1004 BYTE),
BUILDING_USE VARCHAR2(1 BYTE),
COMMENTS VARCHAR2(200 BYTE),
EAST NUMBER(17,5),
NORTH NUMBER(17,5)
insert into routinginfo
(post_town_name,delivery_office_name,route_id,route_name,
building_id,sub_building_id,sequence_no,personal_name,
address,building_use,comments,east,north)
select
p.name,
d.name,
b.route_id,
r.name,
b.building_id,
s.sub_build_id,
b.sequence_no,
b.personal_name,
ad.addr_line_1||' '||ad.addr_line_2||' '||ad.addr_line_3||' '||ad.addr_line_4||' '||ad.addr_line_5,
b.building_use,
rtrim(replace(b.comments,chr(10),'')),
b.east,
b.north
from t_buildings b,
(select * from t_sub_buildings where nvl(invalid,'N') = 'N') s,
t_routes r,
t_delivery_offices d,
t_post_towns p,
t_address_model ad
where b.building_id = s.building_id(+)
and s.building_id is null
and r.route_id=b.route_id
and (nvl(b.residential_delivery_points,0) > 0 OR nvl(b.commercial_delivery_points,0) > 0)
and r.delivery_office_id=d.delivery_office_id
--and r.delivery_office_id=303
and D.POST_TOWN_ID=P.post_town_id
and ad.building_id=b.building_id
and ad.sub_building_id is null
and nvl(b.invalid, 'N') = 'N'
and nvl(b.derelict, 'N') = 'N'
union
select
p.name,
d.name ,
b.route_id ,
r.name ,
b.building_id ,
s.sub_build_id ,
NVL(s.sequence_no,b.sequence_no),
b.personal_name ,
ad.addr_line_1||' '||ad.addr_line_2||' '||ad.addr_line_3||' '||ad.addr_line_4||' '||ad.addr_line_5,
b.building_use,
rtrim(replace(b.comments,chr(10),'')),
b.east,
b.north
from t_buildings b,
(select * from t_sub_buildings where nvl(invalid,'N') = 'N') s,
t_routes r,
t_delivery_offices d,
t_post_towns p,
t_address_model ad
where s.building_id = b.building_id
and r.route_id = s.route_id
and (nvl(b.residential_delivery_points,0) > 0 OR nvl(b.commercial_delivery_points,0) > 0)
and r.delivery_office_id=d.delivery_office_id
--and r.delivery_office_id=303
and D.POST_TOWN_ID=P.post_town_id
and ad.building_id=b.building_id
and ad.sub_building_id = s.sub_build_id
and nvl(b.invalid, 'N') = 'N'
and nvl(b.derelict, 'N') = 'N'
union
select
p.name,
d.name,
b.route_id ,
r.name ,
b.building_id,
s.sub_build_id ,
NVL(s.sequence_no,b.sequence_no) ,
b.personal_name ,
ad.addr_line_1||' '||ad.addr_line_2||' '||ad.addr_line_3||' '||ad.addr_line_4||' '||ad.addr_line_5 ,
b.building_use,
rtrim(replace(b.comments,chr(10),'')),
b.east,
b.north
from t_buildings b,
(select * from t_sub_buildings where nvl(invalid,'N') = 'N') s,
t_routes r,
t_delivery_offices d,
t_post_towns p,
t_localities l,
t_localities lo,
t_localities loc,
t_tlands tl,
t_address_model ad
where s.building_id = b.building_id
and s.route_id is null
and r.route_id = b.route_id
and (nvl(b.residential_delivery_points,0) > 0 OR nvl(b.commercial_delivery_points,0) > 0)
and r.delivery_office_id=d.delivery_office_id
--and r.delivery_office_id=303
and D.POST_TOWN_ID=P.post_town_id
and ad.building_id=b.building_id
and ad.sub_building_id = s.sub_build_id
and nvl(b.invalid, 'N') = 'N'
and nvl(b.derelict, 'N') = 'N';
commit; Edited by: Krithi on 16-Jun-2009 01:48
Edited by: Krithi on 16-Jun-2009 01:51
Edited by: Krithi on 16-Jun-2009 02:44This link is helpful alright..but as a beginner, it is taking me too long to understand..But I am going to learn the techniques for sure..
Fo the time being,I am pasting my explain plan for the above query here, so that I hope any expert can really help me on this one..
STATEMENT_ID TIMESTAMP REMARKS OPERATION OPTIONS OBJECT_NODE OBJECT_OWNER OBJECT_NAME OBJECT_INSTANCE OBJECT_TYPE OPTIMIZER SEARCH_COLUMNS ID PARENT_ID POSITION COST CARDINALITY BYTES
06/16/2009 09:33:01 SELECT STATEMENT CHOOSE 0 829,387,159,200 829,387,159,200 3,720,524,291,654,720 703,179,091,122,042,000
06/16/2009 09:33:01 SORT UNIQUE 1 0 1 829,387,159,200 3,720,524,291,654,720 703,179,091,122,042,000
06/16/2009 09:33:01 UNION-ALL 2 1 1
06/16/2009 09:33:01 HASH JOIN 3 2 1 11,209 87,591 15,853,971
06/16/2009 09:33:01 FILTER 4 3 1
06/16/2009 09:33:01 HASH JOIN OUTER 5 4 1
06/16/2009 09:33:01 HASH JOIN 6 5 1 5,299 59,325 6,585,075
06/16/2009 09:33:01 VIEW GEO2 index$_join$_006 6 7 6 1 4 128 1,792
06/16/2009 09:33:01 HASH JOIN 8 7 1 5,299 59,325 6,585,075
06/16/2009 09:33:01 INDEX FAST FULL SCAN GEO2 POST_TOWN_NAME_I NON-UNIQUE ANALYZED 9 8 1 1 128 1,792
06/16/2009 09:33:01 INDEX FAST FULL SCAN GEO2 POST_TOWN_PK UNIQUE ANALYZED 10 8 2 1 128 1,792
06/16/2009 09:33:01 HASH JOIN 11 6 2 5,294 59,325 5,754,525
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_DELIVERY_OFFICES 5 ANALYZED 12 11 1 7 586 10,548
06/16/2009 09:33:01 HASH JOIN 13 11 2 5,284 59,325 4,686,675
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_ROUTES 4 ANALYZED 14 13 1 7 4,247 118,916
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_BUILDINGS 1 ANALYZED 15 13 2 5,265 59,408 3,029,808
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_SUB_BUILDINGS 3 ANALYZED 16 5 2 851 278,442 3,898,188
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_ADDRESS_MODEL 7 ANALYZED 17 3 2 3,034 1,582,421 88,615,576
06/16/2009 09:33:01 NESTED LOOPS 18 2 2 10,217 1 189
06/16/2009 09:33:01 NESTED LOOPS 19 18 1 10,216 1 175
06/16/2009 09:33:01 HASH JOIN 20 19 1 10,215 1 157
06/16/2009 09:33:01 HASH JOIN 21 20 1 6,467 80,873 8,168,173
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_ROUTES 11 ANALYZED 22 21 1 7 4,247 118,916
06/16/2009 09:33:01 HASH JOIN 23 21 2 6,440 80,924 5,907,452
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_BUILDINGS 8 ANALYZED 24 23 1 5,265 59,408 3,029,808
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_SUB_BUILDINGS 10 ANALYZED 25 23 2 851 278,442 6,125,724
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_ADDRESS_MODEL 14 ANALYZED 26 20 2 3,034 556,000 31,136,000
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_DELIVERY_OFFICES 12 ANALYZED 27 19 2 1 1 18
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 DELIVERY_OFFICE_PK UNIQUE ANALYZED 1 28 27 1 1
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_POST_TOWNS 13 ANALYZED 29 18 2 1 1 14
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 POST_TOWN_PK UNIQUE ANALYZED 1 30 29 1 1
06/16/2009 09:33:01 MERGE JOIN CARTESIAN 31 2 3 806,976,583,802 3,720,524,291,567,130 703,179,091,106,188,000
06/16/2009 09:33:01 MERGE JOIN CARTESIAN 32 31 1 16,902,296 73,359,971,046 13,865,034,527,694
06/16/2009 09:33:01 MERGE JOIN CARTESIAN 33 32 1 1,860 1,207,174 228,155,886
06/16/2009 09:33:01 MERGE JOIN CARTESIAN 34 33 1 1,580 20 3,780
06/16/2009 09:33:01 NESTED LOOPS 35 34 1 1,566 1 189
06/16/2009 09:33:01 NESTED LOOPS 36 35 1 1,565 1 175
06/16/2009 09:33:01 NESTED LOOPS 37 36 1 1,564 1 157
06/16/2009 09:33:01 NESTED LOOPS 38 37 1 1,563 1 129
06/16/2009 09:33:01 NESTED LOOPS 39 38 1 1,207 178 12,994
06/16/2009 09:33:01 TABLE ACCESS FULL GEO2 T_SUB_BUILDINGS 17 ANALYZED 40 39 1 851 178 3,916
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_BUILDINGS 15 ANALYZED 41 39 2 2 1 51
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 BUILDING_PK UNIQUE ANALYZED 1 42 41 1 1 31
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_ADDRESS_MODEL 25 ANALYZED 43 38 2 2 1 56
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 MODEL_MODEL2_UK UNIQUE ANALYZED 2 44 43 1 1 1
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_ROUTES 18 ANALYZED 45 37 2 1 1 28
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 ROUTE_PK UNIQUE ANALYZED 1 46 45 1 1
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_DELIVERY_OFFICES 19 ANALYZED 47 36 2 1 1 18
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 DELIVERY_OFFICE_PK UNIQUE ANALYZED 1 48 47 1 1
06/16/2009 09:33:01 TABLE ACCESS BY INDEX ROWID GEO2 T_POST_TOWNS 20 ANALYZED 49 35 2 1 1 14
06/16/2009 09:33:01 INDEX UNIQUE SCAN GEO2 POST_TOWN_PK UNIQUE ANALYZED 1 50 49 1 1
06/16/2009 09:33:01 BUFFER SORT 51 34 2 1,579 60,770
06/16/2009 09:33:01 INDEX FAST FULL SCAN GEO2 LOCAL_COUNTY_FK_I NON-UNIQUE ANALYZED 52 51 1 14 60,770
06/16/2009 09:33:01 BUFFER SORT 53 33 2 1,846 60,770
06/16/2009 09:33:01 INDEX FAST FULL SCAN GEO2 LOCAL_COUNTY_FK_I NON-UNIQUE ANALYZED 54 53 1 14 60,770
06/16/2009 09:33:01 BUFFER SORT 55 32 2 16,902,282 60,770
06/16/2009 09:33:01 INDEX FAST FULL SCAN GEO2 LOCAL_COUNTY_FK_I NON-UNIQUE ANALYZED 56 55 1 14 60,770
06/16/2009 09:33:01 BUFFER SORT 57 31 2 806,976,583,788 50,716
06/16/2009 09:33:01 INDEX FAST FULL SCAN GEO2 TLAND_COUNTY_FK_I NON-UNIQUE ANALYZED 58 57 1 11 50,716 -------------------------------------------------------------
Edited by: Krithi on 16-Jun-2009 02:47 -
SQL query for join table and multiple values
Trying to join two tables , Emphours and EmpStatus to get
result which gives each emplyees hour
worked each day
in past say 1 year in what status. I need result similar to table 3 , Hours Can also be grouped per week
all I need Is Each employees hours in each week and his status and position at that time if possible
any help will be highly appreciated. Thank you
note: payday is every other Friday- week runs from Saturday through Friday
EmpStatus Table tracks when employees status changed
EmpHours
employee
workday
payday
hours
position
101
1/1/2014
1/3/2014
8
assistant
101
1/3/2014
1/3/2014
8
assistant
101
1/4/2014
1/17/2014
8
assistant
101
1/5/2014
1/17/2014
8
assistant
101
1/7/2014
1/17/2014
8
assistant
101
1/8/2014
1/17/2014
8
assistant
101
1/9/2014
1/17/2014
8
assistant
101
1/11/2014
1/17/2014
8
assistant
101
1/13/2014
1/17/2014
8
assistant
101
1/14/2014
1/17/2014
8
assistant
101
1/18/2014
2/14/2014
8
assistant
102
1/1/2014
1/3/2014
7
manager
102
1/25/2014
1/31/2014
7
manager
102
1/26/2014
1/31/2014
7
manager
102
1/28/2014
1/31/2014
7
manager
102
1/31/2014
1/31/2014
7
manager
103
1/1/2014
1/3/2014
5
intern
103
1/31/2014
1/31/2014
6
intern
104
1/14/2014
1/17/2014
5
supervisor
104
1/30/2014
1/31/2014
6
supervisor
EmpStatus
employee
start_date
status
101
1/1/2014
parttime
101
1/18/2014
fulltime
102
1/1/2014
seasonal
102
1/18/2014
fulltime
103
1/1/2014
partime
103
1/18/2014
fulltime
104
1/4/2014
parttime
104
1/18/2014
fulltime
Table 3
employee
status
hours
position
workday
weekend
payday
101
parttime
8
assistant
1/1/2014
1/3/2014
1/3/2014
101
parttime
8
assistant
1/3/2014
1/3/2014
1/3/2014
101
parttime
8
assistant
1/4/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/5/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/7/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/8/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/9/2014
1/10/2014
1/17/2014
101
parttime
8
assistant
1/11/2014
1/17/2014
1/17/2014
101
parttime
8
assistant
1/13/2014
1/17/2014
1/17/2014
101
parttime
8
assistant
1/14/2014
1/17/2014
1/17/2014
101
fulltime
8
assistant
1/18/2014
1/24/2014
2/14/2014
102
seasonal
7
manager
1/1/2014
1/3/2014
1/3/2014
102
fulltime
7
manager
1/25/2014
1/25/2014
2/14/2014
102
fulltime
7
manager
1/26/2014
1/26/2014
2/14/2014
102
fulltime
7
manager
1/28/2014
1/28/2014
2/14/2014
102
fulltime
7
manager
1/31/2014
1/31/2014
2/14/2014
103
parttime
5
intern
1/1/2014
1/3/2014
1/3/2014
103
fulltime
6
intern
1/31/2014
1/31/2014
2/14/2014
104
parttime
5
supervisor
1/14/2014
1/17/2014
1/17/2014
104
fulltime
6
supervisor
1/30/2014
1/31/2014
1/31/2014Hello David,
Try this query
set dateformat mdy;
declare @EmpHours table
(Employee int,workday date,payday date,hours int,position varchar(50));
insert into @EmpHours values
(101,'1/1/2014','1/3/2014',8,'assistant'),
(101,'1/3/2014','1/3/2014',8,'assistant'),
(101,'1/4/2014','1/17/2014',8,'assistant'),
(101,'1/5/2014','1/17/2014',8,'assistant'),
(101,'1/7/2014','1/17/2014',8,'assistant'),
(101,'1/8/2014','1/17/2014',8,'assistant'),
(101,'1/9/2014','1/17/2014',8,'assistant'),
(101,'1/11/2014','1/17/2014',8,'assistant'),
(101,'1/13/2014','1/17/2014',8,'assistant'),
(101,'1/14/2014','1/17/2014',8,'assistant'),
(101,'1/18/2014','2/14/2014',8,'assistant'),
(102,'1/1/2014','1/3/2014',7,'manager'),
(102,'1/25/2014','1/31/2014',7,'manager'),
(102,'1/26/2014','1/31/2014',7,'manager'),
(102,'1/28/2014','1/31/2014',7,'manager'),
(102,'1/31/2014','1/31/2014',7,'manager'),
(103,'1/1/2014','1/3/2014',5,'intern'),
(103,'1/31/2014','1/31/2014',6,'intern'),
(104,'1/14/2014','1/17/2014',5,'supervisor'),
(104,'1/30/2014','1/31/2014',6,'supervisor');
--select * from @EmpHours
declare @EmpStatus table
(employee int,start_date date,status varchar(20));
insert into @EmpStatus values
(101,'1/1/2014','parttime'),
(101,'1/18/2014','fulltime'),
(102,'1/1/2014','seasonal'),
(102,'1/18/2014','fulltime'),
(103,'1/1/2014','partime'),
(103,'1/18/2014','fulltime'),
(104,'1/4/2014','parttime'),
(104,'1/18/2014','fulltime');
WITH C AS
SELECT es.employee,es.start_date, es.status, ROW_NUMBER() OVER(partition by employee ORDER BY start_date) AS rownum
FROM @EmpStatus ES
CTE_RANGES as(
SELECT cur.employee,Cur.start_date start_range, cur.status,case when nxt.start_date is null then '2099-12-31' else dateadd(d,-1,Nxt.start_date) end AS end_range
FROM C AS Cur
left JOIN C AS Nxt
ON Nxt.rownum = Cur.rownum + 1 and cur.employee=nxt.employee)
select eh.*,es.status from @EmpHours EH join CTE_RANGES Es on EH.Employee =es.employee and EH.workday between es.start_range and es.end_range
--where es.employee=101
You will need a calender table too which can be joined to the output of the above query to get the weekend dates.
You can find the T-SQL code to generate the calender here
http://stackoverflow.com/questions/19191577/t-sql-function-to-generate-calendar-table
and posting the questions with necessary DDL , DML (like I have posted) would help us a lot.
Satheesh
My Blog -
I have a table called logon table which has some columns like userid, Firstname, lastname, email and Parent.
Typically looks like this
userid Firstname lastname email parent
101 Bob Dan [email protected] 309
102 Riz Bob [email protected] 101
309 roger Brooks [email protected] 106
310 Cavin leo [email protected] 700
106 Joe Brooks [email protected] 701
Here parent is again an User id column and this represents the managers id.
I want to write a query which would pull the data in this format.Manager id should be replaced by First Name and Last Name.
userid Firstname lastname email FirstName LastName
101 Bob Dan [email protected] roger Brooks
102 Riz Bob [email protected] Bob Dan
30 Roger Brooks [email protected] Joe Brooks
310 Cavin leo [email protected] San Leo
106 Joe Brooks [email protected] Misty June
Not sure if Self join is a solution, please help.select l1.userid, l1.Firstname, l1.lastname, l1.email, l2.Firstname mgr_Firstname, l2.lastname mgr_lastname
from logon l1,
logon l2
where l1.parent = l2.userid(+);
not tested
C. -
Opitmizing sql query using join
Hello all,
I have the following query that needs to be written using Joins. I am not sure if this is the correct place to post this question
Tables used:
1. activities: (a_id, n_id, market, amount, act_type, act_number, act_seq) --the money paid by the registrant for the market.
2. market_reg (a_id, n_id, market, reg_stage) -- for each market there are registrants
3. market (market) -- stores market details
I need to get sum of total_payment_amount using the condition. I know the query is not opitmized and its not the correct way to get info. I was
wondering if you guys can suggest me a query that will pull the information by using joins
SELECT a.a_id, a.n_id, SUM (a.total_payment_amount)
FROM activities a
WHERE a.market = 'marketname'
AND a.a_id||a.n_id IN
(SELECT mr.a_id||mr.n_id
FROM market_reg mr
WHERE mr.market = 'marketname'
AND mr.reg_stage = 'P'
AND mr.n_id <> 0)
AND (a.act_type = 'A'
OR (a.act_type IS NULL
AND a.act_number||act_seq IN
( SELECT a1.act_number||a1.act_seq
FROM activities a1
WHERE a1.market = 'marketname'
GROUP BY a1.act_number||a1.act_seq
HAVING COUNT (a1.act_number||a1.act_seq) = 1)))
GROUP BY a.a_id, a.n_id;
ThanksHi,
I see you have already re-posted this on a more appropriate forum, the SQL and PL/SQL forum.
Thanks,
Gary -
Forming generic sql query for joining multiple sap tables in ABAp
Hi,
I am new to this abap field ,facing an issue onsap-jco project . I have used RFC_READ_TABLE FM ,Customized this FM but facing an issue how to write generic open SQl select statement for joining multiple tables using RFC_READ_TABLE .Kindly help on this issue.
Thanks.something like this? If your tuples are not single columns, then you'll have to use dynamic sql to achieve the same result.with
table_1 as
(select '|Xyz|Abc|Def|' tuple from dual
table_2 as
(select '|Data1|Data21|Data31|Data41|Data51|' tuple from dual union all
select '|Data2|Data22|Data32|Data42|Data52|' tuple from dual union all
select '|Data3|Data23|Data33|Data43|Data53|' tuple from dual union all
select '|Data4|Data24|Data34|Data44|Data54|' tuple from dual union all
select '|Data5|Data25|Data35|Data45|Data55|' tuple from dual
select case the_row when 1
then tuple
else '|---|---|' || substr(tuple,instr(tuple,'|',1,3) + 1)
end tuple
from (select substr(a.tuple,instr(a.tuple,'|',:one_one),instr(a.tuple,'|',:one_one + 1)) ||
substr(a.tuple,instr(a.tuple,'|',1,:one_two) + 1,instr(a.tuple,'|',1,:one_two + 1) - instr(a.tuple,'|',1,:one_two)) ||
substr(b.tuple,instr(b.tuple,'|',1,:two_one) + 1,instr(b.tuple,'|',1,:two_one + 1) - instr(b.tuple,'|',1,:two_one)) ||
substr(b.tuple,instr(b.tuple,'|',1,:two_two) + 1,instr(b.tuple,'|',1,:two_two + 1) - instr(b.tuple,'|',1,:two_two)) tuple,
rownum the_row
from table_1 a,table_2 b
order by the_rowRegards
Etbin
Message was edited by:Etbin
user596003 -
Performance tuning in SQL query using join of views
Hi,
Am trying to tune a query of the format
select ........ from view1,view2
where view1.keyfield = view2.keyfield
The base tables of the views view1 and view2 have indexes specified on 'keyfield'.
However, when I do an explain plan of the query, Full table scan of those base tables are performed.
Even tried using Hints in this query to force Oracle to use those indexes, but still not successful.
Any pointers on how to tune this kind of query would be highly welcome.
Regards,
BaishIf your query is really of the same form as you posted, then full table scans then a hash or merge join may be the most efficient plan.
You are asking for all of the rows from view1 and the matching rows from view2, because of this, Oracle dedcided that the cost of full table scans using multi-block reads is cheaper than using single block reads to get the rowids from the index, then single block reads to get the rows from the table.
FULL SCAN <> BAD
John -
Hi..I'm having trouble wiht my query.
I'm getting way too many rows when displayed.
I should have either 12 or 24 row...I think 12, but not sure.
What I am getting is 228 rows displayed.
This is all my code, including the select statment that dsiplays rows.
--Begin Create View Casses_Schedule(2)
Drop view classes_schedule;
Create View Classes_Schedule
AS SELECT
Class_ID,
School_year,
Semester,
Course.Course_ID,
Class_Location.Class_Room,
Class_Location.Class_Building,
Title,
Starting_time,
Day,
Duration
FROM
Class, Course,
Class_Location,
Schedule_Type_Details
WHERE
semester ='SPRING'
AND
School_Year= To_Char(To_Date('1/mar/1997'))
AND
Class.Course_ID = Course.Course_ID
AND
Class.Schedule_ID = Schedule_Type_details.Schedule_ID
AND
Class.Class_Building = Class_Location.Class_Building
ORDER BY
Course.Title, Class.Class_ID;
--Query to display view data
SELECT
SubStr(Class_ID,1,7) "Class",
SubStr(School_year,1,10) "Year",
SubStr(Semester,1,10) "Semester",
SubStr(Class_Room,1,4) "Loc",
SubStr(Class_Building,1,15) "Bldng",
SubStr(Title,1,20) "title",
SubStr(Starting_time,1,6) "STime",
SubStr(Day,1,2)"Day",
SubStr(Duration,1,3) "Period"
FROM
Classes_Schedule;
Thankyou.Suggestions:
You can use "CREATE OR REPLACE VIEW" instead of "DROP VIEW" and "CREATE VIEW", although either will work.
Preface column names with table names to avoid ambiguity.
Select from the fewest tables possible. In other words, if some columns are in multiple tables, and you can get all the columns you want from one of those tables, then don't use the others; It just overly complicates things. It looks like you can get all the columns you want from the class, course, and schedule_type_details tables, so eliminate the class_location and schedule_type tables. For example, instead of joining class.schedule_id to schedule_type.schedule_id and joining schedule_type.schedule_id to schedule_type_details.schedule_id, just join class.schedule_id to schedule_type_details.schedule_id.
Make sure you have all the join conditions that you need. For example, is the course_id unique by itself? Or, does it require a combination of course_id and department_id to be unique? You may need to add a join condition for department_id. If this is what you are missing, then this
would account for having more rows than expected.
What data type is school_year? Is it VARCHAR2 or NUMBER or DATE? You have used TO_DATE to convert the VARCHAR2 string '1/mar/1997' to a DATE, then used TO_CHAR to convert it back to VARCHAR2, which seems pointless. If school_year is a DATE, then you want to convert '1/mar/1997' to a DATE for comparison. The best way to do that is to use TO_DATE and supply a date format,
to avoid any problems with implicit conversions and conflicting date formats.
Please see suggested corrected code below:
CREATE OR REPLACE VIEW classes_schedule
AS
SELECT class.class_id,
class.school_year,
class.semester,
class.course_id,
class.class_room,
class.class_building,
course.title,
schedule_type_details.starting_time,
schedule_type_details.day,
schedule_type_details.duration
FROM class,
course,
schedule_type_details
WHERE class.course_id = course.course_id
AND class.department_id = course.department_id
AND class.schedule_id = schedule_type_details.schedule_id
AND class.semester = 'SPRING'
AND class.school_year = TO_DATE ('1/mar/1997', 'dd/mon/yyyy')
ORDER BY course.title,
class.class_id
Maybe you are looking for
-
Can overrides from SCOM 2007 R2 be used in a new SCOM 2012 R2 installation?
Hi All, I'm wondering if we can "easily" use our overrides/configuration from our current SCOM 2007 R2 implementation and use them in a new SCOM 2012 R2 environment that we plan on standing up. We are using SCOM to monitor approx. 400 servers (physi
-
Error while Opening PDF attachment from Mail
Hi, We two Output Types created ZNEU and ZAUF. Two Smartforms are created for the same Output Types. The Issue now is, When ZNEU triggers and send a mail the document is properly decoded and gets opened but for ZAUF it doesn't. It says File damaged.
-
How can I watch my movies in ipad without wifi? I saw icloud but it doesn't help
I want to watch movies bought in itunes without wifi. A couple month before I could do it, but now I can't do it.
-
Color profile after calibration
I use AdobeRGB1998 color profile for monitor and Photoshop. When I have calibrate monitor I must use new color profile? Have I think right? Thanks!
-
Ok, I can't figure this one out... any help would be much appreciated. Background: I started server using the Video Production default customer profile. Then I created a few new metadata Sets including one called Finished Programs which is a media as