Inner join and 'PIVOT' results
Good day to all;Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
"CORE 10.2.0.4.0 Production."
AMT for Solaris: release 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
I have 2 paintings I want to do a query on and then "pivot" results. I understand that, since it is 10 g using the PIVOT operator is not supported.
(Corrected code :)) issues
create table CT
(
DESIGN_ID NUMBER,
CT_ID VARCHAR2(15)
)
INSERT INTO CT VALUES ('654321','10QWER123456');
INSERT INTO CT VALUES ('987654','7ASDF654987');
INSERT INTO CT VALUES ('321654','82CHEV852963');
create table CXRF
(
DESIGN_ID NUMBER,
ROW_SEQ_NBR NUMBER,
XREF_CT_ID VARCHAR2(15)
)
INSERT INTO CXRF VALUES ('654321','1','25ABCD');
INSERT INTO CXRF VALUES ('654321','2','262ABCD');
INSERT INTO CXRF VALUES ('987654','1','14WXYZ');
INSERT INTO CXRF VALUES ('987654','2','34FRED');
INSERT INTO CXRF VALUES ('321654','1','1TOM');
-Oops, measure twice cut once...select design_id,
ct_id,
xref_ct_id
from cxrf
INNER JOIN
ct
on(ct.design_id = cxrf.design_id)
Expected results would look like this:DESIGN_ID CT_ID XREF_CT_1 XREF_CT_2Published by: GMoney on 9 may 2013 08:47
654321 10QWER123456 25ABCD 262ABCD
Published by: GMoney on 9 may 2013 08:49
Thanks for posting examples of data and version, however, as said, it would be better if you can test, DDLS is full of typos.
Did you visit the FAQ before posting?
{message: id = 9360005}
There are also effective methods in previous versions of 11 g data pivot.
For example:
SQL> select ct.design_id
2 , ct.ct_id
3 , min(case when cxrf.row_seq_nbr = 1 then cxrf.xref_ct_id end) as XREF_CT_1
4 , min(case when cxrf.row_seq_nbr = 2 then cxrf.xref_ct_id end) as XREF_CT_2
5 from cxrf
6 join ct on (ct.design_id = cxrf.design_id)
7 group by ct.design_id, ct.ct_id ;
DESIGN_ID CT_ID XREF_CT_1 XREF_CT_2
---------- ------------ ------------ ------------
654321 10QWER123456 25ABCD 262ABCD
987654 7ASDF654987 14WXYZ 34FRED
321654 82CHEV852963 1TOM
Tags: Database
Similar Questions
-
Difference between Inner join and right outer join...
Which is precisely the difference between an Inner join and right outer join...JOIN INTERNAL:-to return all rows from the two tables where there is a football game. That is to say. the table resulting from all the rows and columns will have values.
AND
RIGHT OUTER JOIN:-Returns all rows in the second table, even if there is no match in the first table.
-
Help of query SQL - inner joins and the separate results
Hello
ASP VB, SQL Server
I have a structure of data base with 3 tables - users, albums and photos. each user has a identifier unique, each record has a unique albumid and also contains a column with the user name. each record in the photo has a unique id so that store the user name and the album in which the image belongs.
I'm writing a query that returns a list of the albums for a particular user (based on a user name query string) and who will also bring back the id of the first record in the table for each of these albums photo.
the closest I get is to run a query to select albumid albums where userid = varuserid with a join internal on the pictures table to remove the photo ID - problem I then it comes out all the photos from the photos table where userid = varuserid, so when I do a repeat region to display a list of albums for a certain user It produces a list of all the photos where userid = varuserid
I really want to return just a list of ID album based on the username variable, but also to return the first record in the table of photos for each of these albumids
I tried different combinations of inner joins, select distinct etc but no joy.
any suggestion would be appreciated as am floundering here...
First, you must define 'first' with regard to the photos. Is there a
timestamp? They are numbered inside the album? Do you really care who is
"first", or do you want simply a shot? You also neglected to indicate if they are
empty photo albums have been allowed. I assumed that the empty albums are not
allowed.Whatever you decide, the answer will be similar.
SQL Server tends to get better results with joins with subqueries. You will have
See such a written request more often with subqueries, and there isn't
nothing wrong with that, but I'll use a join on a derived table. I have
have not all column names (hint, hint), so I made them, but the
Comments should help out you.SELECT A.Title, P.PhotoID, P.Caption, A.AlbumID, P.ImagePath
FROM dbo. A albums
-build a table derived, consisting of photo ID lowest for each
album.
INNER JOIN (SELECT AlbumID, MIN (PhotoID) AS FirstPhoto FROM dbo. Photos
AlbumID GROUP) AS PM WE A.AlbumID = PM. AlbumID
-details of the photo for the photo shown in the table above
INNER JOIN dbo. Photos P on A.AlbumID = P.AlbumID AND
H. FirstPhoto = P.PhotoID
User A.UserID ='some WHERE '"tedstar" wrote in message
News:ee4pfn$de$1@forums. Macromedia.com...
> I am writing a query that returns a list of the albums for a
> particular user (based on a user name query string) and also bring
> return
> the id of the first record in the table for each of these albums photo. -
Complex Inner Join and outer subquery where clause to join internal?
The DDL for this post was too big and I had to put on my site:
http://www.harbortownsolutions.com/DDLForDORTable.txt
My goal is to create a report of sales data for 116 stores containing daily sales and count of comments in recent years. My challenge is that I am pulling data from the same table, but with different dates to get data from last year and the week in data from date of last year.
The dates used for reports are:
SalesDate = 06/04/2009
SalesDateForLastYear = 2008-06-05
SalesDateBeginningOfWeek = 01/06/2009
SalesDateBginningOFWeekLastYear = 2008-06-02
PSEUDOQUERYS:
Question: Since they all use the same store of nbrs, I specify on each request? Can't I just specify only once on outer query Where clause?---======= DOLLAR VARIANCE = You Said you have $100 worth of meat(inventory) but you only have $50 worth of meat SELECT BUSI_DATE as BusinessDate, ORACLE_KEY as StoreID, FIELD1 as Sales, FIELD2 as GuestCount, FIELD3 as DollarVariance, FIELD4 as Cars FROM MYDAILYTOTALS WHERE Busi_date = SalesDate AND ORACLE_KEY IN (SELECT StoreID From MyStores); -======= ONLY Guest count and Sales are needed in comparisons against last year ---====== Last YEar dates = corresponding dates for this year ie Tuesday of this week matched to Tuesday of last year (except for leap year) SELECT ORACLE_KEY as StoreID FIELD1 as SalesLastYear FIELD2 as GuestCountLastYear FROM MYDAILYTOTALS WHERE Busi_date = SalesDateForLastYear AND ORACLE_KEY IN (SELECT StoreID From MyStores); SELECT ORACLE_KEY as StoreID SUM(FIELD1) as WeekToDateSales SUM(FIELD2) as WeekToDateGuestCount FROM MYDAILYTOTALS WHERE Busi_date BETWEEN SalesDateBeginningOfWeek and SalesDate AND ORACLE_KEY IN (SELECT StoreID From MyStores) GROUP BY ORACLE_KEY --======= SELECT ORACLE_KEY as StoreID SUM(FIELD1) as WeekToDateSalesLastYear SUM(FIELD2) as WeekToDateGuestCountLastYear FROM MYDAILYTOTALS WHERE Busi_date BETWEEN SalesDateBeginningOfWeekLastYear and SalesDateLastYear AND ORACLE_KEY IN (SELECT StoreID From MyStores) GROUP BY ORACLE_KEY
Also, how would fit the following of HOEK script:
See how I put 1 field based on another field value
He reads a record for each store of 116 stores in the database. There are 2 situations that have been addressed by Hoeks code:
For each of the 116 stores in the store table, there must be 1 and only
registration of 1 stock to match the record of MyStoreTotals. However, sometimes there is no record of inventory.
and sometimes there will be 2 (in a case where a manager of database updates)
After daily treatment) files. If so, the record with Is_posted = 1
is the one that should be included in the query. The following text accomplishes this:
-chr (39) is the single quote
Can I put this request on the place where clause of the outer to query I put on a JOIN clause?Select StoreId, DollarVariance from (select store.storeid, case when inv.storeid is null then 'None' when inv.is_posted = 0 then 'NP' else chr(39)||inv.total_dol_var||chr(39) end as DollarVariance, row_number() over (partition by store.storeid order by inv.is_posted desc) rn from myInven inv right outer join mystores store on store.storeid = inv.fk_str_main_id and inv.busi_date = to_date(SalesDate, 'dd-mon-yyyy')) where rn = 1;
So I guess that's why you used the MAX.
That is right.
I used CASES to identify and complete the necessary columns since the first set of results (WITH).
So then I more or less "swivels somehow" lines of this result set to the columns, each store will appear once the most. -
Difference-conditions (join and a Where Clause)
Hi people,
I need to clearly agree on what a difference exactly when we put any condition in INNER JOIN and the WHERE Clause.
I have tried both way and found the same results. Even in the statistics Plan not much differences. Any help would be appreciated.
As:
1 here, I use filter store in the join condition - Inner
"SELECT i., Gl * Sc1.Item I.
Inner Join Sc1.Part P
On P.Part_Id = I.Part_Id
Inner Join Sc1.Location Gl
On Gl.Location_Id = I.Location_Id
And Gl.Location_Id in (1767, 1747,202,1625)
Inner Join Sc1.Condition C
On C.Condtion_Id = Gl.Condition_Id
Where I.Inactive_Ind = 0
And I.Condition_Id! = 325
2. here I use filter store in Where clause-
SELECT i., Gl * Sc1.Item I
Inner Join Sc1.Part P
On P.Part_Id = I.Part_Id
Inner Join Sc1.Location Gl
On Gl.Location_Id = I.Location_Id
Inner Join Sc1.Condition C
On C.Condtion_Id = Gl.Condition_Id
Where I.Inactive_Ind = 0
and I.LOCATION_ID in (1767, 1747,202,1625)
And I.Condition_Id! = 325
Thank you
Mark
Hello
MarkCooper wrote:
Hi guys,.
To reply to all - I understand.
1. its good practice to use conditions / filter (except CLAUSE) in the WHERE Clause rather Inner join? bon ?
2. now, in my previous example. We could use the location code in where clause as it was in the two tables.
What is the best practice to use the code to location here ( 1 /2) ?
1. here I use filter store in the Inner join condition ( guess the location code is not in the article table)).
"SELECT i., Gl * Sc1.Item I.
Inner Join Sc1.Part P
On P.Part_Id = I.Part_Id
Inner Join Sc1.Location Gl
On Gl.Location_Id = I.Location_Id
And Gl.Location_Id in (1767, 1747,202,1625)
Inner Join Sc1.Condition C
On C.Condtion_Id = Gl.Condition_Id
Where I.Inactive_Ind = 0
And I.Condition_Id! = 325
2. here I use filter store in Where clause (assume that the location code is not in the article table)-
SELECT i., Gl * Sc1.Item I
Inner Join Sc1.Part P
On P.Part_Id = I.Part_Id
Inner Join Sc1.Location Gl
On Gl.Location_Id = I.Location_Id
Inner Join Sc1.Condition C
On C.Condtion_Id = Gl.Condition_Id
Where I.Inactive_Ind = 0
And Gl.Location_Id in (1767, 1747,202,1625)
And I.Condition_Id! = 325
Thank you
If location_id isn't in the item table, then the join condition
On Gl.Location_Id = I.Location_Id
will cause an error.
Once more, it should not affect results or performance if a condition like
Gl.Location_Id in (1767, 1747,202,1625)
is in the clause or the WHERE clause. No matter if it is be a column called location_id in any other table, or if the same column Gl.Location_Id is used in other conditions.
As this condition only refers to a table (GI), I recommend you put it in a WHERE clause, just to make the code clearer.
Yet once, this applies only to the inner joins, not for outer joins and not to CONNECT BY queries.
-
Problem with Outer join and filter
Hello
I join two tables in the source using a left outer join. Outside of the join, I have a filter specified with condition TabA.C1 > TabB.C2.
Now, when ODI generates the query it puts the left outer join on the filter condition as well. So he puts filter as
where
(1 = 1)
And ((TabA.C1 = TabB.C1 (+)) AND)
(TabA.C2 = TabB.C2 (+))
And TabA.C10 > TabB.C14 (+)
How to avoid this problem. I tried this performance on stage as well, always generated query remains the same.
I use the incremental update of the IKM Oracle. My source and target are both on the same PB.
~ ChikkHi Chikk,
If you analyze the data, you'll see it's OK to have the "(+)" to the filter...
Anyway, if you want to drop it, leave it as inner join and put the "(+)" manually to the join object.
This help you?
-
BAD RESULTS WITH OUTER JOINS AND TABLES WITH A CHECK CONSTRAINT
HII All,
Could any such a me when we encounter this bug? Please help me with a simple example so that I can search for them in my PB.
Bug:-8447623
Bug / / Desc: BAD RESULTS WITH OUTER JOINS AND TABLES WITH a CHECK CONSTRAINT
I ran the outer joins with check queries constraint 11G 11.1.0.7.0 and 10 g 2, but the result is the same. Need to know the scenario where I will face this bug of your experts and people who have already experienced this bug.
Version: -.
SQL> select * from v$version; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production PL/SQL Release 11.1.0.7.0 - Production CORE 11.1.0.7.0 Production TNS for Solaris: Version 11.1.0.7.0 - Production NLSRTL Version 11.1.0.7.0 - Production
Why do you not use the description of the bug test case in Metalink (we obviously can't post it here because it would violate the copyright of Metalink)? Your test case is not a candidate for the elimination of the join, so he did not have the bug.
Have you really read the description of the bug in Metalink rather than just looking at the title of the bug? The bug itself is quite clear that a query plan that involves the elimination of the join is a necessary condition. The title of bug nothing will never tell the whole story.
If you try to work through a few tens of thousands of bugs in 11.1.0.7, of which many are not published, trying to determine whether your application would be affected by the bug? Wouldn't be order of magnitude easier to upgrade the application to 11.1.0.7 in a test environment and test the application to see what, if anything, breaks? Understand that the vast majority of the problems that people experience during an upgrade are not the result of bugs - they are the result of changes in behaviour documented as changes in query plans. And among those who encounter bugs, a relatively large fraction of the new variety. Even if you have completed the Herculean task of verifying each bug on your code base, which would not significantly easier upgrade. In addition, at the time wherever you actually performed this analysis, Oracle reportedly released 3 or 4 new versions.
And at this stage would be unwise to consider an upgrade to 11.2?
Justin
-
difference between Inner Join join natural r
Experts,
I'm trying to understand the difference between Natural Join and inner join.
Inner join: the following query provides 106 lines based on two tables
SQL > SELECT EMPLOYEE_ID EID, last NAME, DEPT_NAME DEPARTMENT_NAME
2 EMPLOYEES A, B MINISTRIES
3. WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID
4 order employee_id;
Natural Join: This is the combination or combined result of all the columns in both tables.
When I run the Sub statement, it combines employees and the table from department of a HR schema and provides only 32 ranks
SQL > select employe_id, first_name, department_name
2 departments of NATURAL JOIN employees;
EMPLOYEE_ID NAME DEPARTMENT_NAME
----------- -------------------- -------------------
Neena 101 Executive
Lex 102 Executive
104 Bruce IT
David 105 HE
question: why the NATURAL JOIN query omit the employee_id 103 and on what basis it omit a few other employee_id
Thanks in advance
Hello
The main difference is that INNER JOIN is used in real life; NATURAL JOIN is used only in textbooks.
More rigorously, FRANCKLIN JOIN is an inner join, involving all the columns that have the same name. The hr.departments and hr.employees have 2 columns with the same name: department_id and manager_id, so
OF hr.departments d
NATURAL JOIN e hr.employees
is equivalent to
OF hr.departments d
INNER JOIN hr.employees e ON e.department_id = d.deparment_id
AND e.manager_id = d.manager_id
or, using the old join syntax
OF hr.departments d
e hr.employees
WHERE e.department_id = d.deparment_id
AND e.manager_id = d.manager_id
-
Update with the join and group by
Hi all
I'm currently updating multiple columns in a table from an inner join query.
First one recover the affected rows and values that I need for the update (which I call this subquery ED_Query).
It is important to note that this subquery has a group of and and the having clause.
My first attempt (using the query running in the SQL Server query) fails:
SQL > update ED_Update
2 set ED_Update.dtHoraInicioReal = ED_Query.dtHoraInicioReal,
3 ED_Update.dtHoraFinReal = ED_Query.dtHoraFinReal,
4 ED_Update.fPorcentajeRealizado = ED_Query.fPorcentajeRealizado
HISTORICOS_AVANZA 5. HSAE_HIS_EXPEDICIONDIARIA ED_Update
inner join 6)
7. select distinct ED.iIdExpedicion, ED.iIdExpedicionDiaria,
8 MAX (PT.iOrdenEnTrayecto) + 1 as iNumParadas,
9 MAX (HPP.iOrden) as iOrdenUltimaParada,
10 MIN (dtHora_LlegadaReal + iTiempoEnParada /(24*60*60)) as dtHoraInicioReal,
11 MAX (dtHora_LlegadaReal) as dtHoraFinReal,
12 100 * cast ((MAX (HPP.iOrden) + 1) as float) / cast ((MAX (PT.iOrdenEnTrayecto) + 1) as float) as fPorcentajeRealizado
13 of HISTORICOS_AVANZA. ED HSAE_HIS_EXPEDICIONDIARIA
14 left join HISTORICOS_AVANZA. HSAE_HIS_HORAPASOPARADA HPP
15 ED.iIdExpedicion = HPP.iIdExpedicion and ED.dtJornada = HPP.dtJornada
16 left join AVANZA. SAE_URB_PARADASTRAYECTO PT on ED.iIdLinea = PT.iIdLinea and ED.iIdTrayecto = PT.iIdTrayecto
17 where ED.dtJornada = TO_DATE (January 14, 2013 ',' DD/MM/YYYY ') and ED.iIdExpedicion in (-131076)
18 ED.iIdExpedicion, ED.iIdExpedicionDiaria, ED.dtHoraInicioReal, ED.dtHoraFinReal group
19 having ED.dtHoraInicioReal <>min (dtHora_LlegadaReal + iTiempoEnParada /(24*60*60))
20 or ED.dtHoraFinReal <>max (dtHora_LlegadaReal)
(21) ED_Query
22 we ED_Update.iIdExpedicionDiaria = ED_Query.iIdExpedicionDiaria;
ERROR on line 5:
ORA-00933: SQL not correctly completed command.
Subquery (ED_Query) work fine in Oracle, so I suspect that the problems are when I mix with the update clause.
SQL > select distinct ED.iIdExpedicion, ED.iIdExpedicionDiaria,.
2 MAX (PT.iOrdenEnTrayecto) + 1 as iNumParadas,
3 MAX (HPP.iOrden) as iOrdenUltimaParada,
4 MIN (dtHora_LlegadaReal + iTiempoEnParada /(24*60*60)) as dtHoraInicioReal,
5 MAX (dtHora_LlegadaReal) as dtHoraFinReal,
6 100 * cast ((MAX (HPP.iOrden) + 1) as float) / cast ((MAX (PT.iOrdenEnTrayecto) + 1) as float) as fPorcentajeRealizado,
7 ED.dtHoraInicioReal ED_dtHoraInicioReal, ED.dtHoraFinReal as ED_dtHoraFinReal, ED.fPorcentajeRealizado as ED_fPorcentajeRealizado
8 of HISTORICOS_AVANZA. ED HSAE_HIS_EXPEDICIONDIARIA
9 left join HISTORICOS_AVANZA. HSAE_HIS_HORAPASOPARADA HPP
10 on ED.iIdExpedicion = HPP.iIdExpedicion and ED.dtJornada = HPP.dtJornada
11 left join AVANZA. SAE_URB_PARADASTRAYECTO PT on ED.iIdLinea = PT.iIdLinea and ED.iIdTrayecto = PT.iIdTrayecto
where the ED.dtJornada 12 = TO_DATE (January 14, 2013 ',' DD/MM/YYYY ') and ED.iIdExpedicion in (-131076)
13 group of ED.iIdExpedicion, ED.iIdExpedicionDiaria, ED.dtHoraInicioReal, ED.dtHoraFinReal, ED.fPorcentajeRealizado
14 after ED.dtHoraInicioReal <>min (dtHora_LlegadaReal + iTiempoEnParada /(24*60*60))
15 or ED.dtHoraFinReal <>max (dtHora_LlegadaReal);
IIDEXPEDICION IIDEXPEDICIONDIARIA INUMPARADAS IORDENULTIMAPARADA DTHORAINI
------------- ------------------- ----------- ------------------ ---------
DTHORAFIN FPORCENTAJEREALIZADO ED_DTHORA ED_DTHORA ED_FPORCENTAJEREALIZADO
--------- -------------------- --------- --------- -----------------------
-131076 5662 406 15 JANUARY 13
15 JANUARY 13 JANUARY 15, 13 15 JANUARY 13 0
-131076 5663 406 15 JANUARY 13
15 JANUARY 13 JANUARY 15, 13 15 JANUARY 13 0
-131076 5664 406 15 JANUARY 13
15 JANUARY 13 JANUARY 15, 13 15 JANUARY 13 0
After reading this forum, I have change the query and try the following:
SQL > UPDATE
() 2
3. select distinct ED.iIdExpedicion, ED.iIdExpedicionDiaria,
4 MAX (PT.iOrdenEnTrayecto) + 1 as iNumParadas,
5 MAX (HPP.iOrden) as iOrdenUltimaParada,
6 MIN (dtHora_LlegadaReal + iTiempoEnParada /(24*60*60)) as dtHoraInicioReal,
7 MAX (dtHora_LlegadaReal) as dtHoraFinReal,
8 100 * cast ((MAX (HPP.iOrden) + 1) as float) / cast ((MAX (PT.iOrdenEnTrayecto) + 1) as float) as fPorcentajeRealizado,
9 ED.dtHoraInicioReal ED_dtHoraInicioReal, ED.dtHoraFinReal as ED_dtHoraFinReal, ED.fPorcentajeRealizado as ED_fPorcentajeRealizado
HISTORICOS_AVANZA 10. ED HSAE_HIS_EXPEDICIONDIARIA
11 left join HISTORICOS_AVANZA. HSAE_HIS_HORAPASOPARADA HPP
12 on ED.iIdExpedicion = HPP.iIdExpedicion and ED.dtJornada = HPP.dtJornada
13 left join AVANZA. SAE_URB_PARADASTRAYECTO PT on ED.iIdLinea = PT.iIdLinea and ED.iIdTrayecto = PT.iIdTrayecto
14 where ED.dtJornada = TO_DATE (January 14, 2013 ',' DD/MM/YYYY ') and ED.iIdExpedicion in (-131076)
Group 15 of ED.iIdExpedicion, ED.iIdExpedicionDiaria, ED.dtHoraInicioReal, ED.dtHoraFinReal, ED.fPorcentajeRealizado
16 having ED.dtHoraInicioReal <>min (dtHora_LlegadaReal + iTiempoEnParada /(24*60*60))
17 or ED.dtHoraFinReal <>max (dtHora_LlegadaReal)
18)
19 SET ED_dtHoraInicioReal = dtHoraInicioReal,
20 ED_dtHoraFinReal = dtHoraFinReal,
21 ED_fPorcentajeRealizado = fPorcentajeRealizado;
ERROR on line 2:
ORA-01732: operation non-legal data manipulation on this point of view
Little help?
Thanl in advance.
Published by: 984483 on 28-ene-2013 01:48Hello
Thanks for posting the CREATE TABLE and INSERT.
Don't forget to post the desired results of these sample data, i.e. the content of the or the tables changed after the UPDATE is made.Is that what you want ed_update to look like after that everyhting is finished?
DATE01 NUMBERMAX NUMBERSUM ---------- ---------- ---------- 01/01/2013 30 60 02/01/2013 0 0 03/01/2013 0 0
If so, here's a way to do it:
UPDATE ed_update u SET (numbermax, numbersum) = ( SELECT MAX (number01) , SUM (number01) FROM ed_query WHERE date01 = u.date01 ) WHERE date01 = TO_DATE ('01/01/2013', 'DD/MM/YYYY') -- If wanted ;
In Oracle, INNER JOIN works only in the FROM clause of a SELECT statement.
When you want to update a table with values from another table, you can do a subquery correlated (as I showed above) or use the MERGER instead of UPDATE. -
inner join is not eliminating duplicates!
Name of the table is customers
VALUE CITY SIDE NUMS CNAME
1 2001 Hoffman 100 1001 London
2 Rome 2002 200 1003 Giovanni
3 2003 Liu San Jose 200 1002
4 2004 grass Berlin 300 1002
5 2006 Clemens 100 1001 London
6 2008 Cisneros San Jose 300 1007
7 2007 perish Rome 100 1004
Query is
To find all pairs of customers having the same side
I tried:
SELECT c.cname, cb.cname FROM customers c INNER JOIN customers cb WE cb.rating AND c.cname = c.rating! = cb.cname
CNAME CNAME
1 perish Hoffman
2 Clemens Hoffman
3 Liu Giovanni
4 Giovanni Liu
5 Cisneros grass
6 perira Clemens
7 Hoffman Clemens
8 grass Cisneros
Clemens 9 perish
10 Hoffman perish
Ideally, it should up to 5 rows, but it does not return these lines for an e.g. Hoffman perish since I'm Perira Hoffman already. Your answers will be appreciated.
Thank you
VaibhavHi, Vaibhav,
914683 wrote:
Name of the table is customersVALUE CITY SIDE NUMS CNAME
1 2001 Hoffman 100 1001 London
2 Rome 2002 200 1003 Giovanni
3 2003 Liu San Jose 200 1002
4 2004 grass Berlin 300 1002
5 2006 Clemens 100 1001 London
6 2008 Cisneros San Jose 300 1007
7 2007 perish Rome 100 1004Query is
To find all pairs of customers having the same sideI tried:
SELECT c.cname, cb.cname FROM customers c INNER JOIN customers cb WE cb.rating AND c.cname = c.rating! = cb.cname
Perhaps what you want is
SELECT c.cname , cb.cname FROM customers c INNER JOIN customers cb ON c.rating = cb.rating AND c.cname < cb.cname -- not != ;
CNAME CNAME
1 perish Hoffman
2 Clemens Hoffman
3 Liu Giovanni
4 Giovanni Liu
5 Cisneros grass
6 perira Clemens
7 Hoffman Clemens
8 grass Cisneros
Clemens 9 perish
10 Hoffman perishThe title you chose for this thread is "inner join is not eliminating duplicates!
Inner joins (or other types of joints) are not supposed to remove duplicates. Use SELECT DISTINCT to remove duplicates, which is exactly the same lines.
You don't have to duplicate. For example, these lines of your result set:` CNAME CNAME 1 Perira Hoffman 10 Hoffman Perira
are not identical; Indeed, the two columns are different.
Ideally, it should up to 5 rows, but it does not return these lines for an e.g. Hoffman perish since I'm Perira Hoffman already.
Please report the exact output of the sample data you want. (As said by Hoek, post CREATE TABLE and INSERT statements for the sample data too.)
-
Hi all
I have a few tables with samples as:
As you can see I have Hotels with basic information, persons of type hotel_people (1:baby, 2:young and 3:Adult), which contains the type of people that each hotel can accommodate with min max age and hotel_service that contains the info from the number of rooms and the amount of people that can be in each room.CREATE OR REPLACE TABLE hotel ( hotel_id int NOT NULL , hotel_name varchar2(50) NULL , hotel_address varchar2(100) NULL ); CREATE OR REPLACE TABLE people_type ( people_type_id int NOT NULL , type_des varchar2(30) NOT NULL ); CREATE OR REPLACE TABLE hotel_people ( hotel_id int NOT NULL , people_type_id int NOT NULL , age_min int NULL , age_max int NULL ); CREATE OR REPLACE TABLE hotel_service ( hotel_id int NOT NULL , qty_room int NOT NULL , qty_people int NOT NULL, qty_people_extra int NOT NULL ); insert into hotel(hotel_id, hotel_name, hotel_address) values(1,'hotel1','hotel1 address1'); insert into hotel(hotel_id, hotel_name, hotel_address) values(2,'hotel2','hotel2 address2'); insert into hotel(hotel_id, hotel_name, hotel_address) values(3,'hotel3','hotel3 address3'); insert into hotel(hotel_id, hotel_name, hotel_address) values(4,'hotel4','hotel4 address4'); insert into people_type (people_type_id, type_des) values (1,'Baby'); insert into people_type (people_type_id, type_des) values (2,'Young'); insert into people_type (people_type_id, type_des) values (3,'Adult'); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (1,1,0,2); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (1,2,3,10); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (1,3,11,200); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (2,2,3,17); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (2,3,18,200); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (3,3,18,200); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (4,1,0,2); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (4,2,3,10); insert into hotel_people (hotel_id, people_type_id, age_min, age_max) values (4,3,11,200); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (1,10,2,1); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (1,5,4,0); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (2,6,2,1); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (2,12,3,1); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (3,10,2,1); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (3,5,4,0); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (4,6,2,1); insert into hotel_service (hotel_id, qty_room, qty_people,qty_people_extra) values (4,12,3,1);
I'm trying to get the records that match my condition as:
1. I want to get the hotels that can accommodate 4 pieces with adults of 3 for each room.
2 get hotels that can accommodate 6 rooms 2 where 2 of the bedrooms can have 1 young (3 people per room) and young people 9 years.
For 1:
For 2:select h.hotel_id, h.hotel_name, ss.qty_room, t.type_des, ss.qty_people, p.age_min, p.age_max from hotel h inner join hotel_service ss on h.hotel_id = ss.hotel_id inner join hotel_people p on h.hotel_id = p.hotel_id inner join people_type t on p.people_type_id = t.people_type_id where ss.qty_room >= 4 and t.people_type_id = 3 and ss.qty_people >= 3
This does not work, the start of the query becomes more complex if I try to search for multiple rooms with a different number of people and children of different ages.select h.hotel_id, h.hotel_name, ss.qty_room, t.type_des, ss.qty_people, p.age_min, p.age_max from hotel h inner join hotel_service ss on h.hotel_id = ss.hotel_id inner join hotel_people p on h.hotel_id = p.hotel_id inner join people_type t on p.people_type_id = t.people_type_id where ss.qty_room >= 6 and ss.qty_people >= 3 and t.people_type_id in (2,3) and 9 between p.age_min and p.age_max
Can anyone help with this?
Thank you
Published by: user9542267 on April 3, 2012 22:26Hello
user9542267 wrote:
... Yes, what I need is a hotel that can accommodate TWO adults and youth. What do you mean by two copies?Right now I read something on the Grand Duke by Gilbert. I also have a book with the complete works of Gilbert, so I can leave it open, and when I see a reference in the commentary, I see exactly what the author is talking about in the book. However, when the comment compares some parts of this game to the other plays of Gilbert, The Yeomen of the Guard , it gets hard, because I have to keep changing the pages in the complete works, and I can't see both at the same time. It would be much easier if I could get a full second copy of the work; then I could leave a copy of the Grand Duke and leave the other copy open to The Yeomen of the Guard . This is similar to what you should do with the table of hotel_people, and what I do with the table scott.emp below.
Imagine (using tables in the scott schema) you have to find the departments where both an analyst and a clerk to work.
This:SELECT DISTINCT d.* FROM scott.dept d JOIN scott.emp e ON d.deptno = e.deptno WHERE job IN ('ANALYST', 'CLERK') ORDER BY d.deptno ;
product
DEPTNO DNAME LOC ------ -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO
because all these departments have either an analyst or a clerk. However, only deptno = 20 a time .
If, instead of a single table of big job, you have a separate table for each Olympic Games, then you might do something like this:SELECT DISTINCT d.* FROM scott.dept d JOIN analyst a ON d.deptno = a.deptno JOIN clerk c ON d.deptno = c.deptno ORDER BY d.deptno ;
Of course, you don't have tables separated like that, but you could generate result sets who behaved as separte tables, for example:
WITH analyst AS ( SELECT * FROM scott.emp WHERE job = 'ANALYST' ) , clerk AS ( SELECT * FROM scott.emp WHERE job = 'CLERK' ) SELECT DISTINCT d.* FROM scott.dept d JOIN analyst a ON d.deptno = a.deptno JOIN clerk c ON d.deptno = c.deptno ORDER BY d.deptno ;
Note that the main request is exactly what I posted earlier.
What produces the right output:DEPTNO DNAME LOC ------ -------------- ------------- 20 RESEARCH DALLAS
There is no need to actually generate separate result sets, however. You can use two copies of the table scott.emp in the same query, as follows:
SELECT DISTINCT d.* FROM scott.dept d JOIN scott.emp a ON d.deptno = a.deptno JOIN scott.emp c ON d.deptno = c.deptno WHERE a.job = 'ANALYST' AND c.job = 'CLERK' ORDER BY d.deptno ;
-
Hi all
I am trying to learn something new, and I'm having a little difficulty. I'm trying to learn more about inner joins. I looked at countless books, tutorials, and all the rest have that I can find, but I can't seem to understand that. Nothing I try works.
I have related tables, and I try to get the data to display. I have the database set up as follows:
films (table name) has the movie_id, movie_name and movie_star_rating fields
family_rating (table name) has star_id, star_name, and star_file_name fields
in PHPMyAdmin, I had movies.movie_star_rating internally associated with family_rating.star_id
When I view the data, I'm pulling the table of movies, but instead of having a number, I would like to have displayed family_rating.star_name. I have the following code to do this:
$query="SELECT * FROM movies INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id WHERE movies.movie_name_series='$search'"; $result= mysql_query($query)or die (mysql_error()); while($row=mysql_fetch_array($result)){ // Display the data echo $row['movies.movie_name']; echo $row['family_rating.star_file_name']; }
Can someone let me know what I am doing wrong? Thanks for your help.$query = "SELECT * FROM movies, family_rating"
INNER JOIN family_rating ON movies.movie_star_rating = family_rating.star_id
WHERE movies.movie_name_series = '$search';OK, that SQL is not the same as your original post.
> Not unique table/alias: 'family_rating '.
This tells us what's wrong. You specify the two tables after the from clause. Try again with your original query:
$query = "SELECT * FROM movies"
INNER JOIN family_rating ON movies.movie_star_rating = family_rating.star_id
WHERE movies.movie_name_series = '$search'; -
Dear all,
Kindly advise on below 2 doubts which have been daunting for quiet some time. The bottom has forced our requests to take alternative routes to receive solutions.
Doubt 1) is it possible for us to use 'select (*)' with a group and a join clause, if we have at least 2/3 tables with minimum 15-20 columns... given the constraint of having to add all the names column in the group by clause.
Question 2) is it possible to use "select" (*) with the exception of a single column of the table?
That is to say, I hate the output of a statement select (*) to have the entire field except a field in a tableAsk your advisor for the same thing. Excuse me because I have no examples to illustrate this point in time. A successful idea will do.
Thank you and best regards,
Séverine Suresh
Hey, Sebastian,
Séverine Suresh - 3035408 wrote:
Dear all,
Kindly advise on below 2 doubts which have been daunting for quiet some time. The bottom has forced our requests to take alternative routes to receive solutions.
Doubt 1) is it possible for us to use 'select (*)' with a group and a join clause, if we have at least 2/3 tables with minimum 15-20 columns... given the constraint of having to add all the names column in the group by clause.
Sure. If you had problems, you did it wrong. Your postal code, examples of data (CREATE TABLE and INSERT statements) and the results desired from these data.
Check out the Forum FAQ: Re: 2. How can I ask a question on the forums?
Normally, you don't want to ' SELECT *...» "in a join. Are most joins are equijoins, ' SELECT *...» "produced 2 copies of / columns used for Assembly. In addition, many tables have columns (for example, modified_date) that are not necessary in most applications.
All columns must be included explicitly in a GROUP BY clause. You cannot use * it.
You can not have parentheses around the * in ' SELECT *...» ». You might think 'SELECT COUNT (*) '
Question 2) is it possible to use "select" (*) with the exception of a single column of the table?
That is to say, I hate the output of a statement select (*) to have the entire field except a field in a table...
No.; If you don't want a particular column in the result set, you cannot use "SELECT *...» ».
Your front end can have so as not to display a column in the result set. For example, in SQL * more:
COLUMN modified_date NOPRINT
means that you won't see any column called modified_date.
-
Syntax of UPDATE of Oracle with the JOIN and the WHERE clause
I need to update one of my paintings and can't seem to get the syntax right. I tried 4 different approaches, but does not have a chance. Any help would be greatly appreciated.
Attempt #1: Error: not correctly completed SQL command
UPDATE TESTDTA.F4941
SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM
FROM TESTDTA.F4941
INNER JOIN TESTDTA.F4981
ON TESTDTA.F4941.RSSHPN = TESTDTA.F4981.FHSHPN
WHERE TESTDTA.F4941.RSSHPN = 647143
AND TESTDTA.F4941.RSDSTN = 0
AND TESTDTA.F4941.RSADDJ > 110365
AND TESTDTA.F4981.FHCGC1 = 'DIS';Try #2: Error report: SQL error: ORA-00904: "TESTDTA." "" F4981 ". "' FHUOM ': invalid identifier 00904. 00000 - '% s: invalid identifier '.
UPDATE (SELECT TESTDTA.F4941
FROM TESTDTA.F4941
INNER JOIN TESTDTA.F4981
ON TESTDTA.F4941.RSSHPN = TESTDTA.F4981.FHSHPN
WHERE TESTDTA.F4941.RSSHPN = 647143
AND TESTDTA.F4941.RSDSTN = 0
AND TESTDTA.F4941.RSADDJ > 110365
AND TESTDTA.F4981.FHCGC1 = 'DIS')
SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOMTry #3: Error report: SQL error: ORA-00904: "TESTDTA." "' F4941 ': invalid identifier 00904. 00000 - '% s: invalid identifier '.
UPDATE (SELECT *
FROM TESTDTA.F4941
INNER JOIN TESTDTA.F4981
ON TESTDTA.F4941.RSSHPN = TESTDTA.F4981.FHSHPN
WHERE TESTDTA.F4941.RSSHPN = 647143
AND TESTDTA.F4941.RSDSTN = 0
AND TESTDTA.F4941.RSADDJ > 110365
AND TESTDTA.F4981.FHCGC1 = 'DIS')
SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM;Try #4: Error: ORA-00925: Missing keyword 00925. 00000 - "missing IN the keyword"
MERGE TESTDTA.F4941
USING TESTDTA.F4981
ON (TESTDTA.F4941.RSSHPN = TESTDTA.F4981.FHSHPN
AND TESTDTA.F4941.RSSHPN = 647143
AND TESTDTA.F4941.RSDSTN = 0
AND TESTDTA.F4941.RSADDJ > 110365
AND TESTDTA.F4981.FHCGC1 = 'DIS'
)
WHEN MATCHED THEN
UPDATE SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ
,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM;Finally managed to do work, adding DISTINCT to select. I have pasted the SQL error in Google to begin with. Laughing out loud!
Code snippet work for anyone who comes across this thread...
MERGE INTO TESTDTA. F4941 DST
USING (SELECT DISTINCT TESTDTA. F4941. RSSHPN,
TESTDTA. F4941. RSDSTN,
TESTDTA. F4941. RSUMD1,
TESTDTA. F4941. RSADDJ,
TESTDTA. F4981. FHSHPN,
TESTDTA. F4981. FHRTDQ,
TESTDTA. F4981. FHUOM,
TESTDTA. F4981. FHCGC1
OF TESTDTA. F4941
JOIN INTERNAL TESTDTA. F4981
ON TESTDTA. F4941. RSSHPN = TESTDTA. F4981. FHSHPN
WHERE - TESTDTA. F4941. RSSHPN = 647143 AND
TESTDTA. F4941. RSDSTN = 0
AND TESTDTA. F4941. RSADDJ > 110365
AND TESTDTA. F4981. FHCGC1 = 'SAY') CBC
on (DST. RSSHPN = SRC. FHSHPN)
When matched then update
SET DST. RSDSTN = SRC. FHRTDQ, SUMMER TIME. RSUMD1 = SRC. FHUOM;
Thanks to everyone who helped with this.
-
How to do an INNER JOIN subquery?
Can't seem to find anything that could help out me with this.
My prototype query goes like this but its gives me an ORA-00905: lack of keyword
SELECT Poker_Site.site_name, bonus.sign_up_bonus
OF poker_site
INNER JOIN bonus
WHERE Poker_site.site_name = 'Poker Stars'
WE Poker_Site.site_name = bonus.site_name;Join INNER JOIN must be immediately followed by ONE and only then, where:
SELECT Poker_Site.site_name, bonus.sign_up_bonus FROM poker_site INNER JOIN bonus ON Poker_Site.site_name = bonus.site_name WHERE Poker_site.site_name = 'Poker Stars';
SY.
Maybe you are looking for
-
Additional: I searched on: config but can't find a setting that seems to apply. Am I missing something?
-
My copy of Firefox has been hacked, and I can't uninstall it
My family has been changed to a bogus research framework. I have reset to Google, but every time I use Google, fake sites come.
-
My TSST CDRWDVD TST 462D driver has been damaged or lost. How can I download another driver without having to pay a lot of money. My games have also disappeared.
-
Hello After the update of some files on my USB stick and re - mount, everything has been spotted in read-only. If I right click on properties and uncheck the read-only box, it goes through the motions but doesn't change anything. I also tried to use
-
HP 5610 all-in-one printer: printing problems from the internet
my printer Hp 5610 all in one printer don't print properly on the internet, bold text is readable, any type is barely legible, and in some cases, unreadable. I have downloaded and run the hp print and scan doctor and followed touble shooting recommen