sql update multiple rows with multi join subselect - sql

This is an updated part 2 of a question I asked earlier. I'm trying to make the following update, but this query does not actually seem to do anything.
UPDATE u
SET graduation_class_id = gc.graduation_class_id
FROM [user] u
JOIN graduation_class gc
ON u.graduation_class_id = gc.graduation_class_id
JOIN graduation_term gt
ON gt.graduation_year_id = gc.graduation_year_id
TABLE SCHEMA
**user
user_id
graduation_class_id
**graduation_class
graduation_class_id
graduation_year_id
**graduation_term
graduation_term_id
graduation_year_id
The goal is to get the matching graduation_class_id value into the user table. I've been told this won't work because the match won't be found unless the user already has the matching graduation_class_id. That is a problem, because I'm trying to actually get the proper one in there!

This idea is fundamentally doomed to fail. You're trying to get the SQL server to link a graduation class to a user by itself, despite the SQL server not having information on which graduation class should be linked to the user. Unless you have some data somewhere (in user, in graduation_class, in some other table) that links a user_id to the graduation term, class, or year (or someone manually telling SQL which data match up), SQL can't magically do that job for you.

Related

Missing query data when using Sum

My database in Microsoft Access looks like this:
Every Client can have many assistants.
Every Assistant may have one client or no clients at all.
Assistant have a Nice field which is Boolean, indicating whether the Assistant is nice.
I need a query where I can get all of the clients, together with a boolean value indicating whether they have at least one nice assistant.
Currently this is the query I have:
This query is working as you can see: (apperantly 0 is false and -1 is true)
But here is the problem:
If there is a Client with no Assistants at all, it will not show up in the query.
I am wondering if there is a way to add all of the Clients with no Assistant to the query and their MinOfAS-Nice column will be 0. I will also accept any other creative way for example creating another query - but in the end I’m going to need a one query with all of the Clients data.
I need this data for a Report I'm going to create in Access.
Thanks in advance!
GitHub repo: https://github.com/orihpt/MissingRecordsInQueryAccessIssue
For your convinience here is the query as SQL query:
SELECT Client.[CL-ID], Client.[CL-Name], Min(Assistant.[AS-Nice]) AS [MinOfAS-Nice]
FROM Client INNER JOIN Assistant ON Client.[CL-ID] = Assistant.[AS-Client]
GROUP BY Client.[CL-ID], Client.[CL-Name]
ORDER BY Client.[CL-ID];
Use a Left Join:
SELECT Client.[CL-ID], Client.[CL-Name], Min(Nz(Assistant.[AS-Nice], 0)) AS [MinOfAS-Nice]
FROM Client LEFT JOIN Assistant ON Client.[CL-ID] = Assistant.[AS-Client]
GROUP BY Client.[CL-ID], Client.[CL-Name]
ORDER BY Client.[CL-ID];
Also: the Nz function is a default operator, means that on records without any linked records on the another table you will get 0 instead of Null.

Can't get MS Access SQL Statement W/ Nested Joins to Function Properly

Let me start by saying, I have scoured the web for help with this, tried figuring it out with a professor and used a Database Systems text book and have still not gotten this to work properly. I am attempting to finish this database as my final project for a DB management course that I'm taking, but it is also for real world use at work. My team at work currently creates access to a clinical system for end users. When we receive a request form it includes a "ServiceNow Position" (Column A) and a "ServiceNow Role" (Column B). My team has to then reference a spreadsheet, which I've attempted to attach, to find out what this correlates to in the clinical system (Column C). The row of the spreadsheet also dictates what, if any, user orgs (Column F) and user groups (column G) that the user account receives. I'm including a screenshot of the relationship between the tables prior to my modifications to the SQL statement, as once the outer joins are in place, the relationships don't illustrate.
The idea here is that I will always be given the ServiceNow role and position, it will always correlate to a row on the "Cerner_HNA_Positions" table. I will NOT necessarily always have User_Group or Organizations, though when I do have them, I may have up to 2 and 4 respectively.
My goal is to get a query that works properly, and then create a form on top of this so that who ever is working account creations can insert the ServiceNow Position and Role and have returned what the "Cerner_HNA_Positions.HNA_Position" is and any correlated User Groups and Organizations. The following is the closest SQL statement that I've gotten to function without a syntax error. However, this is returning every HNA_User_Group and HNA_Org, even though the row for HNA_Position that it returns does not have any HNA_User_Group's or Orgs assigned. I'm sure this is something to do with the fact that I've done a left join, but my goal in this was to ensure that I see the HNA_Position. Can anyone help me with this?
SELECT DISTINCT Cerner_HNA_Positions.HNA_Position,
Cerner_HNA_Positions.Like_User,
Cerner_HNA_Positions.Physician,
Cerner_HNA_Positions.Add_Resources,
Cerner_HNA_User_Groups.*,
Cerner_HNA_Organizations.*,
[Service Now Position Name].Service_Now_Position,
[ServiceNow Role].Service_Now_Role
FROM Cerner_HNA_User_Groups,
Cerner_HNA_Organizations,
[ServiceNow Role]
INNER JOIN ([Service Now Position Name]
INNER JOIN Cerner_HNA_Positions
ON [Service Now Position Name].Service_Now_Position_ID =
Cerner_HNA_Positions.Service_Now_Position_ID)
ON [ServiceNow Role].Service_Now_Role_ID =
Cerner_HNA_Positions.Service_Now_Role_ID
WHERE ((([Service Now Position Name].Service_Now_Position)="CIS - PowerChart")
AND (([ServiceNow Role].Service_Now_Role)="Resident"));
Spreadsheet referenced above: https://www.dropbox.com/s/1vreampypo17lcd/Positions_Roles%20Sheet.xlsx?dl=0

Error in SQL SELECT with INNER JOIN

So, basically, I have two tables called "dadoscatalogo" and "palavras_chave", with a common field, "patrimonio" which is the primary key of "dadoscatalogo".
I'm using a servlet to connect to the database with these tables, and passing a query to search for entries based on some search criteria that's defined by the user.
Now, since the user can search for entries based on information present in both tables, I need to do an INNER JOIN, and then use WHERE to search for that info. I'm also using LIKE, because the user may pass just part of the information, and not all of it.
So, to test it all out, I tried passing it a few parameters to work with, and see how it went. After some debugging, I found out that there was some mistake in the query. But I can't seem to be able to point out exactly what it is.
Here's the test query:
SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo
INNER JOIN palavras_chave
ON dadoscatalogo.patrimonio=palavras_chave.patrimonio
WHERE dadoscatalogo.patrimonio LIKE '%'
AND dadoscatalogo.titulo LIKE '%tons%'
OR palavras_chave.palchave LIKE '%programming%';
So, basically, what I'm trying to do with this query is, get all the primary keys from "dadoscatalogo" that are linked to a record with a "titulo" containing "tons", or a "palchave" containing "programming".
PS. Sorry for the names not being in English, hopefully it won't be too much of a distraction.
EDIT: Right now, the tables don't have much:
This is the dadoscatalogo table:
http://gyazo.com/fdc848da7496cea4ea2bcb6fbe81cb25
And this is the palavras_chave table:
http://gyazo.com/6bb82f844caebe819f380e515b1f504e
When they join, I'm expecting it to have 4 records, and it would get the one with patrimonio=2 in dadoscatalogo (which has "tons" in titulo), and the one with palchave=programming (which would have patrimonio=1)
As per my understanding run below query:
SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo
INNER JOIN palavras_chave
ON dadoscatalogo.patrimonio=palavras_chave.patrimonio
WHERE dadoscatalogo.titulo LIKE '%tons%'
OR palavras_chave.palchave LIKE '%programming%';

Search through Users with dynamic attributes with SQL

.Hi i'm working with Asp and SQL-Server and i have no problem with writing dynamic query
I'm trying to Write a search page for searching people.
I have 3 related tables:
See my table diagram in : http://tinypic.com/r/21159go/5
What i'm trying to do is to design a search page that a person can search users with a dynamic number of attributes.
Example:
think that a username called "User1" has 3 attributes named "Attr1", "Attr2" and "Attr3" related to him in "UserAttributes" table and "User2" has 3 attributes named "Attr1", "Attr2" and "Attr4".
Attribute names and other bunch of items unrelated to search function saved in "Attributes" Table. This is because i want to relate an attribute between multiple users. and their values are stored in "UserAttributes" table.
Well someone wants to search upon "Attr1" and "Attr2" and wants to return all users that have "Attr1" and "Attr2" with specific value.
I need a query to know how to implement this. I can write a dynamic query with asp.net so if someone please give me a query for this one example i have brought, i would be thankful
P.S. This is not my real database. my real database is much more complex and has more fields and tables but i just cut it and brought only necessary items. and because attributes are very dynamic they can't be embedded in table columns.
Thanks in advance
Based on your DB diagram your code would be something like this
update:
SELECT u.*
FROM users AS u
LEFT OUTER JOIN UserAttributes AS ua1
ON u.USER_ID = ua1.USER_ID
LEFT OUTER JOIN UserAttributes AS ua2
ON u.USER_ID = ua2.USER_ID
WHERE (
ua1.attribute_id = 'att1'
AND ua.attribute_value = 'MyValue' )
AND (
ua2.attribute_id = 'att2'
AND ua.attribute_value = 'MyValue2' )
In where clause you would specify the attirbute_Id and what value you are expecting out of it. Than just decide if you want to restrict users to have all values match or just one of them, in that case modify AND between statements to be OR
if you just want to do this quick and dirty you can create your own class library that can create adhoc sql that will pass to the database.
if you want more organized matter, create SP that will bring back users and accepts any left of id and value. Do a lot of that by passing list separated by comma, colon or semicolon. Than split it up in SP and filter results based on those values
there are many other alternatives like EntityFramework, LINQ-to-SQL and other options, just need to figure out what works best for you, how much time you want to spend on it and how easy will it be to support later.

Please help me make this Access 2007 Friendly

I asked a friend to hellp me with an issue I was having with my Access Database since I haven't programmed in years, and here's what he replied with:
Let me toss an example out just to make sure I'm looking at this
right. You start with a record with ID 1. This gets renewed, and the
system generates a new record with ID 2, and brings along the old ID
of 1 in the RenewalOf field, and so on for future renewals. If that
is correct, and each record is only allowed to be referenced once (so
there will only ever be one record with ID of 1 in the RenewalOf
field), then the following should work:
This bit of code didn't work:
UPDATE
tblSold
SET
RenewedToID = RenewalRecord.SoldID
FROM
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
Not sure what is allowed in your SQL queries, but this is basic and
should be fine. You can also add in some criteria to only update
records where the RenewedToID field is blank, or only for one record
if you are processing this just after you add a new record. You can
check to make sure this is going to work by running the following:
But this did:
SELECT
tblSold.SoldID
,RenewalRecord.SoldID
FROM
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
This will list the original ID along with the renewal ID, i.e. the one
that will be put in the original record. Let me know if this works or
if you have any issues with it.
Can you help me make his first code snippet work in Access 2007?
You may need to rearrange the update slightly for Access:
UPDATE
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
SET
tblSold.RenewedToID = RenewalRecord.SoldID
Some other SO answers showing this type of syntax:
SQL Update Statement in MS Access
How to create a correlated update subquery in MS-Access?