Oracle inner join give wrong result set - sql

When I execute this query it gives a 10 result set .
select * from OA_SERVICE_REQUESTS WHERE
OA_SERVICE_REQUESTS.CUSREG_ID=4
But when I join with other table with to get more information, I use 2 inner join because this is 2 foreign key from ELVM_SMUNT_CUS table it gives me 120 results
select * from OA_SERVICE_REQUESTS
inner join ELVM_SMUNT_CUS T1 on OA_SERVICE_REQUESTS.DIVCOD = T1.DIVCOD
inner join ELVM_SMUNT_CUS T2 on OA_SERVICE_REQUESTS.UNTNUM = T2.UNTNUM
WHERE OA_SERVICE_REQUESTS.CUSREG_ID=4

Try to combine them together :
select * from OA_SERVICE_REQUESTS R
inner join ELVM_SMUNT_CUS T1 on ( R.DIVCOD = T1.DIVCOD
and R.UNTNUM = T1.UNTNUM )
where R.CUSREG_ID=4;
for your query not to produce cross-product results.
Probably, you have 12 matching records for R.DIVCOD = T1.DIVCOD, and 10 matching records for R.UNTNUM = T1.UNTNUM for R.CUSREG_ID=4, by combining the result set by an and you can have 10results at the same time, but may yield 120 occurences by 12 times 10, if conditions are taken apart by more joins.

Related

SQL Server mismatch row count in SELECT and UPDATE query with same conditions

I am trying to match the update and select query count with same condition. but row count is coming with big difference.
UPDATE Query:
UPDATE MKP set MKP.Quantity = MKP.Quantity + LQD.Quantity, ModifiedDate = GETDATE()
FROM IM_MarketPlace MKP
INNER JOIN IM_ChannelListings CL ON MKP.ListingID = CL.ListingID
INNER JOIN #ListingQuantityData LQD ON LQD.ChanelListingID = RTRIM(LTRIM((CL.ChannelListingID))) and LQD.SalesChannelID = CL.ChannelID
Left outer join IM_ListingVariations LV on LV.ListingCode = RTRIM(LTRIM((LQD.VariationSKU))) and MKP.ListingVariationID = LV.ID and CL.ListingID=LV.ListingID
WHERE MKP.IsActive =1 and MKP.IsDeleted=0 and CL.IsActive =1 and CL.IsDeleted=0
AND (LQD.VariationSKU is null OR (LQD.VariationSKU = LV.ListingCode and lv.ID = MKP.ListingVariationID))
Select Query
select count(mkp.ListingID) FROM IM_MarketPlace MKP
INNER JOIN IM_ChannelListings CL ON MKP.ListingID = CL.ListingID
INNER JOIN #ListingQuantityData LQD ON LQD.ChanelListingID = RTRIM(LTRIM((CL.ChannelListingID))) and LQD.SalesChannelID = CL.ChannelID
Left outer join IM_ListingVariations LV on LV.ListingCode = RTRIM(LTRIM((LQD.VariationSKU))) and MKP.ListingVariationID = LV.ID and CL.ListingID=LV.ListingID
WHERE MKP.IsActive =1 and MKP.IsDeleted=0 and CL.IsActive =1 and CL.IsDeleted=0
AND (LQD.VariationSKU is null OR (LQD.VariationSKU = LV.ListingCode and lv.ID = MKP.ListingVariationID))
Please help me out for this.
and also please let me know how ##rowcount will work for update query.
this will happen if there is a one to many relationship between at least two of the tables involved in the joins.
The SELECT will count all rows including those multiplied out by the join. The UPDATE will just count the unique rows in IM_MarketPlace affected by the UPDATE.
Where there is a one to many relationship it is not deterministic which of the "many" rows joining to a specific row in IM_MarketPlace are used as the source in the update for that row.

Column in SQL writes for too many times

I'm trying to make a query which shows the name of the guys(Angajat.nume and Angajat.prenume) and the salary (Angajat.salar) working at a restaurant (Restau.nume) and instead of writing it for 5 times, it writes for 25 times. I have 5 workers(id_a) and 1 restaurant(id_r) and 5 menu(id_m). How can I make it to show only for 5 times and keep the Meniu table in the query?
My Relations and Data Base
My Query
SELECT
Restau.nume, Angajat.nume AS Expr1, Angajat.prenume
FROM
Restau
INNER JOIN
Meniu ON Restau.id_r = Meniu.id_r
INNER JOIN
Angajat ON Restau.id_r = Angajat.id_r
You are using an unuseful join
SELECT
Restau.nume
, Angajat.nume AS Expr1
, Angajat.prenume
FROM Restau
INNER JOIN Angajat ON Restau.id_r = Angajat.id_r
add distinct if you have duplicated result
SELECT DISTINCT
Restau.nume
, Angajat.nume AS Expr1
, Angajat.prenume
FROM Restau
INNER JOIN Angajat ON Restau.id_r = Angajat.id_r

syntax error when joining tables

I am trying to merge multiple tables. This is how I am doing it:
CREATE TABLE big3 AS SELECT *
FROM trainSearchStream a
LEFT OUTER JOIN SearchInfo b ON b.SearchID=a.SearchID LIMIT 3
LEFT OUTER JOIN AdsInfo c ON c.AdID=a.AdID LIMIT 3;
However, I get this error:
Error: near "LEFT": syntax error
As jarlh already mentioned, there can be only one LIMIT per select statement.
So this is forbidden:
select *
from a limit 5
join b limit 6 an a.x = b.x;
But this would be allowed:
select *
from (select * from a limit 5) alim
join (select * from b limit 6) blim on alim.x = blim.x;
As you simply want to test your query however, I'd suggest, you take a sample from trainSearchStream to test it. The modulo operator % is great for taking samples:
CREATE TABLE big3 AS SELECT *
FROM (select * from trainSearchStream where searchid % 12345 = 6789) a
LEFT OUTER JOIN SearchInfo b ON b.SearchID = a.SearchID
LEFT OUTER JOIN AdsInfo c ON c.AdID = a.AdID;
Choose whatever numbers you like for the modulo operation. Above statement divides your trainSearchStream count by about 12345 (provided the IDs are evenly spread).

SQL Server : FULL OUTER JOIN unusual behavior

I have 2 tables. I want to get the records from both the table using FULL OUTER JOIN while filtering out some data from one of the tables.
14026 - Total number of common records.
8428 - unique records in table 1
1512 - unique records in table 2.
The total records that I should get are 23966. However, the following queries return different results.
Query 1 -
SELECT DISTINCT pu.id,
vm.id
FROM table1 vm
FULL OUTER JOIN (SELECT DISTINCT pu.id
FROM table2 pu
WHERE pu.column1 = 'filter1'
AND pu.column2 = 'filter2') pu ON vm.id = pu.id;
Output - 23966 rows (expected).
Query 2 -
SELECT DISTINCT pu.id,
vm.id
FROM table1 vm
FULL OUTER JOIN table2 pu ON vm.id = pu.id AND pu.column1 = 'filter1' AND pu.column2 = 'filter2';
Output - 48804 rows.
Query 3 -
SELECT DISTINCT pu.id,
vm.id
FROM table1 vm
FULL OUTER JOIN table2 pu ON vm.id = pu.id
WHERE (pu.column1 = 'filter1' AND pu.column2 = 'filter2')
OR (pu.column1 IS NULL AND pu.column2 IS NULL);
Output - 21830.
According to my understanding at least the third query should also give me the same result as query 1. Can someone explain me how SQL Server is treating these 3 queries?

SQL join 3 table

i m using SQL 2008 R2
i got duplicate value from my join table:
SELECT *
FROM LETTRE_VOIT
LEFT JOIN FAWEB_CLIENT ON FAWEB_CLIENT.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
LEFT JOIN ORDRE ON ORDRE.CODE_DEST = LETTRE_VOIT.CODE_DEST AND ORDRE.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
AND ORDRE.DATE_CLOTUR = LETTRE_VOIT.DATE_CLOTURE
WHERE LETTRE_VOIT.NO_ORDRE IN ('5530','5533')
as you can see on image i got double value of 5530 and 5533.
my table FAWEB_CLIENT with ID Code_Client
table LETTRE_VOIT with ID NOID and NO_ORDRE
table ORDRE with ID NO_ORDRE and NO_CLIENT
i can not use DISTINCT:
error message: The text data type can not be selected as DISTINCT because it is not comparable
I suspect the problem is that your joins are not correct. My suspicion is that you are missing a join condition on Letter_Voit.No_Ordre:
SELECT *
FROM LETTRE_VOIT
LEFT JOIN FAWEB_CLIENT ON FAWEB_CLIENT.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
LEFT JOIN ORDRE ON ORDRE.CODE_DEST = LETTRE_VOIT.CODE_DEST AND ORDRE.CODE_CLIENT = LETTRE_VOIT.CODE_CLIENT
AND ORDRE.DATE_CLOTUR = LETTRE_VOIT.DATE_CLOTURE and
LETTRE_VOIT.No_ORDRE = Order.No_ORDRE
WHERE LETTRE_VOIT.NO_ORDRE IN ('5530','5533')
You may be able to remove some of the other join conditions, which may become redundant.