Want to change SQL query to LINQ - linq

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;

Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!

if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

Related

Oracle Hierarchical queries: Translate START WITH ... CONNECT BY PRIOR into 'Recursive Subquery Factoring'

How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.

Comparing two columns one of which is null

I have three tables as follow
TABLE1
ID VALUE
1 NULL
TABLE2
ID VALUE
1 1
TABLE3
ID VALUE
1 10
I'm trying to do the following:
Compare the two fields (ID) in tables 1 & 2 if one is null return null then return value from TABLE3.
It seems you are looking for something like this:
select table3.id, table3.value
from table1 join table2 on table1.id = table2.id
join table3 on table1.id = table3.id
where table1.value is null or table2.value is null
If this isn't what you had in mind, you need to give more details about your requirement.
Try this query:
SELECT t1.id,
(CASE WHEN t1.value IS NULL OR t2.value IS NULL
THEN t3.value
END) VALUE
FROM table_1 t1
JOIN table_2 t2
ON t1.id = t2.id
JOIN table_3 t3
ON t1.id = t3.id

2x COUNT in HAVING clause

I have following problem:
I want to count data in one table, count data in second table and compare countings in having clause, and display only that rows which have the same countings
Something like that:
SELECT bla
FROM T1 t1 JOIN T2 t2
ON t1.id = t2.id
HAVING COUNT(counted data from table1) = COUNT(counted data from table2)
Do you have any idea?
Cheers
Standard SQL:
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) AS t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) AS t2
ON t1.id = t2.id AND t1.counter = t2.counter
Oracle SQL (because Oracle doesn't like AS before table aliases):
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) t2
ON t1.id = t2.id AND t1.counter = t2.counter
You just have to decide where bla comes from; I nominated t1. I'm assuming that for any given value of t1.id, there is a single value of t1.bla. If there isn't, then you need to explain much more clearly what you're counting and where the various columns are, and what the keys of the tables are.
Update: Apologies for not noticing the Oracle tag and giving invalid Oracle syntax.
WITH jezyki as
(SELECT pseudo_wampira, COUNT(*) AS counter
FROM Jezyki_obce_w
GROUP BY pseudo_wampira
)
,sprawnosc as
(SELECT pseudo_wampira, sprawnosc, COUNT(*) AS counter
FROM Sprawnosci_w
GROUP BY pseudo_wampira, sprawnosc
)
SELECT jezyki.pseudo_wampira, sprawnosc.counter
FROM jezyki,sprawnosc
WHERE jezyki.pseudo_wampira = sprawnosc.pseudo_wampira
AND jezyki.counter = sprawnosc.counter

Oracle: Using results of one subquery inside another

select t1Joint2.c1,t1Joint3.c3 from
(select * from table1 where name = "some") t1,
(select t1.c1,t2.c2 from table2 t2 where t1.c1 = t2.c2) t1Joint2,
(select t1.c1,t3.c3 from table3 t3 where t1.c1 = t3.c3) t1Joint3,
;
The above query doesn't work in Oracle.
Any workaround?
Using Oracle 11g.
Why not write it as:
select t1.c1, t3.c3
from table1 t1
join table2 t2 on t1.c1 = t2.c2
join table3 t3 on t1.c1 = t3.c3
where name = "some"
with t1 as
(select * from table1 where name = 'some')
, t1Joint2 as
(select t1.c1,t2.c2 from t1, table2 t2 where t1.c1 = t2.c2)
, t1Joint3 as
(select t1.c1, t3.c3 from t1, table3 t3 where t1.c1 = t3.c3)
select t1Joint2.c1,t1Joint3.c3
from t1Joint2, t1Joint3
;

How to use join on three tables

I have three tables
Table1 Table2 and Table3.
Table1 having column ID.
Table2 having column names ID,Name.
Table three having column name Name.
Now i want to retrive ID from table1 which is there in Table2 by so that the name associated with ID in table to should be in Table3.
Table1.ID=Table2.ID(Table2.Name=Table3.Namw).
Without using IN operator.Only joins.
select table1.id, table2.name
from table1
join table2 on table2.id = table1.id
join table3 on table3.name = table2.name
select distinct t1.ID
from Table1 t1
,Table2 t2
,Table3 t3
where t1.ID = t2.ID
and t2.Name = t3.Name
;
or
select t1.ID
from Table1 t1
where exists (
select 1
from Table2 t2
,Table3 t3
where t1.ID = t2.ID
and t2.Name = t3.Name
);

Resources