How to write self join in sql?
Hi,
I have table named "table_upload", column "record_type" value "01,03,04....." and start_date,end_date and so on
And i will have value for start_date,end_date only for record_type=01,rest of type these two columns will be null.
now i need to write query with self join, to include above concept. can any one please help me .
and my query look like follows,
I just want to re write
Please provide DDL+DML + expected output!
and short explanation what is it that we all ask you for:
DDL = Data Definition Language. In our case that is, CREATE TABLE statements for your tables and other definitions that are needed to understand your tables structure and there for let us to test and reproduce the problem in our server. Without DDL no one
can execute any query.
How to get DDL: Right click on the table in Object Explorer and select script table as CREATE. Post these create table scripts here.
DML = data manipulation language is a family of queries used for manipulating the data it self like: inserting, deleting and updating data. In our case we need some sample data in order to check the query and get result, so we need some indert query for
sample data.
If you post a "create table query" for the tables and "insert <table> query" with some sample, then we could help you without Assuming/Guessing. There is a reason that DDL is generally asked for and expected when discussing query problems - it helps
to identify issues, clarify terminology and prevent incorrect assumptions. Sample data also provides a common point of reference for the discussion. A script that can be used to illustrate or reproduce the issue you have, will encourage others to help.
[Personal Site] [Blog] [Facebook]
Similar Messages
-
How to resolve Self Joins in OBIEE
Hi All
Could any one help how to resolve the Self Join relations in OBIEE (ex. "SCOTT.EMP" table has "EMPNO" column which is referenced by "MGR" column in the same table.
Request:
Need to drill down from the Top manager to his employees and so on
Another example can be applied for reporting the Organisation ChartThis is a great article that shows ho to do the Alias:
Adding an Alias table within the BI physical layer using Oracle BI Administration Tool
http://www.mandsconsulting.com/adding-an-alias-table-within-oracle-bi-administration-tool
Thanks
Subra -
How to write workflow rules/expressions/SQL
Hi all.
We are recent users of CRM onDemand, we would like to write some workflow rules but cannot seem to find any information on how to do this. The help section is limited in answers, and some of the questions here are also not specific to what we would like to do.
Does anyone have any advice on books, web sites, etc. on where to find the knowledge we need on how to write the workflow rules and use SQL and expressions in CRM on Demand?
Thanks in advance!!For your first case:
1. You cannot create a workflow in r15 for activity but in R16 you can
Create new to create a workflow
Name: Auto email send
record type: activity
trigger: when new record saved
rules: put your rules by clicking on the fx icon i.e this is where you are entering the rule saying the owner is different from the person who is creating the activity or the delegated field is filled (another way of looking at it)
then save the workflow
In the actions section
choose the action "Create Email Notification"
In the To: Section choose "Relative User on Record" and Choose "Activity Owner"
Save it. Make sure both the workflow and activity are marked active.
2. In your second scenario:
How do you expect to create an account and automatically populate the Sales rep's name - not sure
But you can create an opportunity and auto fill with the Account Owner's name by using this in the field validation (you do not need the workflow), you can use the FieldValueJoin function.
Hope this helps - btb there is no book you can go thro the help and you will find some directions -
Hi
Assume I have following table
Id Code
1 aa
1 bb
1 cc
2 bb
2 cc
The records will be grouped by Id. I want to find out group containing code say 'aa' and 'bb'.
Is it possible to obtain the desired result in a single query without using Self join?
Regards
~PravinThis is a little clumsy....Anyway try it:
WITH t AS
(SELECT 1 id,'aa' code FROM dual
UNION ALL
SELECT 1,'bb' FROM dual
UNION ALL
SELECT 1,'cc' FROM dual
UNION ALL
SELECT 2,'bb' FROM dual
UNION ALL
SELECT 2,'cc' FROM dual)
SELECT id FROM t
WHERE code IN('aa', 'bb')
GROUP BY id HAVING COUNT(*) = 2; -
How to write table name in SQL according to the user input
hi
i want to select data from a table , and the table name, field name is input by the user, for example, user input table name T1, field name F1 and i want to construct a SQL like select F1 into a_var from T1 . how does the SQL statement written? thanks for your advice.hi
for ur requirement u should go for field sysmbols...
dynamic internal tables..
u can chk this....
TYPE-POOLS : slis.
PARAMETERS : p_struc LIKE dd02l-tabname.
DATA : it_fieldcat TYPE lvc_t_fcat,
t_output TYPE REF TO data .
FIELD-SYMBOLS: <t_output> TYPE table ,
<wa_output> TYPE ANY,
<fval> TYPE ANY.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_BUFFER_ACTIVE = I_BUFFER_ACTIVE
i_structure_name = p_struc
I_CLIENT_NEVER_DISPLAY = 'X'
I_BYPASSING_BUFFER = I_BYPASSING_BUFFER
I_INTERNAL_TABNAME = I_INTERNAL_TABNAME
CHANGING
ct_fieldcat = it_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
IF sy-subrc <> 0.
MESSAGE i000(bctrain) WITH 'MERGE FAILED'.
*ELSE.
MESSAGE i000(bctrain) WITH 'MERGE SUCCESS'.
ENDIF.
CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
ASSIGN t_newline->* TO <fs_dyntable>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
I_STYLE_TABLE =
it_fieldcatalog = it_fieldcat
I_LENGTH_IN_BYTE =
IMPORTING
ep_table = t_output
E_STYLE_FNAME =
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ASSIGN t_output->* TO <t_output>.
SELECT * up to 10 rows FROM
(p_struc) INTO
CORRESPONDING FIELDS OF
TABLE <t_output>.
*write : / 'dynamic table created'.
LOOP AT <t_output> ASSIGNING <wa_output>.
DO.
ASSIGN component sy-index of structure <wa_output> to <fval>.
IF sy-subrc <> 0.
WRITE : /.
EXIT.
ELSE.
WRITE : <fval>.
ENDIF.
ENDDO.
ENDLOOP.
Message was edited by:
Premalatha G -
How to write exact query from sql to oracle?
Hi all,
hope doing well,
sir i am using one query in sql that is
declare @earlyleavers varchar(max), @earlyleavers1 int
select @earlyleavers1 = DATEDIFF(Minute,'1900/01/01 10:00:00.000','1900/01/01 11:00:00.000')
select @earlyleavers = CONVERT(char(8),DATEADD(n,@earlyleavers1,0),108)
print @earlyleavers
and i am getting the result like this
01:00:00
how to get the same result in oracle
please help me.
thanks in advance.952646 wrote:
sir i need result in this format 01:00 not in 1 format.The name is Billy and not "sir". :-)
Is "01:00" a string format or an elapsed time format?
The number that the above code returns is decimal hours. So a number value of 1.5 means 1h30m or 1:30 or however you need to render that.
And that is the question. Is this a rendering issue? Must the result be rendered in a specific format?
If so, then this is not a SQL or PL/SQL issue. The server returning decimal hours is acceptable. The client (e.g. C#/Java code) needs to decide how to format and render this meaningful value. Keep in mind that if the sever code returns "1.30" or "1:30" for example, these are string/text data and pretty much meaningless. And it does not make sense for server code to return meaningless text strings to the client.
If the client expects a duration and not a numeric data type, then the server code should return a value of the INTERVAL DAY TO SECOND data type.
This data type specifically exists to contain duration values between to dates or times.
In this case, the server code will expect the host (C#/Java) variable to be of data type INTERVAL DAY TO SECOND. The server code will look something as follows:
SQL> declare
2 earlyleavers interval day to second;
3 begin
4 earlyleavers := NumToDSinterval(
5 (to_date('1900/01/01 11:00:00','yyyy/mm/dd hh24:mi:ss') -
6 to_date('1900/01/01 10:00:00','yyyy/mm/dd hh24:mi:ss') ) * 24,
7 'hour'
8 );
9
10 dbms_output.put_line( to_char(earlyleavers) );
11 end;
12 /
+00 01:00:00.000000
PL/SQL procedure successfully completed.
SQL>An interval-to-string conversion function is used (with default formatting) to "print" the interval value via DBMS_OUTPUT. The default format includes days and a sign to indicate a positive or negative interval. Look at the SQL Reference manual for the format masks that can be used.
The important thing is to treat data values correctly using the most appropriate type. And intervals should either be a decimal day/hour/minute numeric type, or an actual interval type. -
How to tune self join query in Oracle 11g
Oracle & SQL new to me,and I'm still in learning phase.
Could you please help me to tune below oracle query?? This table contains ~95 lac records and it takes 1 hour to retrieve data using this query.
Your suggestions/comments/help will be appreciated.
Thanks in advance.
SELECT A.CNO AS CNO, A.FNO AS FNO, A.CID AS CID, A.IID AS IID
FROM CAC_LKP A, (SELECT C_DATE, CNO, FNO
FROM (SELECT MAX(CAC_LKP.C_DATE) AS C_DATE, CAC_LKP.CNO AS CNO, CAC_LKP.FNO AS FNO
FROM CAC_LKP
WHERE ACTIVE = 'Y' GROUP BY CNO, FNO)) B
WHERE A.C_DATE = B.C_DATE
AND A.CNO = B.CNO
AND A.FNO = B.FNO
AND A.ACTIVE = 'Y'
Primary key is defied over combination of c_date,iid,active.
Edited by: 1009236 on Jun 1, 2013 12:52 AM>
SELECT A.CNO AS CNO,
A.FNO AS FNO,
A.CID AS CID,
A.IID AS IID
FROM CAC_LKP A,
SELECT C_DATE,
CNO,
FNO
FROM
SELECT MAX(CAC_LKP.C_DATE) AS C_DATE,
CAC_LKP.CNO AS CNO,
CAC_LKP.FNO AS FNO
FROM CAC_LKP
WHERE ACTIVE = 'Y'
GROUP BY CNO, FNO
) B
WHERE A.C_DATE = B.C_DATE AND
A.CNO = B.CNO AND
A.FNO = B.FNO AND
A.ACTIVE = 'Y';Hi,
Before even starting to see why there is a performance problem, I think you should consider the fact that there is logical problem in your WHERE clause. According to what you mentioned the primary key is composed of *(c_date, iid, active)* yet iid is absent in both the global query's WHERE clause and also that of the subquery.
Consequently the (aggregate) rows in the subquery will not be linked based on a correct logic to the external query.
Regards,
Dariyoosh -
HOW TO WRITE QUERY OR PL/SQL PROGRAM FOR THIS
I HAVE TABLE X
SELECT ENO FROM X
ENO
123
423
332
562
678
986
621
WHEN I RUN PL/SQL PROGRAM I WILL PASS PARAMETER
FOR EXAMPLE LIKE 134
I NEED OUTPUT LIKE
134 EXIST OUTPUT
123
423
332
562
621
134 NOT EXIST OUTPUT
678
986
Regards
DevThis?
<tested in TOAD>
SQL> accept my_parm
old: with t as (
select '123' eno from dual union all
select '423' from dual union all
select '332' from dual union all
select '562' from dual union all
select '678' from dual union all
select '986' from dual union all
select '621' from dual )
select eno || ' ' || output_type results
from (
select to_char(&&my_parm) eno, 'EXIST OUTPUT' output_type, 1 output_id
from dual union all
select null eno, null output_type, 3 output_id
from dual union all
select to_char(&&my_parm) eno, 'NOT EXIST OUTPUT' output_type, 4 output_id
from dual union all
select
eno,
null,
case when regexp_instr(eno,'[' || &&my_parm || ']') >0
then 2
else 5
end output_id
from t
order by output_id, eno
new: with t as (
select '123' eno from dual union all
select '423' from dual union all
select '332' from dual union all
select '562' from dual union all
select '678' from dual union all
select '986' from dual union all
select '621' from dual )
select eno || ' ' || output_type results
from (
select to_char(134) eno, 'EXIST OUTPUT' output_type, 1 output_id
from dual union all
select null eno, null output_type, 3 output_id
from dual union all
select to_char(134) eno, 'NOT EXIST OUTPUT' output_type, 4 output_id
from dual union all
select
eno,
null,
case when regexp_instr(eno,'[' || 134 || ']') >0
then 2
else 5
end output_id
from t
order by output_id, eno
SQL> with t as (
select '123' eno from dual union all
select '423' from dual union all
select '332' from dual union all
select '562' from dual union all
select '678' from dual union all
select '986' from dual union all
select '621' from dual )
select eno || ' ' || output_type results
from (
select to_char(134) eno, 'EXIST OUTPUT' output_type, 1 output_id
from dual union all
select null eno, null output_type, 3 output_id
from dual union all
select to_char(134) eno, 'NOT EXIST OUTPUT' output_type, 4 output_id
from dual union all
select
eno,
null,
case when regexp_instr(eno,'[' || 134 || ']') >0
then 2
else 5
end output_id
from t
order by output_id, eno
RESULTS
134 EXIST OUTPUT
123
332
423
621
134 NOT EXIST OUTPUT
562
678
986
10 rows selected.Message was edited by:
MScallion -
How to write a query in sql?
List employee details empno,ename,job,DOB, retire_date. i want
Retire date=bod + 58 year.. Display DOB in 'dd-mon-yyyy' format and retire_date in 'mon-yyyy'
Edited by: 1008586 on Jun 5, 2013 8:26 PM
Edited by: 1008586 on Jun 5, 2013 8:30 PMWelcome to the forum!
Whenever you post provide your 4 digit Oracle version.
>
List employee details empno,ename,job,DOB, retire_date.
Retire date=bod + 58 year.. Display DOB in 'dd-mon-yyyy' format and retire_date in 'mon-yyyy'
>
And your question or issue is? -
Self join with fact table in Obie 10G
I am a newbie to obiee.I have a development requirement as follows-
I need to find supervisors designation with the existing star RPD design. explanation is below
DIM_Designation(Desig_Wid)
|(Row_wid)
|
DIM_EMPLOYEE--------WORKER_FACT------------DIM_Supervisor
(Row_Wid)-----------------(Employee_Wid)
(Supervisor_Wid)------------(Row_Wid)
3 dimension is joined to fact to get employee, his supervisor and designation of employee. now i want to get the supervisor's designation? how is it possible? already employee and supervisor dimension is same W_employee_d table joined with fact as alias DIM_EMPLOYEE and DIM_SUPERVISOR. how to do self join with fact to get supervisor's designation. i do not have any supervisor_desig_wid in fact table. any help is deeply appreciated.Yes,Duplicate the fact table create a primary key on the newly fact table alias dimension table.So you can ur data modelling as usual.
-
How to re-write this self join update using a CTE
I would like to improve my performance on this update statement and would like to try re-writing using a CTE:
UPDATE "usr_sessions" "a" SET "is_ended_at_trustable" = 't'
WHERE (
EXISTS (
SELECT 1
FROM "usr_sessions" "b"
WHERE "a"."ended_at" = "b"."started_at"
AND "a"."usr_space_id" = "b"."usr_space_id"
AND "a"."account_id" = "b"."account_id"
) ) AND "a"."is_ended_at_trustable" IS NULL
Any help is greatly appreciated! Open to other suggestions as well if there is a better way!If I understood your description correctly, here's a way to accomplish the same thing, while dodging the need for the self join. The update itself won't be any faster, but the overall query leading to the update will likely be faster sans self-join.
(If my interpretation wasn't exactly what you meant, tweak the "partition by" clause).
MERGE is generally considered better then UPDATE, but your particular update isn't at risk for the shortcomings of update (still, Merge is newer, cooler, and more trustworthy).
Setup_Example_Data:
Declare @Usr_Sessions table (account_id int, usr_space_id int, is_ended_at_Trustable Char(1), started_at varchar(99), ended_at varchar(99))
Insert @Usr_Sessions
Select 1, 10, 't', 'A1', 'A1'
UNION ALL Select 2, 20, 'f', 'B1', 'B2'
UNION ALL Select 3, 30, NULL, 'C1', 'C1'
UNION ALL Select 4, 40, NULL, 'D1', 'D2'
UNION ALL Select 5, 50, NULL, 'E1', 'E2'
UNION ALL Select 5, 51, NULL, 'E3', 'E3'
UNION ALL Select 6, 61, NULL, 'F1', 'F2'
UNION ALL Select 6, 62, 't', 'F3', 'F3'
UNION ALL Select 6, 62, 'f', 'F4', 'F4'
Select 'Before', * from @Usr_Sessions
OP_Query:
BEGIN TRAN
UPDATE A SET is_ended_at_trustable = 't' from @usr_Sessions A-- Select * from @Usr_Sessions "a" --
WHERE (
EXISTS (
SELECT 1
FROM @usr_sessions "b"
WHERE "a"."ended_at" = "b"."started_at"
AND "a"."usr_space_id" = "b"."usr_space_id"
AND "a"."account_id" = "b"."account_id"
) ) AND "a"."is_ended_at_trustable" IS NULL
Select 'After 1', * from @Usr_Sessions
ROLLBACK TRAN /* Just to reset test data to original form, so second query below runs against original data */
Dodge_Self_Join:
With X as
Select *
, count(case when started_at = ended_at and is_ended_at_trustable is null then 'x' else null end)
over(partition by account_id, usr_space_id) as Updatable
From @Usr_Sessions
Update X
set is_ended_at_Trustable = 'T'
where Updatable > 0 -- EDIT -- fixed error, previously said "updatable = 1"
Select 'After 2', * from @Usr_Sessions -
How to write complex sql for this
Hi ALL,
I have a requirement like this
I have 5 tables which i have to join to get the result
but there no join column to 2 other table.
I want to get all the applications using cobal,running on UNIX.
How to write the query for this
1.APP
APP_i DESC
1 Accounts
2 Payments
3 order transfer
4 Order processing
2.Techgy
techid techdesc
1 cobal
2 Java
3.APP_Techgy
APP_I Techid
1 1
2 1
3 1
4 2
4.Pltfrm
pltfmid pltfrmdesc
1 Windows NT
2 UNIX
5.APP_Pltfrm
APP_I pltfrmid
1 1
2 1
3 2
4 2
ouput must be
APP_i Desc techDESC pltfrmdesc
3 ordertranfer Cobal UNIX
Thanks in advanceThis ('descr' in place of 'desc')?
SQL> select a.app_i, a.descr, t.techdesc, p.pltfrmdesc
from app_techgy atc,
app a,
techgy t,
app_pltfrm ap,
pltfrm p
where atc.techid = t.techid
and atc.app_i = a.app_i
and atc.app_i = ap.app_i
and ap.pltfrmid = p.pltfmid
order by a.app_i
APP_I DESCR TECHDESC PLTFRMDESC
1 accounts cobal windows nt
2 payments cobal windows nt
3 order transfer cobal unix
4 order processing java unix
4 rows selected. -
How to perform a self-join in WebI?
Post Author: willgreenland
CA Forum: WebIntelligence Reporting
I want to perform a self-join on a table in WebI, in order to achieve the following result (of course, if there is another way of doing this I'd be glad to hear it):
I have a table that lists the department in which an employee is located at given dates in the past:
EMPLID DEPT DATE
123 Sales 2007...
I want to use this table to track migration between departments, in other words I want to produce the following output table, showing how in 2008, 5 employees moved from Sales to Marketing (etc):DEPT_A DATE_A DEPT_B DATE_B COUNT(EMPLID) Sales 2007 Mrkting 2008 5...
In order to do this in SQL, I would do the following:
SELECT a.DEPT, b.DEPT, count(distinct EMPLID)FROMEMPL_DEPT a, EMPL_DEPT b // note the self-join hereWHERE( a.EMPLID = b.EMPLID AND a.DATE = '2007' AND b.DATE = '2008' )GROUP BY a.DEPT, b.DEPT;
Is there a way of doing this in WebI, ideally without resorting to manual SQL editing (I want this to be a report that other users can make sense of without necessarily getting into the SQL)?Post Author: amr_foci
CA Forum: WebIntelligence Reporting
you cant do something like that in the WebI directly, you have to manager that at the unvinerse level first
good luck -
How to write a sql query to retrieve data entered in the past 2 weeks
Hi,
I have file names and last accessed date(java.sql.Date format) stored in my database table, I would like to know how I can write a query to get the name of files accessed in the past 2 weeks,I use open sql server at the back end.
Thanks in advance.This has essentially nothing to do with JDBC. JDBC is just an API to execute the SQL language using Java and thus interact with the databases.
Your problem is related to the SQL language, you don't know how to write the SQL language. I suggest you to go through a SQL tutorial (there is one at w3schools.com) and to read the SQL documentation which come along with the database in question. A decent database manfacturer has a website and probably also a discussion forum / mailinglist as well.
I'll give you a hint: you can just use equality operators in SQL like everywhere. For example: "WHERE date < somedate". -
How to write a SQL query in SAP B1 2007 B with input parameters?
How to write a SQL query in SAP B1 2007 B with input parameters, on execution of which will ask for some input value from the user and the values will be selected from a list such as item list?
The syntax like
SELECT * FROM OITM T0 WHERE T0.ItemCode = '[%0\]'
Thanks,
Gordon
Maybe you are looking for
-
Open a new table or chart on clicking on "Input field" - Urgent
Hi all, I have a form view that contains many input fields. Is it possible to open a new table or a chart if an input field is clicked? If yes then how? Please respond. Regards, Aisha Ishrat ICI Pakistan Ltd.
-
What happened to the nightly builds?
I've noticed that as of late there have been no nightly builds on the Flex 3 or 4 SDK. This is odd to me, simply because these used to get published on a relatively regular basis. However, there hasn't been a nightly build of the Flex 4 SDK since
-
1St time user of Aperture here, coming from a Windows environment. I am a bit lost at the moment wit Aperture and how it works and why it does certain things. Firstly, managed or referenced. Which is best? I want to import my camera pics and keep the
-
Where are account settings files/folders?
I would like to back up the settings for the user accounts I have set up on the family computer, and while I can access all of the account home folders from the root admin account, I haven't been able to figure out where the system stores the setting
-
How can I sort properties in KM?
Hi! Can someone tell me, how can I sort properties in a table (KM) - for example the propertie modified in the default-Table Thanks in advance! Emma