Building queries in Access 2007? - sql

I'm trying to build a query where I'm able to get names of clients. So I have two tables, the 1st table has a column AppointmentNO, and this field is a number (there are other columns but they're irrelevant). In the 2nd table I have an ID as primary key, FirstName, LastName. ID is what matches the AppointmentNO in the first table.
Basically what I'm trying to do is link the two tables so that when I have an AppointmentNO in one column, I can see the LASTNAME associated with it in the 2nd column (need to include this in my report). I'm trying to link the AppointmentNO to ID and on JOIN PROPERTIES -> include all records from left table (1st table) and only those from right table (2nd table) where the joined fields are equal.
If I try to run the query it gives me a MISMATCH error. What am I doing wrong?

The type mismatch error could be happening:
because the two fields that you're trying to join aren't set as the same data type (e.g. one is Number and the other is Text) - check this in the Properties tabs for the relevant fields in each table;
it could be that Access has a join between the tables involving other fields (it will sometimes do this with AutoIDs) - you can check the relationships (and establish them) in the Tools -> Relationships window (where this is located might depend on your version). You can also use this tool to explicitly build the relationship, by connecting your 'ID' to 'AppointNO' - though you should still ensure that the fields are of the same data type.
ADDITION:
Based on what you're describing, I think this is the situation (correct me if I'm wrong, though):
Three tables - Client, AppointmentNO, Children
In each table, there is a 'MemberID' - this is primary key in Client Table, and is Foreign Key in the other tables.
The Children and AppointmentNO tables are linked to Client table by one-to-many relationships (a client can have >1 children and >1 appointment).
I'd set this up so that the Member ID is the same datatype in each table, and join all tables on that field. Then, when set up a query that gives you MemberID, ClientName, ClientDOB (and anything else you want from the client table), ChildName, and AppointmentID. Once the query is working and giving you the desired output, you can build a report and group the output by Client and Client Description, so you'll get "Client A" followed by list of appointments and children, then "Client B" etc.
Hope that's clear-ish.

Related

MS Access duplication

I got my query in MS Access duplicating its errors. The data is being entered from a Windows Forms app, and the data is in two separate tables. I'm using a basic query to show each row in the table. Name table and score table.
Now the issue is that the data is duplicating itself.
Image of the access table
This is the SQL query:
SELECT DISTINCT score.score, name.Name
FROM name, score;
Table Schema
What SQL code could I use to stop the duplication error?
Thanks in advance
Need to add a long integer number field StudID in Scores table then do data entry to add the appropriate student identifier value into each Scores record. This will establish a primary/foreign key relationship between tables that can be used in query with a LEFT or RIGHT JOIN on key fields.
This assumes a student can have multiple scores (for what?). If there can be multiple scores, then probably also need a date field ScoreDate and then possibly should be a Subject field.
If student can have only 1 score, then don't need Scores table - have a field Score in Students table.
Strongly advise not to use reserved words as names for anything. Name is a reserved word.

How to make 2 relations to the same table(using diferent columns) on dataset.relations?

I have a table clients and a table country, clients has 2 columns referencing country id (id of the country the client is from, and id from the counry the client wants its packages sent to), while dataset.relations is useful to fuse tables using 1 column(or more as long as they are diferent) i cant figure how to display a table that contains the info of the client, and the NAME of both countires corresponding to the ids.
For just the country of the client i go ass follows
billOrderDataset.Relations.Add("clientCountryRelation",countriesTableCopy.Columns("id"), clientTableCopy.Columns("countryId"))
clientTableCopy.Columns.Add("countryName",GetType(String), "Parent.countryName")
but after that i dont know how would i also add the name of the country to the column "deliveryCountryId" since there is already a relation using both tables and the id column from country, so bascially i would need something like
billOrderDataset.Relations.Add("clientCountryRelation2",countriesTableCopy.Columns("id"), clientTableCopy.Columns("DeliveryCountryId"))
clientTableCopy.Columns.Add("DeliveryCountryName",GetType(String), "Parent.countryName")
The answer is hidden in a paragraph inside the Remarks section in the documentation of the DataColumn.Expression property.
Parent/Child Relation Referencing
....
When a child has more than one
parent row, use Parent(RelationName).ColumnName. For example, the
Parent(RelationName).Price references the parent table’s column named
Price via the relation.
So, in other words, when you have more than one relation on the child table you need to explicitly give the name of the relation in the expression syntax
clientTableCopy.Columns.Add("countryName",GetType(String), "Parent(clientCountryRelation).countryName")
clientTableCopy.Columns.Add("DeliveryCountryName",GetType(String), "Parent(clientCountryRelation2).countryName")

Power Pivot relationships

Trying to create relationships (joins) between tables in power pivot.
Got 2 tables I wold like to join together, connected with a common column = CustomerID.
One is a Fact Table the other Dim table (look up).
I have run the "remove duplicates" on both tables without any problem.
But I still get an error saying : "the relationship cannot be created because each column contains duplicate values. Select at least one column that contains only unique values".
The Fact Table contains duplicates (as it should?) and the Dim Table do not, why do I get this error?
Help much appreciated
Created an appended table with both columns "CustomerID". After the columns where appended together I could "remove duplicates" and connect the tables together through the newly created appended table.
Don't know if this causes another problem later however.
You can also check for duplicate id values in a column by using the group by feature.
Remove all columns except ID, add a column that consists only of the number 1.
Group by ID, summing the content of the added column and filter out IDs whose total equals 1. What's left are duplicated IDs.

T-SQL - Using Column Name in where condition without any references when having multiple tables that are engaged using multiple joins

I am a newbie for T-Sql, I came across a SP where multiple tables are engaged using multiple joins but the where clause contain a column field without any table reference and assigned for an incoming variable,like
where 'UserId = #UserId'
instead - no table reference like
'a.UserId = #Userid'`
Can any please do refer to me any material that clears my mind regarding such issue.
If the query works it means that there is only one Column with the name UserId, if there are multiple columns with the same name you have to reference the table too.
If you don't specify the table reference you will get
Ambiguous column name 'UserId'. error
Which means there are more then 2 tables with a column name UserId.
Anyway, always try and use the reference table.

Need query to return Null fields if no associated data in joined table

I'm trying to design a query in Access 2003 that returns information for a directory. One of the values I wish to include is contained in a second table, but that table doesn't have records for all the records I want.
I want a directory line for EACH MomID in table Registration, regardless of whether that MomID is referenced in table Kids. I can't just leave it out, because if there are children listed in Kids, that needs to be in the directory.
Currently, my query returns only those records where MomID is in both Registration and Kids. I'm filtering the data so that I only get records from Registration with the current YearStart field, so I can't make it an OR statement (even if I knew how). How do I get EVERY record in Registration which meets the YearStart criterion, and have the query return an empty field if the relevant MomID isn't present in Kids"
I could just go manually add MomID to Kids and leave the other fields blank, but that solution lacks elegance. I'm also not entirely sure that would work, since I'm running the data in Kids through two other queries to sort and concatenate before pulling it into the directory query. Plus, I'd like to retain the ability to include childless individuals in the directory if I ever want to expand the database to include the volunteers associated with the group.
What you're describing is a classic case of LEFT JOIN. This operator brings ALL records from the left part of the join even if there're no matches in the right part,
In your case it would be something like
SELECT * FROM Registration LEFT JOIN Kids ON Registration.MomID = Kids.MomID
WHERE Registration.YearStart = Year(Date())
Substitute * with only needed fields if you don't need all the fields from both tables.