I'm using MS Access 2013. My database has four relevant tables:
Table A) Numerical_Key_A, {Other Data Items}
Table B) Numerical_Key_B, "Adjustment Value", {Other Data Items}
Table C) Numerical_Key_C, {Other Data Items}
Table D) {Other Data Items}, Link-to-B, Link-to-A, Link-to-C
The way it works, however, is that the Link-to-C going into a record of Table D is always exactly "Adjustment Value" away from Numerical_Key_A. As such, I would like to make the Link-to-C be automatically calculated when I enter Link-to-A and Link-to-B.
As far as I can tell, this would require Table D having a Calculated Field that gets its data from the linked Table B, which Access does not allow. Is there another way to do this? I'd prefer not to use VBA if possible, but if it's the only way, I'll just have to learn how to do it. (I know VBA for Excel, but have never used it in Access before).
If i understand yo well, all you need to do is to create join between tables, for example:
SELECT D.Field1, D.Field2, B.[Adjustment Value]
FROM Table_D AS D INNER JOIN Table_B AS B ON D.Link-To-B = B.Numerical_Key_B
For further information, please, see:
Join tables and queries
How to: Perform Joins Using Access SQL
[EDIT]
In table D, there is a field, which I have labeled in the above snippit as "Link-to-C". This field should be populated with the value of Link-to-A plus TableB.AdjustmentValue
SELECT D.Field1, D.Link-To-C + D.Link-To-A + B.[Adjustment Value] AS CalculatedField
FROM (Table_D AS D INNER JOIN Table_B AS B ON D.Link-To-B = B.Numerical_Key_B)
INNER JOIN Table_C AS C ON C.Numerical_Key_C = D.Link-To-C)
INNER JOIN Table_A AS A ON A.Numerical_Key_A = D.Link-To-A
Note: i have no chance to test it. It's just an idea.
Finally i need to warn you: Access likes () /brackets/ while multiple joins are used.
[EDIT2]
Based on discussion, below query should return calculated field:
SELECT C.MetaLevel, R.LevelAdjustment, C.MetaLevel+ R.LevelAdjustment AS NominalLevel
FROM tblRank AS R INNER JOIN tblCreatures AS C ON R.ID = C.Rank;
If you would like to update NominalLevel in a tblCreatures, use query like this:
UPDATE tblCreatures AS C
INNER JOIN tblRank AS R ON R.ID = C.Rank
SET C.NominalLevel = (C.MetaLevel+ R.LevelAdjustment);
NominalLevel has been updated. You can select it also...
SELECT C.MetaLevel, R.LevelAdjustment, C.NominalLevel
FROM tblRank AS R INNER JOIN tblCreatures AS C ON R.ID = C.Rank;
Cheers,
Maciej
Related
I know we can specify our data in the select, but These tables have a lot of columns, so I'm trying to see if there's a way to save some time!
So instead of the following, is there another way to merge the similar columns and return all data without the duplicate columns?
select
coalesce(a.item, b.item, c.item) as item,
coalesce(a.thing, b.thing, c.thing) as thing,
a.something,
b.somethingelse,
c.thirdthing
from a
inner join b on a.ID = b.ID
inner join c on a.ID = c.ID
Questions similar to this one about using DISTINCT values in an INNER JOIN have been asked a few times, but I don't see my (simple) use case.
Problem Description:
I have two tables Table A and Table B. They can be joined via a variable ID. Each ID may appear on multiple rows in both Table A and Table B.
I would like to INNER JOIN Table A and Table B on the distinct values of ID which appear in Table B and select all rows of Table A with a Table A.ID which appears matching some condition in Table B.
What I want:
I want to make sure I get only one copy of each row of Table A with a Table A.ID matching a Table B.ID which satisfies [some condition].
What I would like to do:
SELECT * FROM TABLE A
INNER JOIN (
SELECT DISTINCT ID FROM TABLE B WHERE [some condition]
) ON TABLE A.ID=TABLE B.ID
Additionally:
As a further (really dumb) constraint, I can't say anything about the SQL standard in use, since I'm executing the SQL query through Stata's odbc load command on a database I have no information about beyond the variable names and the fact that "it does accept SQL queries," ( <- this is the extent of the information I have).
If you want all rows in a that match an id in b, then use exists:
select a.*
from a
where exists (select 1 from b where b.id = a.id);
Trying to use join just complicates matters, because it both filters and generates duplicates.
I'll try my best to pose this as a question, and not a "write my script" request- but finding it hard to phrase without using the working example.
To start, here's a relational diagram of what I'm working with;
**TABLE 1 - TABLE 2 are scalable, i.e there is a TABLE 4 - TABLE 5, TABLE -... etc, it's Tables, A/B/C that are constant.
Essentially, what I'm trying to do with this structure is match a certain value that only exists in TABLE 1 or 2 to TABLE C, where I'll pass that value to TABLE 1 or 2 and return the TABLE C value.
SELECT
TABLEC.CUSTOMID
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.TABLE2ENTITYKEY = TABLE2.ENTITYKEY
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE2.TABLEAENTITYKEY
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY
WHERE TABLE1.USERENTITYKEY = /*ENTER_VALUE*/
OR
SELECT
TABLEC.CUSTOMID
FROM TABLE3
INNER JOIN TABLE4 ON TABLE3.TABLE4ENTITYKEY = TABLE4.ENTITYKEY
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE4.TABLEAENTITYKEY
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY
WHERE TABLE3.USERENTITYKEY = /*ENTER_VALUE*/
Both of these work, and will work for my Tables 5/6, 7/8 etc - but that leaves me with multiple queries.
What I'd like to do is 'combine' these queries into one, where I'll pass my value to Tables 1/3/5 etc and return any values from C that link back to any of the source tables - it's not important that I know which source table the value in C links too, it's only important that it links to one of them.
Does anyone have any suggestions or examples I can use to see how this kind of script is formatted? I have played around with some things - but my SQL isn't that strong, so it's futile so far.
Thanks in advance, please comment if more information is required - and sorry in advance if the question is poorly asked!
-L
Not a full solution - but currently tying the two above queries together using UNION.
Morning all, I need some help please.
I have a database with several tables. I'm trying to write an INSERT INTO and SELECT query that copies all values from one Table (TableA) into another Table (TableD) but substitutes one value with a value it looks up in another table (Table B).
Table A with various fields including TableBRef
Table B includes various fields starting with TableBRef and also includes a field TableCRef
I want to copy all of TableA into Table D but replace The TableBRef with the TableCRef ie I know TableBRef, I need to search for it in TableB and return the associated value from the TableCRef field.
INSERT INTO TableD
(DRef, CRef, DData1, DData2)
SELECT TableA.ARef, TableB.CRef, TableA.AData1, TableA.AData2
FROM TableD AS TableD_1 CROSS JOIN
TableC CROSS JOIN
TableA INNER JOIN
TableB ON TableA.BRef = TableB.BRef
Sorry, I thought that calling them generic table names may help but it's actually a little confusing :-)
I don't understand how you relate each table but see if this query will work for you
INSERT INTO TableD
(DRef, CRef, DData1, DData2)
SELECT a.ARef, c.CRef, a.AData1, a.AData2
from tableA a
left outer join tableB b on a.ARef = b.ARef
inner join tableC c on b.CRef = c.CRef
Platform used:
SQL Server 2008 and C++ Builder
I am doing an inner join between 2 tables which was giving me an error:
Row cannot be located for updating
Query:
SELECT DISTINCT
b.Acc, b.Region, b.Off, b.Sale, a.OrgDate
FROM
sales b
INNER JOIN
dates a ON (a.Acc = b.Acc and a.Region = b.Region and a.year= b.year)
WHERE
(a.xdate <> a.yDate)
and (b.Sale = a.SaleDate)
and b.year = 2010
Note: Acc, Region, Off are primary keys of table b and are also present in table a.
Table a has an id which is the primary key which does not appear in the query.
It turned out that my inner join was returning duplicate rows.
I changed my inner join query to use 'DISTINCT' so that only distinct rows are returned and not duplicate. The query runs but then I get the error:
Insufficient key column information for updating or refreshing.
It does turn out that the fields which are primary keys in Table A have the same names as the fields in Table B
I found that this is a bug which occurs while updating ADO record-sets.
BUG: Problem Updating ADO Hierarchical Recordset When Join Tables Share Same Column Name
I have the following 2 questions:
Is it not a good idea to use Distinct on an inner join query?
Has anyone found a resolution for that bug associated with TADO Query's?
Thank you,
The way I would solve this is to construct an update query by hand and run it through TADOQuery.ExecSQL. That assumes you actually know what you are doing.
The question is WHY are you working on a recordset that results in multiples of the same row, on all fields? You should be inspecting your query and fixing it. DISTINCT doesn't help, because SQL Server has picked one record but ADO won't know which one it picked, since there isn't enough information to properly identify the source on each side of the JOIN.
This query pulls in a.id to make the source records identifiable:
SELECT Acc,Region,Off,Sale,OrgDate,id
FROM
(
SELECT b.Acc,b.Region,b.Off,b.Sale,a.OrgDate, a.id,
rn=row_number() over (partition by b.Acc,b.Region,b.Off order by a.id asc)
FROM sales b
JOIN dates a ON(a.Acc = b.Acc and a.Region = b.Region and a.year= b.year)
WHERE a.xdate <> a.yDate
and b.Sale = a.SaleDate
and b.year = 2010
) X
WHERE rn=1;
Not tested, but it should work with ADO