Update table using join and conditions(where) from another table [duplicate] - sqlite

This question already has answers here:
How do I make an UPDATE while joining tables on SQLite?
(2 answers)
Closed 5 years ago.
I have two tables: Teams and Players.
Teams: TeamID, NumberOfPlayers, ..
Players: PlayerID, Name, Surname, TeamID, ..
I want to update NumberOfPlayers according to a player's Name, Surname and TeamID.
This is what I tried:
UPDATE Teams
SET NumberOfPlayers = 28
FROM Teams
JOIN Players ON Teams.TeamID = Players.TeamID
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
But I get an error:
near "FROM": syntax error: UPDATE Teams
SET NumberOfPlayers = 28
FROM
I also tried:
UPDATE Teams
JOIN Players ON Teams.TeamID = Players.TeamID
SET NumberOfPlayers = 28
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
And the error I get is:
near "JOIN": syntax error: UPDATE Teams
JOIN
Another shot was this:
UPDATE Teams AS T
JOIN Players P ON T.TeamID = P.TeamID
SET T.NumberOfPlayers = 28
WHERE P.Name LIKE 'Amata' AND P.Surname LIKE 'Walton' AND P.TeamID = 3
Which led me to this error:
near "AS": syntax error: UPDATE Teams AS
It seems like the problem is after UPDATE Teams, but that's a valid table.
Is this possible? What am I missing?

Your code is a bit odd, because you already know the count and the team ID you want to update, so I'm not sure why the player matters.
Further, I'd be concerned that it sorta looks like you might be using a firstname/lastname as a key, and I'd strongly not recommend that. Also, if you want an exact match on a string, = is referenced. See details on that here: Equals(=) vs. LIKE
But I believe this is the query you are technically going for.
Some relational databases support a FROM/JOIN on a update (like SQL Server), so don't (Like Oracle). SQLite doesn't support that syntax.
UPDATE Teams
SET NumberOfPlayers = 28
Where TeamId In
(SELECT TeamId From Players
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
)

Related

Get top records by latest date in Azure CosmosDB

I have a table like this:
Assume there are lots of Names (i.e., E,F,G,H,I etc.,) and their respective Date and Produced Items in this table. It's a massive table, so I'd want to write an optimised query.
In this, I want to query the latest A,B,C,D records.
I was using the following query:
SELECT * FROM c WHERE c.Name IN ('A','B','C','D') ORDER BY c.Date DESC OFFSET 0 LIMIT 4
But the problem with this query is, since I'm ordering by Date, the latest 4 records I'm getting are:
I want to get this result:
Please help me in modifying the query. Thanks.

PeopleSoft Query - finding people without leave type

I'm working with PeopleSoft's query manager and I'm having trouble creating a report that will find all active employees who do not have a certain leave type.
I have the two tables (Employees - Non terminated Employees and Leave_Accrual-EE). They are left outer joined. The field in question is PLAN_TYPE. Now, I've tried creating a filter to pull in all employees who do not have plan type 54. The criteria is B.PLAN_TYPE not equal to 54, but that still brings up everyone, it just doesn't bring up the row for 54.
I feel like I'm missing something obvious - maybe I have to create a subquery? If so, I have never done this in PeopleSoft.
Anyone have any advice?
Original SQL screenshot.
UPDATED
This is less of a PeopleSoft question, and more of a SQL question.
The problem you have been running into is that you are doing a per-row filter and excluding only rows that have the undesirable code.
What you need to do instead is exclude all rows for a user that has the undesirable code in any row.
This can be done with a NOT IN or NOT EXISTS query.
e.g.
SELECT EMPLID
FROM TABLE1
WHERE
EMPLID NOT IN
(
SELECT EMPLID
FROM TABLE1
WHERE CODE = 123
)
/
alternately
SELECT A.EMPLID
FROM TABLE1 A
WHERE
NOT EXISTS
(
SELECT B.EMPLID
FROM TABLE1 B
WHERE
B.CODE = 123
AND B.EMPLID = A.EMPLID
)
/
See this SQL Fiddle example to test out the SQL:
http://sqlfiddle.com/#!4/2b0f6/7
To do this in PS Query, you could do this by adding a criteria with a subquery on the right side of the equivalence.
Here is some documentation:
Home > PeopleSoft PeopleTools 8.53 > PeopleSoft Query > Working with Subqueries

How to stop a row from show on Null count in query Access 2010

Here is the problem. I have a table and the table is 100% completed. There are no null values in the table. The table is broken down to:
Division > Region > RAPM > Status > Disposition
I want to count how many times "Training" is in [Disposition] for a [Region] using a query.
The error I get is when a [Region] has 0 "Training" in [Disposition] the count is a coming back as Null so the entire row is not shown.
How do i get the count to come back as "0" so I can keep the [Division], [Region], & [RAPM] in the results for reporting even if there is 0 count for training.
I have tried NZ() but this will not work because there is technically no Null cell to be converted.
Here is the statement:
SELECT tblAlignment.Division, tblAlignment.Region,tblAlignment.RAPM, Count(tblCase.Dispostion) AS CountofTraining
FROM tblCase INNER JOIN tblAlignment ON (tblCase.Region = tblAlignment.Region) AND (tblCase.Store = tblAlignment.[Store Number])
Where (((tblCase.Status)="Closed") AND ((tblCase.Disposition)="Training")
Group BY tblAlignment.Division, tblAlignment.Region, tblAlignment.RAPM
HAVING (((tblAlignment.Division)=[Forms]![frmDashboardNative]![NavigationSubform].[Form]![NavigationSubform].[Form]![Combo16]))
This is not the complete answer, but my reputation is not high enough to comment so this is the only way I can respond. To get a fuller answer you would need to provide more detailed table structure including primary keys and relationships between the tables. Making guesses from what you have provided I can make a couple of suggestions, but your post raises a few questions:
You say you want to count related entries based on same Region, but your join links on Region and Site. Is there a relationship between Site and Region? Can a single site only ever appear in one Region? If so then I think this information should possibly be in a separate table.
I think the HAVING condition should actually be in the WHERE clause.
I think you may have duplicated the ![NavigationSubform].[Form]
Anyway, a slightly more generic example of one way of achieving what you're after would be:
SELECT a.Region, Nz(b.RecordCount, 0) AS FinalCount
FROM TableA a
LEFT JOIN (SELECT Region, Count(*) AS RecordCount
FROM TableB
WHERE Status = "Closed" AND Disposition = "Training"
GROUP BY Region) AS b ON a.Region = b.Region
WHERE a.Division = [Combo16]
Hope this is of some help.

MS ACCESS application get crashing after applying filter to certain queries

Currently I am developing an Access 2013 based Application for tracking evaluation information of the school students. The database contains only 3 simple tables:
tblSubjects = Contains different subject information
tblStudents = Contains student's personal information
tblMarks = Contains subject wise evaluation marks for each student
and few other queries based on these 3 tables. Now I have a (bit ugly) sql query like following:
SELECT tblStudents.*,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID) AS PAPER_COUNT,
(SELECT SUM(MR_TOTAL) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID) AS ALL_TOTAL,
(SELECT MIN(MR_TOTAL) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'E?') AS MIN_ELEC,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'A?') AS LANG_PS,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'E?') AS ELCT_PS,
IIf([PAPER_COUNT]>5,ALL_TOTAL-MIN_ELEC,ALL_TOTAL) AS [GT],
IIf([LANG_PS]=2 And [ELCT_PS]>=3,'PASS','FAIL') AS STATUS
FROM tblStudents;
The Problem is, whenever I try to run a filter on the STATUS field of this query (Like When STATUS='PASS') the entire ACCESS is first STOPES RESPONDING! and then SHUTS DOWN and RESTARTS.
I have no idea what is going on here. I have seen far more complex queries running perfectly well, but not this one. Any help will be appreciated.
I've experienced this a lot in Access. I don't know the cause of the problem but I just export the query to Excel or create a table based on the query to apply filtering.

Access 2010 performance query

Hi I am trying to create a query that can give me the best performing program among several companies.
I have a table that has several companies and the programs there are a couple companies that have a program that others do not so I know those will be the best performing. I want a query to say for example.
CompanyTwo,Program One,12.85%
CompanyOne,Program Two,12.56%
CompanyTwo,Program Three,1%
..... and so on...
Table looks like this.
CompanyName, ProgramName,PerformancePercentage,ProgramYear
CompanyOne,Program One,10.04%,2015
CompanyOne,Program Two,12.56%,2015
CompanyTwo,Program One,12.85%,2015
CompanyTwo,Program Two,9.5%,2015
CompanyTwo,Program Three,1%,2015
Your query is a bit complicated and requires either 2 queries or a correlated subquery. Below I provided the latter.
SELECT BestPerfByProg.program, Table1.company, BestPerfByProg.BestPerf
FROM Table1
INNER JOIN (SELECT Table1.program, Max(Table1.Performance) AS BestPerf
FROM Table1
WHERE table1.period = 2015
GROUP BY Table1.program
) AS BestPerfByProg ON (Table1.program = BestPerfByProg.program)
AND (Table1.Performance = BestPerfByProg.BestPerf)
ORDER BY BestPerfByProg.program;
If you want to make it in 2 pieces, start with the part that is between () and save it as BestPerfByProg

Resources