SQL: Cross join query - sql

SELECT * FROM training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (SELECT DISTINCT PC2.POSITION training.dbo.PERSON_CAREER_HISTORY) PC2
WHERE PC.POSITION IS NULL
the cross join part is not working giving the error
"Incorrect syntax near '.'."
I can't fix it, and been fixing it for about an hour. Please tell me my error

You missed the FROM in the CROSS JOIN.
SELECT * FROM training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (SELECT DISTINCT PC2.POSITION FROM training.dbo.PERSON_CAREER_HISTORY) PC2
WHERE PC.POSITION IS NULL

You have missed out the FROM keyword from the sub query. Try this:
SELECT
*
FROM
training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC
ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (
SELECT DISTINCT
PC2.POSITION
FROM
training.dbo.PERSON_CAREER_HISTORY) PC2
WHERE
PC.POSITION IS NULL

If you are looking for the positions that people do not have, then this is the query that you want:
SELECT *
FROM training.dbo.[PERSON] P CROSS JOIN
(SELECT DISTINCT PC2.POSITION FROM training.dbo.PERSON_CAREER_HISTORY
) pp LEFT JOIN
training.dbo.PERSON_CAREER_HISTORY PCH
ON P.PERSON_ID = PC.PERSON_ID AND pp.POSITION = PC.POSITION
WHERE PC.POSITION IS NULL;
The joins are not correct in your version (as well as the problem with the subquery). Otherwise, I cannot figure out the purpose of your original query.

Related

ORA-01427: single-row subquery returns more than one row - I get this error even though I use SELECT DISTINCT

I am trying to make a query that selects all the drivers that were fined in the same place where another driver was fined.
SELECT DISTINCT(c.IdSofer), s.NumeSofer
FROM Soferi s
INNER JOIN contraventii c ON c.IdSofer= s.IdSofer
HAVING (SELECT DISTINCT IdLocContr FROM Contraventii c
INNER JOIN Soferi s ON c.IdSofer=s.IdSofer
INNER JOIN Localitati l ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer='Maneta Gheorghe' AND l.DenLoc='Pocreaca' AND l.Jud='IS' GROUP BY IdLocContr) = c.IdLocContr
GROUP BY s.NumeSofer, c.IdLocContr, c.IdSofer;
This is what I tried but I get the error message
ORA-01427: single-row subquery returns more than one row.
If I run only only the SELECT statement between parentheses it works fine. It shows the id of cities (two cities) where this driver was fined. But if I try to run the code as I wrote here I get this error.
EDIT
This is the SQL from my comment...
SELECT DISTINCT(c.IdSofer)
,s.NumeSofer
FROM Soferi s INNER JOIN contraventii c
ON c.IdSofer= s.IdSofer
GROUP BY s.NumeSofer
,c.IdLocContr
,c.IdSofer
HAVING (SELECT DISTINCT IdLocContr
FROM Contraventii c INNER JOIN Soferi s
ON c.IdSofer=s.IdSofer INNER JOIN Localitati l
ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer = 'Maneta Gheorghe'
AND l.DenLoc = 'Pocreaca'
AND l.Jud = 'IS'
GROUP BY IdLocContr) = c.IdLocContr
WHERE c.IdLocContr IN (
SELECT DISTINCT IdLocContr
FROM Contraventii c
INNER JOIN Soferi s ON c.IdSofer=s.IdSofer
INNER JOIN Localitati l ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer = 'Maneta Gheorghe' AND l.DenLoc = 'Pocreaca' AND l.Jud = 'IS'
)
Just a few notes for your benefit.
The distinct isn't really necessary inside an in subquery although I left it there. Also you were using both distinct and group by which were redundant for that query. Having comes into play when you want to filter based on group aggregates.
It's too long to put in comment, so I put it here. Debugging is part of programmer's job. Have you checked whether the query below returns one row or not? If not, then that's what you have to fix.
SELECT DISTINCT IdLocContr
FROM Contraventii c
INNER JOIN Soferi s ON c.IdSofer=s.IdSofer
INNER JOIN Localitati l ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer='Maneta Gheorghe' AND l.DenLoc='Pocreaca' AND l.Jud='IS'
GROUP BY IdLocContr
From the look at it, I doubt it will only return 1 row.
your having clause should return one value:
SELECT DISTINCT IdLocContr
FROM Contraventii c INNER JOIN Soferi s
ON c.IdSofer=s.IdSofer INNER JOIN Localitati l
ON s.IdLocSofer=l.IdLoc
WHERE s.NumeSofer = 'Maneta Gheorghe'
AND l.DenLoc = 'Pocreaca'
AND l.Jud = 'IS'
AND rownum<=1
GROUP BY IdLocContr

Duplicate data from select statement

I want to make a stress test to a procedure than generate a .csv file.
The problem is that i have not enough data, so i want to duplicate data in my sql select .
The query look like this:
SELECT P.FST_NAME,
P.LAST_NAME,
P.EMAIL_ADDR,
P.PERSON_UID,
PR.FST_NAME PRSP_FST_NAME,
PR.LAST_NAME PRSP_LAST_NAME,
M.X_BAPRO_DT_01,
M.X_BAPRO_DT_02,
M.X_BAPRO_DT_03,
M.X_BAPRO_MONTO,
M.X_BAPRO_NUM_01,
M.X_BAPRO_NUM_02,
M.X_BAPRO_NUM_03,
M.X_BAPRO_TEXT_01,
M.X_BAPRO_TEXT_02,
M.X_BAPRO_TEXT_03,
M.X_BAPRO_TEXT_04,
M.X_BAPRO_TEXT_05
FROM SIEBEL.S_SRC C
left join SIEBEL.S_CAMP_CON M on C.ROW_ID = M.SRC_ID
left join SIEBEL.S_DMND_CRTN_PRG T on T.ROW_ID = M.DCP_ID
left join SIEBEL.S_CONTACT P on P.ROW_ID = M.CON_PER_ID
left join SIEBEL.S_PRSP_CONTACT PR on PR.ROW_ID= M.PRSP_CON_PER_ID
WHERE
C.ROW_ID <> p_row_id
So, This query return about 100 records, i want to retrive 1000 records and i dont really care if the data is duplicated.
You can add a cross join:
FROM SIEBEL.S_SRC C
left join SIEBEL.S_CAMP_CON M on C.ROW_ID = M.SRC_ID
left join SIEBEL.S_DMND_CRTN_PRG T on T.ROW_ID = M.DCP_ID
left join SIEBEL.S_CONTACT P on P.ROW_ID = M.CON_PER_ID
left join SIEBEL.S_PRSP_CONTACT PR on PR.ROW_ID= M.PRSP_CON_PER_ID
cross join (select 1 as n from dual union all
select 2 from dual
. . .
) x
You can also use the VALUE clause to construct the little "muliplier"-table as shown below:
SELECT ...
FROM SIEBEL.S_SRC C
left join SIEBEL.S_CAMP_CON M on C.ROW_ID = M.SRC_ID
left join SIEBEL.S_DMND_CRTN_PRG T on T.ROW_ID = M.DCP_ID
left join SIEBEL.S_CONTACT P on P.ROW_ID = M.CON_PER_ID
left join SIEBEL.S_PRSP_CONTACT PR on PR.ROW_ID= M.PRSP_CON_PER_ID
cross join (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) tabl(n)

Right outer join with OR conditition

I have below query -
select *
from g
right outer JOIN p
ON
( g.c1 = p.c1
or g.c2=p.c2
or g.c3=p.c3
)
OR condition in ON clause is causing serious performance issue.
I tried with UNION as -
select g.*,p.option_code
from g
right outer JOIN p
ON (g.c1= p.c1)
UNION
select g.*,p.option_code
from g
right outer JOIN p
ON (g.c2=p.c2)
UNION
select g.*,p.option_code
from g
right outer JOIN p
ON (g.c3=p.c3)
IS it possible to realise this join in alternative way.
Best Regards
First, right outer join is so hard to follow logically. So, let's replace this with a left join:
select *
from p left join
g
on g.c1 = p.c1 or g.c2=p.c2 or g.c3 = p.c3;
Assuming you want the first value that matches, you can do something like:
select p.*, coalesce(g1.col, g2.col, g3.col)
from p left join
g g1
on g1.c1 = p.c1 left join
g g2
on g2.c2 = p.c2 left join
g g3
on g3.c3 = p.c3;
You can add as many coalesce() expressions as you need.
This is not 100% the same as your query, but it works in many similar situations.
select distinct *
from
(
select g.*,p.option_code
from g
right outer JOIN p
ON (g.c1= p.c1)
UNION all
select g.*,p.option_code
from g
right outer JOIN p
ON (g.c2=p.c2)
UNION all
select g.*,p.option_code
from g
right outer JOIN p
ON (g.c3=p.c3)
) x

SQL syntax error with joins

Can someone point our what is wrong with below query? I have 3 tables.
1. Order Header - Order_header_id
2. Order Pricelist - order_header_id(fk),pricelist_id(fk)
3. Pricelist - pricelist_id,Name
I am trying to get the pricelist name based on the order header using below query and it throws "missing right parenthesis" error. Not sure if I am in the right direction.
Select pricelist.Name from ORDER_HEADER
left outer join
(select order_pricelist.pricelist_id from order_pricelist on order_header.order_header_id = order_pricelist.order_header_id
left outer join
(select pricelist.name from pricelist) pricelist on order_pricelist.pricelist_id = pricelist.pricelist_id)
Thanks
Select pricelist.Name from ORDER_HEADER
left outer join
(select pricelist_id, order_header_id from order_pricelist) op
ON order_header.order_header_id = op.order_header_id
left outer join
(select name,pricelist_id from pricelist) pr
ON op.pricelist_id = pr.pricelist_id
But actually, you don't need subqueries here, so it can be like this:
Select pricelist.Name from ORDER_HEADER
left outer join order_pricelist op on order_header.order_header_id = op.order_header_id
left outer join pricelist pr on op.pricelist_id = pr.pricelist_id

Join on 3 tables not working properly

I'm trying to get members according to business and subscriber id. But this is not working properly. Please help me to solve this.
SELECT distinct m.Fname,m.Created,m.[User_Name],b.Business_Name
FROM dbo.Members m,dbo.Business b,dbo.Assign_Business a
WHERE m.Subcriber_id=a.Subscriber_id
AND a.Business_id=b.id
AND b.Subcriber_id=a.Subscriber_id
AND a.Business_id='6' AND a.Subscriber_id='1'
Try This..
select distinct m.Fname,m.Created,m.[User_Name],b.Business_Name
from dbo.Members m
left join dbo.Assign_Business a on m.Subcriber_id=a.Subscriber_id
left join dbo.Business b on a.Business_id=b.id
where a.Business_id='6' and a.Subscriber_id='1' and b.Subcriber_id=a.Subscriber_id
First step, format your code cleanly. This will help greatly with debugging.
SELECT
distinct m.Fname,
m.Created,
m.[User_Name],
b.Business_Name
FROM
dbo.Members as M
inner join dbo.Assign_Business as A on M.Subscriber_Id = A.Subscriber_id
inner join dbo.Business as B on B.id = A.Business_id and B.Subscriber_id = A.Subscriber_id
WHERE
A.Business_ID = '6'
and A.Subscriber_id = '1'
It's hard to tell without seeing your tables' DDL but you can try
SELECT DISTINCT
m.Fname,m.Created,m.[User_Name],b.Business_Name
FROM dbo.Business b INNER JOIN
dbo.Assign_Business a ON a.Business_id=b.id AND
a.Subscriber_id=b.Subcriber_id INNER JOIN
dbo.Members m ON m.Subcriber_id=a.Subscriber_id
WHERE a.Business_id=6 AND
a.Subscriber_id=1
Try the following:
SELECT DISTINCT
m.Fname,
m.Created,
m.[User_Name],
b.Business_Name
FROM dbo.Members m
INNER JOIN dbo.Assign_Business a ON m.Subcriber_id=a.Subscriber_id
INNER JOIN dbo.Business b ON a.Business_id=b.id AND b.Subcriber_id=a.Subscriber_id
WHERE
a.Business_id='6' AND a.Subscriber_id='1'