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

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);
};

Related

How to use cross apply with Sql Kata

I am dont know to construct a query with Cross Apply and SqlKata. I was searching the net and I find out that cross apply was not supported by SqlKata. Is there any other way to achieve my requirements.
var query = new Query("Test as t")
cross apply
(select top 1 t2.TestID from Test as t2 where t1.LegID = t2.LegID order by t2.Sequence desc)
This is the sql query
select * from Test1 t1
cross apply
(select top 1 s.OperationalStopID
from Test2 t2
where t2.LegID = t1.LegID
order by t2.SequenceNumber desc) t3
Just use the Join method and pass "CROSS APPLY" or whatever method you like in the last parameter
check this example on SqlKata Playground
var another = new Query("Test2 as t2")
.WhereColumns("t2.LegID", "=", "t1.LegID")
.Select("s.OperationalStopID")
.OrderByDesc("t2.SequenceNumber")
.Limit(1);
var query = new Query("Test1 as t1").Join(another.As("t3"), j => j, "CROSS APPLY"); // <---
I am not familiar with sqlkata, but you can rewrite your SQL query to an inner join, like:
select DISTINCT
t1.I,
t1.LegID,
x.OperationalStopID
from test1 t1
inner join (select LegID, MAX(OperationalStopID) as OperationalStopID
from test2
group by LegID) x on x.LegID = t1.LegID
I hope you are able to convert this SQL query to sqlkata syntax ?
This query is tested here: DBFIDDLE
P.S.: oops, the DISTINCT should not have been in there anymore, please remove it.

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)
)

Run second query if first query does not return any results and return

I am trying to keep this to the minimal table queries to ensure less database usage. I am using Microsoft SQL Server Management Server.
I have a query which will sometimes return nothing depending on the user's current status. If this first query does not return any results, I would like the second query to run.
This is inside of a function and requires returning a single row of column data. I will include the queries with names changed for an example. I will do my own optimization after to make a temp table so the database is not accessed as often. I just need to figure out how to make this work first.
-- Query #1
INSERT #tlbReturn (returnInfo1, returnInfo2)
SELECT TOP(1) returnInfo1, returnInfo2
FROM table1 AS t1a
INNER JOIN table1 AS t1b ON t1a.someData1 = t1b.someData1
AND t1a.someData2 = t1b.someData2
AND t1a.someData3 = t1b.someData3
AND t1a.someData4 = t1b.someData4
INNER JOIN table2 AS t2 ON t2.someData6 = t1b.someData7
AND t2.someData8 = t1b.someData9
WHERE t1a.someData10 = 'value'
AND t1b.someData11 IN ('value1', 'value2')
ORDER BY t1b.someDate DESC;
-- Query #2
INSERT #tlbReturn (returnInfo1, returnInfo2)
SELECT TOP(1) returnInfo1, returnInfo2
FROM table1 AS t1a
INNER JOIN table1 AS t1b ON t1a.someData1 = t1b.someData1
AND t1a.someData5 = t1b.someData5
INNER JOIN table2 AS t2 ON t2.someData6 = t1b.someData7
AND t2.someData8 = t1b.someData9
WHERE t1a.someData10 = 'value'
AND t1b.someData11 IN ('value1', 'value2')
ORDER BY t1b.someDate DESC;
In theory I would like something like,
IF EXISTS(QUERY1) THEN RETURN
ELSE RETURN QUERY2
Check the value of ##ROWCOUNT after the first query.
Do a first select, check ##ROWCOUNT and if it's a zero do a second select

Linq Left Join returns repeated same rows for all set

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.

WHERE NOT EXISTS multiple conditions

How Do I set multiple AND conditions?
ex.
SELECT *
FROM CONFIRMED
WHERE NOT EXISTS
(
SELECT *
FROM Import_Orders
WHERE Import_Orders.Customer = CONFIRMED.Customer
AND Import_Orders.Reference = CONFIRMED.Reference
AND Import_Orders.[Index] = CONFIRMED.[Index]
AND Import_Orders.QuantityToDeliver = CONFIRMED.QuantityToDeliver
AND Import_Orders.DateToDeliver = CONFIRMED.DateToDeliver
);
I know this works on my tables with one WHERE & AND condition but not with several.
I Need a result of two tables where the above conditions do not match. I do not have identical keys in the two tables. Now with this code I get all the results that are in table CONFIRMED.
Here is the syntax for multiple tables:
WHERE NOT EXISTS (...) AND NOT EXISTS (...) AND NOT EXISTS (...)
However, if the database is so large that you care about performance, you'll need a much less obvious syntax along the following lines:
LEFT JOIN Some_Table t ON (t.xxx = Main_Table.xxx)
LEFT JOIN Another_Table t2 ON (t2.xxx = Main_Table.xxx)
LEFT JOIN Yet_Another_Table t3 ON (t3.xxx = Main_Table.xxx)
...
WHERE t.id IS NULL AND t2.id IS NULL AND t3.id IS NULL
For one table and one composed condition, like in the SQL sample in your question:
LEFT JOIN Some_Table t ON
t.xxx = Main_Table.xxx
AND t.yyy = Main_Table.yyy
AND t.zzz = Main_Table.zzz
WHERE t.id IS NULL
This is expected to return rows that exist in Main_Table but do not have matching rows in Some_Table, assuming the columns xxx, etc., are non-nullable.
If, for example, xxx is nullable, here is how you need to modify the query further:
LEFT JOIN Some_Table t ON
(t.xxx = Main_Table.xxx OR (t.xxx IS NULL AND Main_Table.xxx IS NULL))
AND t.yyy = Main_Table.yyy
AND t.zzz = Main_Table.zzz
WHERE t.id IS NULL
I am guessing that you have an ID on Import_Orders, if not use any field name that is turning up empty on the query. You would be better using field names rather than *. I have added an example for Import_Orders.
SELECT CONFIRMED.*, Import_Orders.ID, Import_Orders.Customer
FROM CONFIRMED
LEFT JOIN Import_Orders
ON Import_Orders.Customer = CONFIRMED.Customer
AND Import_Orders.Reference = CONFIRMED.Reference
AND Import_Orders.[Index] = CONFIRMED.[Index]
AND Import_Orders.QuantityToDeliver = CONFIRMED.QuantityToDeliver
AND Import_Orders.DateToDeliver = CONFIRMED.DateToDeliver
WHERE Import_Orders.ID Is Null
More information
Fundamental Microsoft Jet SQL for Access 2000
Intermediate Microsoft Jet SQL for Access 2000
Advanced Microsoft Jet SQL for Access 2000
You could just replace all the "=" with "<>" and you should get all the results that don't have a match on all criteria.
SELECT *
FROM CONFIRMED
WHERE EXISTS
(
SELECT *
FROM Import_Orders
WHERE Import_Orders.Customer <> CONFIRMED.Customer
AND Import_Orders.Reference <> CONFIRMED.Reference
AND Import_Orders.[Index] <> CONFIRMED.[Index]
AND Import_Orders.QuantityToDeliver <> CONFIRMED.QuantityToDeliver
AND Import_Orders.DateToDeliver <> CONFIRMED.DateToDeliver
);