Linq Left Join returns repeated same rows for all set - sql

my linq returns all repeated same rows for all set
run SQL in DB :
select top 20 * from t1
left join t2
on t1.sid= t2.sid
and t1.pid=t2.pid
where(t2.sid is null and t1.pid='r')
i can get 20 different rows of result.
then i write Linq:
Entities dbconn = new Entities();
List<t1> myResult = (
from t1Data in dbconn.t1
join t2Data in dbconn.t2
on new { sid = (int)t1.sid, pid= t1.pid}
equals new { sid= (int)t2.sid, pid= t2.pid}
into joinSet
from joinUnit in joinSet.DefaultIfEmpty()
where (joinUnit == null) && (t1.pid== "r")
select t1Data
).Take(20).ToList();
all rows of result are the some row.

select t1Data is wrong as t1Data is from the original dataset.
Instead, select the joined result: select joinSet.

Related

how to convert postgres query looking for records where value appears only once to knex

I am trying to convert a postgres valid query that returns what I require to a knex query...
I keep getting various errors and can't write the knex query properly.
I have several versions of the query in postgres to find records in a table that appear only once and that have a specific user_id.
here are the different variants:
select * from project_authors t1
where (select count(*) from project_authors t2
where t1.project_id = t2.project_id) = 1
AND t1.user_id=2
OR
select * from project_authors t1
where NOT exists
(select 1 from project_authors t2
where t1.project_id = t2.project_id and t1.user_id <> t2.user_id) AND t1.user_id=2
OR
select * from project_authors t1
INNER JOIN (SELECT t3.project_id FROM project_authors t3 GROUP BY t3.project_id
HAVING COUNT (t3.project_id)=1) t2
ON t1.project_id=t2.project_id Where t1.user_id=2
the way I write the knex query is using parameters supplied in the request (for example):
const _readDbSingleAuthor = (table1, column1, criteria) => {
return db(`${table1} as t1`)
.select("*")
.where(
db(`${table1} as t2`)
.select("*")
.count("*")
.where(`t1.${column1}`, "=", `t2.${column1}`),
"=",
1
)
.andWhere(criteria);
I would appreciate any help...
the version above gave me a "maximum call stack exceeded" - so I hit the stackoverflow literally.
a version using join was the closest thing but gave me a result saying that the project_id column was ambiguous:
const _readDbSingleAuthor = (table1, data, column1, criteria) => {
return db
.select(data)
.from(`${table1} as t1`)
.join(
db
.select(column1)
.from(`${table1} as t3`)
.count(`${column1} as count`)
.groupBy(column1)
.having("t3.count", "=", 1)
.as("t2"),
`t2.${column1}`,
"=",
`t1.${column1}`
)
.where(criteria);
};

How to write a query to get data count with combination of codision

I have two tables named [DrugPrescriptionEdition] and [PrescriptionDoseDetail] and now, I join that two tables using the below query and taking a result set.
select * from DrugPrescription dp where id in(
SELECT distinct dpe.template
FROM [DrugPrescriptionEdition] dpe
join PrescriptionDoseDetail pdd on pdd.prescription = dpe.id
where doseEnd_endDate is NULL and doseEnd_doseEndType =1
)
but now I want to take records only contain, (1,2) combination of 'datasource' column and prescription.id should be same.
Example : like records { prescriptionID =4 and there contain ,(1,2) }. I will not consider, only 1 ,or 2 contain records.
Need some expert help to adding this conditions to my above query and modify it .
Expected result : I need to filter out , above query result using this, new condition too.
Let me assume your records are in a single table. Here is one method:
select t.*
from t
where (t.dataSource = 1 and
exists (select 1
from t t2
where t2. prescriptionid = t.prescriptionid and
t2.dataSource = 2
)
) or
(t.dataSource = 2 and
exists (select 1
from t t2
where t2.prescriptionid = t.prescriptionid and
t2.dataSource = 2
)
);
It is unclear if any other data sources are allowed. If they are not, then add:
and
not exists (select 1
from t t3
where t3.prescriptionid = t.prescriptionid and
t3.dataSource not in (1, 2)
)

Want to get only 1 record back from an inner join that can pass back multiple records

I have the following SQL query:
SELECT *
FROM My_TABL wr
INNER JOIN His_TABL pk ON (wr.Company = pk.company AND wr.NUMBER = pk.number)
WHERE wr.NUMBER = 'L00499233'
AND wr.S_CODE IN ('in', 'ji', 'je')
I want to get back 1 record but found out that it can pass back multiple records because a record could have more than 1 field with 'in', 'ji' and 'je'
How can I just pick the first one? Thanks.
If the goal is to join to the top 1 match on the join (ultimately returning several rows), use an OUTER APPLY:
SELECT *
FROM My_TABL wr
OUTER APPLY ( SELECT TOP 1 * FROM His_TABL pk WHERE wr.Company = pk.company
AND wr.NUMBER = pk.number ) AS pk2
WHERE wr.NUMBER = 'L00499233'
AND wr.S_CODE IN ( 'in', 'ji', 'je' );
However, if the goal is to return only a single row in your result set, use Stuart's suggestion.
If it doesn't matter which row you want, you can use TOP 1:
select TOP 1 * from My_TABL wr
inner join His_TABL pk on (wr.Company = pk.company and wr.NUMBER = pk.number)
where wr.NUMBER = 'L00499233' and wr.S_CODE in ('in', 'ji', 'je')
Note that you should get out of the habit of using SELECT * and be more precise about the rows and columns you want to retrieve.

update rows from joined tables in oracle

I'm trying to migrate some tables into an existing table, I need to perform the updates only where DET_ATTACHMENT_ID equals DET_ATTACHMENT.ID, here's the query I have so far.
UPDATE DET_ATTACHMENT
SET attachment_type = 'LAB', -- being added by the query, to replace the table difference
payer_criteria_id = (
SELECT PAYER_CRITERIA_ID
FROM DET_LAB_ATTACHMENT
WHERE DET_LAB_ATTACHMENT.DET_ATTACHMENT_ID = DET_ATTACHMENT.ID)
WHERE exists(
SELECT DET_ATTACHMENT_ID
FROM DET_ATTACHMENT
JOIN DET_LAB_ATTACHMENT ON (ID = DET_ATTACHMENT_ID)
WHERE DET_ATTACHMENT_ID = DET_ATTACHMENT.ID
the problem with the existing query is that it's setting every row to have an attachment_type of "LAB", and nulling out the payer_criteria_id where it didn't match. What am I doing wrong?
The problem might be that your exists(...) predicate always evaluates to true, thus making the update run for all rows of det_attachment. Try it this way:
UPDATE DET_ATTACHMENT X
SET X.attachment_type = 'LAB',
X.payer_criteria_id = (
SELECT C.PAYER_CRITERIA_ID
FROM DET_LAB_ATTACHMENT C
WHERE C.DET_ATTACHMENT_ID = X.ID
)
WHERE
exists(
SELECT 1
FROM DET_ATTACHMENT A
JOIN DET_LAB_ATTACHMENT B
ON B.DET_ATTACHMENT_ID = A.ID
where B.det_attachment_id = X.id
)
;

Combine two different select statements, one distinct the other not

I have two selects which are required to filter data. They are not complicated:
"SELECT * FROM StevesTable t WHERE "
"t.data1 = '%s' AND "
"t.data2 = to_date('%s','DD/MM/YYYY');",
strdata1,
dtDate.Format();
and
SELECT distinct data1 FROM anothertable ftt
join table1 tab on tab.somedata = ftt.somedata
where tab.somedata = 0
and tab.someotherdata = 1
I would like to combine these two as I need to filter the returned dataset from the first select statement by the returned field in the second (ie if a record returned in the first set does not have a data1 value which is contained in the second returned set it is invalid).
I tried to union and intersect the selects but you need the same number of columns returned and that cannot happen as these are completely different tables. When I tried to simply merge them together I found it difficult as the second select statement is a distinct select whereas the first is not.
I was wondering whether I had missed a trick somewhere for combining these sorts of selects?
What you need is a SQL sub-query:
SELECT * FROM StevesTable t
WHERE t.data1 = '%s'
AND t.data2 = to_date('%s','DD/MM/YYYY')
AND t.data1 in (select distinct data1 FROM anothertable ftt
join table1 tab on tab.somedata = ftt.somedata
where tab.somedata = 0
and tab.someotherdata = 1)
There, you check that all records in the first select have a data1 value in the second set.
You can do this using an EXISTS condition:
SELECT * FROM StevesTable t
WHERE t.data1 = '%s' AND
t.data2 = to_date('%s','DD/MM/YYYY') AND
EXISTS (select null
from anothertable ftt
join table1 tab on tab.somedata = ftt.somedata
where tab.somedata = 0 and
tab.someotherdata = 1 and
ftt.data1 = t.data1)