Update query using IIf() in a WHERE condition - sql

I am writing an update query in Access which is based on a make table query.
In my temporary table (the output of my make table query), I have 3 fields [Tick], [DivLevel4] and [T]. I can have identical Ticks displaying different values for [DivLevel4] if [T] bears different values (only C or F):
Tick DivLevel4 T
AA 5 C
AA 0 F
BB 2 C
CC 3 C
CC 0 F
How can I incorporate this condition in my update query? I have tried adding an IIf() statement in the criteria field but it is not doing the job:
IIf([tblTrades].[T]="Y",0,[tbl_DIV_Temp].[[DivLevel4])
as in
UPDATE
tblTrades
INNER JOIN
tbl_DIV_Temp
ON tblTrades.Tick = tbl_DIV_Temp.Tick
SET tblTrades.DIV = [tbl_DIV_Temp].[DivLevel4]
WHERE (((tblTrades.DIV)=IIf([tblTrades].[T]="F",0,[tbl_DIV_Temp].[DivLevel4])));

Topic is close. Alternative method found.

Related

SQL Query with 2 joins and different values

I'm quite the beginner so I suppose some of you would have an easy time on my task but I need some help:
I have 3 DBs. dbo_A_Personal, dbo_Z_Ferien and dbo_Z_ERFASSUNG
A_Pers has a Pers_ID (LPE_ID) that I can use to join Z_Ferien and Z_ERFASSUNG on.
In Z_Ferien I have 4 rows with that pers_ID and in Z_ERFASSUNG 96.
What I need is a result that has columns that are basically like that:
PersID
Erf
Fer
1224
5
0
1234
4
0
1234
6
0
1234
0
6
so far I have this:
SELECT dbo_A_PERSONAL.LPE_ID, dbo_Z_Ferien.ZFE_TAGE, dbo_Z_ERFASSUNG.ZER_Std100
FROM dbo_A_PERSONAL
INNER JOIN dbo_Z_Ferien ON dbo_A_PERSONAL.LPE_ID = dbo_Z_Ferien.ZFE_LPE_ID
INNER JOIN dbo_Z_ERFASSUNG ON dbo_A_PERSONAL.LPE_ID = dbo_Z_ERFASSUNG.ZER_LPE
WHERE dbo_A_PERSONAL.LPE_ID=804 AND dbo_Z_ERFASSUNG.ZER_EIGENSCH = 3;
I need that so I can sum up the value I need from Z_ERFASSUNG and Z_Ferien but I don't know how to make it so each value is only "printed" once.
I hope I explained it well enough so you guys can help me out.
If I understand correctly an aggerate function is what you need here.
I added a sum function of both dbo_Z_Ferien & dbo_Z_ERFASSUNG, as well as adding a group by statement for LPE_ID. Which tells SQL to partition the sum only on LPE_ID
SELECT dbo_A_PERSONAL.LPE_ID, sum(dbo_Z_Ferien.ZFE_TAGE), sum(dbo_Z_ERFASSUNG.ZER_Std100)
FROM dbo_A_PERSONAL
INNER JOIN dbo_Z_Ferien ON dbo_A_PERSONAL.LPE_ID = dbo_Z_Ferien.ZFE_LPE_ID
INNER JOIN dbo_Z_ERFASSUNG ON dbo_A_PERSONAL.LPE_ID = dbo_Z_ERFASSUNG.ZER_LPE
WHERE dbo_A_PERSONAL.LPE_ID=804 AND dbo_Z_ERFASSUNG.ZER_EIGENSCH = 3
GROUP BY dbo_A_PERSONAL

sum of query result form two different table that have nothing in common

hi i have two query result:
Table A
Id u
1 50,00
2 60,00
3 70,00
and
Table B
id c
4 110,00
5 120,01
6 130,02
Now i have doing two query on this table and i want sum their query result.
I want update column c from table B with 160 that is sum of(110+50).
Table B
Id c
4 160,00
Table B and Table A they have nothing in common.
Now i have doing two query for select their value ed for sum two data:
$data=number_format($row['c']+$row1['u']);
$query_updatee="update B set c= (integer)$data where c=110,00";
Can i sum the data from two different table that don't have nothing in comon?
My output is pg_query(): Query failed: ERRORE: syntax error at or near "1" LINE 1: update B set punti = (integer)1680,00 where c=110,00 ^ in C:\xampp\htdocs\table_A.php on line 81
You can use a subquery to fetch the increment. The rest seems to just be filtering:
update B
set c = c + (select a.u from a where a.id = 1)
where B.id = 4;
Obviously, you can use where b.c = 110.00 if you want to filter by a number (or use a comma if that is how the database is set up).

Pulling "All but X" in a select statement, testing multiple fields at once

So after doing a very large select statement, I wanted to check if there was a slick way to pull all the fields paired on multiple tables into a report while testing a large set of null fields to have empty records removed. Say for example I have table a paired to table b paired to table c. I want almost all the records except for a.something, b.somthing, and a couple c.somethings.
I also want to make sure if all the fields in c are empty, exclude the record. (Well... all but the index)
Is there a good way to do this? I ended up building a largish report field by field but it was A: Mostly tedious and B: Would not scale if I ever ran into a bigger project.
SELECT * <except for c.4, c.5. c.6, a.3, a.4, b.2>
FROM a,b,c
LEFT JOIN b ON a.indexA = b.indexA
LEFT JOIN c ON b.indexB = c.indexB
WHERE a.1 is not null
AND b.1 is not null
and c.1 is not null
and c.2 is not null
and c.3 is not null
and a.2 is > 0
and b.2 = 'Test'
Feel free not to use my example.
You can actually do multiple join conditions:
SELECT *
FROM a
LEFT JOIN b ON a.indexA = b.indexA
and b.1 is not null
and b.2 = 'Test'
LEFT JOIN c ON b.indexB = c.indexB
and c.1 is not null
and c.2 is not null
and c.3 is not null
WHERE a.1 is not null
and a.2 is > 0
Also, I'm pretty sure when specifying the left join syntax as you have, listing all of the tables after the FROM is not necessary.
I'm not sure if this will change the performance at all however.

Difference in NA/NULL treatment using dplyr::left_join (R lang) vs. SQL LEFT JOIN

I want to left join two dataframes, where there might be NAs in the join column on both side (i.e. both code columns)
a <- data.frame(code=c(1,2,NA))
b <- data.frame(code=c(1,2,NA, NA), name=LETTERS[1:4])
Using dplyr, we get:
left_join(a, b, by="code")
code name
1 1 A
2 2 B
3 NA C
4 NA D
Using SQL, we get:
CREATE TABLE a (code INT);
INSERT INTO a VALUES (1),(2),(NULL);
CREATE TABLE b (code INT, name VARCHAR);
INSERT INTO b VALUES (1, 'A'),(2, 'B'),(NULL, 'C'), (NULL, 'D');
SELECT * FROM a LEFT JOIN b USING (code);
It seems that dplyr joins do not treat NAs like SQL NULL values.
Is there a way to get dplyr to behave in the same way as SQL?
What is rationale behind this type of NA treatment?
PS. Of course, I could remove NAs first to get there left_join(a, na.omit(b), by="code"), but that is not my question.
In SQL, "null" matches nothing, because SQL has no information on what it should join to -- hence the resulting "null"s in your joined data set, just as it would appear if performing left outer joins without a match in the right data set.
In R however, the default behaviour for "NA" when it comes to joins is almost to treat it like a data point (e.g. a null operator), so "NA" would match "NA". For example,
> match(NA, NA)
[1] 1
One way you can circumvent this would be to use the base merge method,
> merge(a, b, by="code", all.x=TRUE, incomparables=NA)
code name
1 1 A
2 2 B
3 NA <NA>
The "incomparables" parameter here allows you to define values that cannot be matched, and essentially forces R to treat "NA" the way SQL treats "null". It doesn't look like the incomparables feature is implemented in left_join, but it may simply be named differently.
By default column code have primary key,therefore not accept NULL value

Problem with SQL Join

I have two tables, tblEntities and tblScheduling.
tblEntities:
EntityID ShortName Active
1 Dirtville 1
2 Goldtown 1
3 Blackston 0
4 Cornfelt 1
5 Vick 1
tblScheduling:
ScheduleID EntityID SchedulingYearID
1 1 20
2 1 21
3 2 20
4 3 19
5 5 20
I need a query that will show ALL ACTIVE Entities and their schedule information for a particular ScheduleYearID.
Output should look like (the desired SchedulingYearID in this case is 20):
EntityID ScheduleID
1 1
2 3
4 NULL
5 5
The query that I have written so far is:
SELECT tblEntities.EntityID, tblEntities.ShortName, tblScheduling.ScheduleID
FROM tblScheduling RIGHT OUTER JOIN
tblEntities ON tblScheduling.EntityID = tblEntities.EntityID
WHERE (tblScheduling.SchedulingYearID = #SchedulingYearID)
AND (tblEntities.Active = 1)
ORDER BY tblEntities.EntityID
My problem is that using this query it will not include active entities without schedule information (such as EntityID 4 in the example above). I can write the query to display all active entities and their schedule status fine, but once I start limiting it via the SchedulingYearID I lose those particular entities.
Are there any solutions that I am obviously missing without having to resort to subqueries, cursors, etc.? If not it's not a big deal, I just feel like I am missing something simple here.
Try this... Join conditions are evaluated to produce the intermediate Join result set, and then, (for an outer join), all the rows from the "Outer" side are added back in before moving on... Where conditions are evaluated after all joins are done...
SELECT E.EntityID, E.ShortName, S.ScheduleID
FROM tblEntities E
Left Join tblScheduling S
ON S.EntityID = E.EntityID
And S.SchedulingYearID = #SchedulingYearID
WHERE E.Active = 1
ORDER BY E.EntityID
I change your join order cause I prefer left joins... but it doesn't matter
It's your conditions in the where clause:
(tblScheduling.SchedulingYearID = #SchedulingYearID)
when there is no tblScheduling info this wil always fail. Add
(((tblScheduling.SchedulingYearID = #SchedulingYearID) OR (tblScheduling.SchedulingYearID is null) )
or wathever null condition checking your DB uses.
I think the trouble is that the WHERE clause is filtering out the rows where SchedulingYearID is null. So don't.
SELECT tblEntities.EntityID, tblEntities.ShortName, tblScheduling.ScheduleID
FROM tblScheduling RIGHT OUTER JOIN
tblEntities ON tblScheduling.EntityID = tblEntities.EntityID
WHERE (tblScheduling.SchedulingYearID = #SchedulingYearID OR
tblScheduling.SchedulingYearID IS NULL)
AND (tblEntities.Active = 1)
ORDER BY tblEntities.EntityID;