I have a T-SQL query that I need to change to an Access query. I tried a lot of times to change it but no success
UPDATE T1
SET Cap = ((txg*T2.Cap)/100 ),
IPi=(case when t2.TPi=1 then ((txg*T2.IPiTT)/100) else T1.IPi end)
FROM prg T1
JOIN main T2
ON T1.id = T2.id
where T1.Cap=0
Use IIF in Access SQL:
UPDATE
T1
SET
Cap = txg * T2.Cap / 100,
IPi = IIF(T2.TPi = 1, txg * T2.IPiTT / 100, T1.IPi)
FROM
prg AS T1
INNER JOIN
main AS T2
ON T1.id = T2.id
WHERE
T1.Cap = 0
Related
I want to get a result by typing subquery into the sum query.
The following code works when I write to Sql.
But on EF, how do I add another select at the top?
SQL
This code is work.
select
sum (data.rate)
from
(
SELECT
t1.Id,
(c.rate /
(SELECT COUNT(1) FROM [table4] AS [t4] WHERE ([t4].[FKId] = p1.Id))) as rate
FROM [table1] AS [t1]
INNER JOIN [table2] AS [t2] ON ([t1].[FKId] = [t2].[Id])
INNER JOIN [table3] AS [t3] ON ([t1].[FKId] = [t3].[Id]))
as data
C#
var data = await (
????
from t1 in ctx.table1
join t2 in ctx.table2 on new { t1.FKId} equals new { FKId = t2.Id}
join t3 in ctx.table3 on new { t1.FKId} equals new { FKId = t3.Id}
select new
{
rate = t3.Rate /
(from t4 in ctx.table4
where t4.FKId == t2.Id
select t4.Id)
.Count())
})
.SumAsync(sm => (double?)sm.rate ?? 0);
This c# code is not working.
Error:
Cannot perform an aggregate function on an expression containing an
aggregate or a subquery.
Per this answer, EF Core 3.0 still can't handle this situation (!).
However, you can translate the query without a subquery (effectively, using SelectMany) and it should work:
var ans = await (from t1 in ctx.table1
join t2 in ctx.table2 on t1.FKId equals t2.Id
join t3 in ctx.table3 on t1.FKId equals t3.Id
from t4 in ctx.table4
where t4.FkId == t2.Id
group t4.Id by new { t3.rate, t2.Id } into t2g
select t2g.Key.rate / t2g.Count()
)
.SumAsync();
I need to update values in a table from another table and if they don't exist it must be inserted. So I saw this example in here
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
IF ##ROWCOUNT=0
INSERT INTO Table1 VALUES (...)
Now I would like to do this but instead of a where clause with a value i need to check if columns match on another table.
Something like
UPDATE t1
SET
t1.status = t2.status
FROM
table1 t1, table2 t2
WHERE t1.reference = t2.reference and t1.section = t2.section
I assume this is very bad in terms of performance and i would like to hear some other suggestions.
EDIT
Also please, if you can, i would like to hear why the approach you give is better than using a merge or a join for example. Much appreciated.
RESULT
Here is the approach i went for after checking how bad merge performance is compared to an update then insert and trying to test everyones approach this is what i decided for, basically uses a bit of everyone's answer
UPDATE C
SET
C.ConsumoWeek01 = M.ConsumoWeek01
FROM
Consumos AS C
INNER JOIN #tempTable M
ON C.Referencia = M.Referencia
AND C.UAP = M.UAP
INSERT INTO
Consumos
SELECT *
FROM #tempTable M
WHERE NOT EXISTS
(
SELECT 1 FROM Consumos C
WHERE C.Referencia = M.Referencia
AND C.UAP = M.UAP
)
Try this
UPDATE t1
SET
t1.status = t2.status
FROM
table1 t1
WHERE NOT EXISTS (SELECT 1 FROM table2 t2
WHERE t1.reference = t2.reference
and t1.section = t2.section )
use not exists
UPDATE t1
SET
t1.status = t2.status
FROM
table1 t1 where not exists( select 1 from table2 t2 where t1.=ref=t2.ref and t1.section=t2.secction)
Use merge statement:
MERGE INTO Table1 T1
USING
(
SELECT * FROM Table2
) T2
ON
(
T1.reference = T2.reference AND
T1.section = T2.section
)
WHEN MATCHED THEN
--UPDATE STATEMENT
WHEN NOT MATCHED THEN
--INSERT STATEMENT
Your method is fine just use standard explicit join syntax instead of old style with comma separate :
update t1
set . . .
from table t1 inner join
table t2
on t1.reference = t2.reference and t1.section = t2.section;
I have two tables. I want to update table1 when the condition is satisfied. The condition here is to check the country in table 2 and if its Mex, then multiply rate i.e. 0.5 to the price.
I wrote the following code
UPDATE table1
SET table1.Price = (SELECT *,
CASE table2.Country
WHEN 'CANADA' THEN (1 * table2.price)
WHEN 'MEXICO' THEN (0.5 * table2.price)
ELSE 0
END AS Price_Calc
FROM table2)
FROM table1;
As I run this it gives the below error
Msg 116, Level 16, State 1, Line 12 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
Try Like below
UPDATE t1
SET t1.table1.Price = (SELECT
CASE t2.Country
WHEN 'CANADA' THEN (1 * t2.price)
WHEN 'MEXICO' THEN (0.5 * t2.price)
ELSE 0
END AS Price_Calc
FROM table2 t2
WHERE t2.Id = t1.Id -- Here it is suggestion to update target
-- column based on relation if exists between
-- ur tables
)
FROM table1 t1;
Assuming Table1 and Table2 are related through IDs:
UPDATE t1 SET t1.Price = t2.Price
* CASE t2.Country
WHEN 'CANADA' THEN 1.
WHEN 'MEXICO' THEN .5
ELSE 0
END
FROM table1 t1
INNER JOIN table2 t2 ON t2.Id = t1.Id
;
I have below query which has multiple nested subqueries.
The requirement is to convert all the nested subqueries into joins with main outer select query.
select * from t1
where not exists (
select 1 from t2
join t21 on t21.c = t2.c
where t2.y = t1.y
and t2.x in (
select top 1 x from t3
join t31 on t31.d = t3.d
where t3.z = t2.z
and t3.a = t1.a
order by t3.b
)
)
Can anyone please help me achieve this?
I have three tables
table1 -> xt1, yt1, zt1;
table2 -> xt2
table3 -> yt3, zt3
SELECT xt1, yt1, zt1
From table1, table3
Where xt1
NOT IN
(SELECT DISTINCT table1.xt1 FROM table2 INNER JOIN table1 ON
table1.xt1 = Replace(table2.xt2,',',''))
And table1.yt1 = table3.yt3
AND table1.zt1 = table3.zt3
it is working correctly but i take long time.
if i replace NOT IN with Not exists it return empty set.
SELECT xt1, yt1, zt1
From table1, table3
Where Not exists
(SELECT DISTINCT table1.xt1 FROM table2 INNER JOIN table1 ON
table1.xt1 = Replace(table2.xt2,',',''))
And table1.yt1 = table3.yt3
AND table1.zt1 = table3.zt3
the results of the second select should be 6 rows but it returns notiong with not exists.
also if i tried to change the compare part to
table1.xt1 != Replace(table2.xt2,',','') and remove the NOT IN
select it get outof memory error.
So is this the best way to write my query and why it return empty set with Not exists
thank you.
Ok, first of all, I changed your implicit join to an explicit one. Then I fixed the NOT EXISTS so it correlates to the outer table1:
SELECT t1.xt1, t1.yt1, t1.zt1
FROM table1 AS t1
INNER JOIN table3 AS t3
ON t1.yt1 = t3.yt3
AND t1.zt1 = t3.zt3
WHERE NOT EXISTS ( SELECT 1
FROM table2 AS t2
INNER JOIN table1 AS t1_1
ON t1_1.xt1 = REPLACE(t2.xt2,',','')
AND t1_1.xt1 = t1.xt1) ;
which can be simplified further to:
SELECT t1.xt1, t1.yt1, t1.zt1
FROM table1 AS t1
INNER JOIN table3 AS t3
ON t1.yt1 = t3.yt3
AND t1.zt1 = t3.zt3
WHERE NOT EXISTS ( SELECT 1
FROM table2 AS t2
WHERE t1.xt1 = REPLACE(t2.xt2,',','')
) ;
You need to select IN or exist depending upon the size of inner query. when there is a outer query and inner-sub-query, if the result of sub query is small, In is preferred as outer query is selected based upon result of sub-query.
if the result of sub-query is large, exist is preferred as outer query is evaluated first.