MS Access Query using criteria - ms-access

I'm new on MS access and I'm strugling in accomplish a query to find out which partners has not paid in a particular selected month/year.
I have 2 tables, one I have all the partners name (Sócios) and the second, the table (Registro de Pagamento) registered all the payments for all partners including the month & year of the payment.
I don't if the way I'm trying is the best.but , firstly, I created a Form to allow user to select the month (Combo box: TBoxMes) and the year (Combo box: TBoxAno) of his interest to find out which partners has not paid on selected period.
My idea then was to created a query (named as Selecionar_Mes_Cobranca) including partner's name and also the month and the year of each payment done from Registro de Pagamento table, I also have set the relation between the 2 tables as display all register from Socios table as the picture below is shown.
however, on the criteria field for the month and the year, I was trying to include the following expression <>[Forms]![Selecionar_Mes_Cobranca]![TBoxMes] into the Month field and the <>[Forms]![Selecionar_Mes_Cobranca]![TBoxAno] into the Year Field. The idea was to return only the partners name which has no register from the selected Month and Year from user, however, it has not worked and I would like some help to understand why and how to accomplish that.
Thanks in advance

using the '<>' will not provide you the results you are looking for. It will result in getting the payments that were NOT done in the specified months.
You have better change the JOIN cause and use the Codigo field, instead of the name. This will yeld better performance. I assume the Codigo (code of the socio) in Registro de pagamento is a foreign key and therefore has an index, also can not be a null value.
It would really be a good idea to revise your db structure in order to have foreign keys assigned to numeric indexed fields.
Suggestions: keep your field names the same across the database. This will make your life easier in the long run. Also avoid using spaces in them. You can use uppercase notation to help reading them (e.g. MesDoBoleto).
Then what you need is a NOT IN operator. That will extract the Socios who are NOT IN Registro de Pagamento in the specified months:
You need something like this:
SELECT [Socio Nome],Codigo FROM Socios NOT IN (SELECT Codigo FROM [Registro de Pagamento] WHERE [Mes do Boleto]=[Forms]![Selecionar_Mes_Cobranca]![TBoxMes] AND [Ano do Boleto]=[Forms]![Selecionar_Mes_Cobranca]![TBoxAno]);
Please note that I have not used the accents in the field names, adjust accordingly.
Mind that IN, EXISTS, NOT IN and NOT EXISTS have their own quirks. Have a good read about them before use. You can find one example here:
Consider Using NOT EXISTS instead of NOT IN

I add here the code in case you cannot or don't want to change your table fields:
SELECT [SocioNome] FROM Socios NOT IN (SELECT [NomeSocio] FROM [RegistroDePagamento] WHERE [MesDoBoleto]=[Forms]![Selecionar_Mes_Cobranca]![TBoxMes] AND [AnoDoBoleto]=[Forms]![Selecionar_Mes_Cobranca]![TBoxAno]);
Please check that the field names in the query are as you set them.
Note that you may expect a poor performance from such a structure. Even more so if [SocioNome] and [NomeSocio] are not indexed.

Related

mysql database column issue

Create a list of all players and the fees they pay. The football league is looking to increase fees by 15%. Show that column in your list. When complete your output should look like the following. Note: your spacing may be a little different. This output is formatted to be smaller.
[1]: https://i.stack.imgur.com/RmN6D.png - Question with the format required
[enter image description here][1]
[2]: https://i.stack.imgur.com/iJNVS.png - Picture of the table that is going to be used. The table name is 'playerrec'
I know that you will have to use an alias for the column Fee it would be renamed to Increased Fee, but where I am getting stuck at is that I don't know what to do for the New Fee since it isn't a column that is existing. I've already thought about creating column, but I looked at my teachers lectures and it seems we won't be getting into that for a bit.
You can operate on a column in your SELECT clause using arithmetic operators.
If you were looking to get the base fees off the playerrec table, you could do so by simply selecting the column
SELECT pr.Fee
FROM playerrec AS pr
If you were looking to get, say, double the normal fee, you could do so like this:
SELECT (2 * pr.Fee) AS DoubledFee
FROM playerrec AS pr
Here you're computing a new column and aliasing it as 'DoubledFee'. This does not change the underlying data nor add a column to the table. If you want a comparison, you could select both together
SELECT Fee, (2 * pr.Fee) AS DoubledFee
...
For more, see https://learn.microsoft.com/en-us/sql/t-sql/language-elements/multiply-transact-sql?view=sql-server-ver15
As a bonus, the microsoft example includes a 15% change

Names of months as names of columns in MySQL

I need to create table where first column is names of clients and next ones are months of previous year starting from current month with amount of orders of user in this month. So my problem is that I don't really know how to name columns as months starting from current. I know that i can get name of month with namemonth() but i can't use it as name of column. Any ideas?
If I understand it right you want to dynamically name your columns. If you really want to do that you can write a script for that in your favorite programming language. But I don't recommend that you do that as it's not a good practive.
This is what I would do:
Create a table with 3 columns, the first one is the name or id of the client (of type int or varchar), the second one the date (of type date) and the third one the value (of type int as an amount of orders can only be a whole number). Then you can write a script to get the amount of orders for a specific (or all) client(s) with a script based on the date.
But if you really want to do what you said, which I really advise against, have a look at the link provieded by Progman in his comment.

Microsoft Access sql, counting how many times a user chosen value appears, displaying related values

I am creating a query in microsoft access that should count the number of times the instructor ID value, which the user will choose, appears in the Instructor column in the class table, and should display this in the query output.
It should also display the instructorID, InsFirstName and InsSurname values from the Instructor table which correspond to the value entered by the user, as the value appears in both tables.
The code i have so far is:
SELECT COUNT (Instructor)
FROM Instructor, class
WHERE Instructor = [Enter Instructors ID]
GROUP BY Instructor.InstructorID, Instructor.InsFirstName, Instructor.InsSurname;
This however will just display how many times each value in the column appears, rather than just how many times the value the user entered appears. It will also not display the instructorID, InsFirstName and InsSurname values.
Ive included an image of the output here.
The 2 tables i am using are:
Class table
And the instructor table
If any more information or clarification is requested to help with this i will provide it as soon as possible. Thank you in advance to anyone who provides any help.
There are a couple problems here:
1) If you want a field to appear in the output, you need to include it in the SELECT portion of the SQL statement. Anything that is not listed there will not appear in your results. You can also use Count(*) if you want to count the records that match within a grouping.
2) You are querying two tables, Instructor and class, but you haven't specified how the two are joined. Either add a condition to the WHERE clause to specify how they are related (ie. "and Instructor.InstructorID = class.Instructor.ID"), or use a JOIN statement (ie. FROM Instructor INNER JOIN class ON Instructor.InstructorID = class.InstructorID)
Try fixing both of those, hope that helps!

Database not having unique ID's

I have a database containing attendance in monthly basis. Now, I want to display that data on a series of text box. But my problem is that it does not contain any unique id's that's making my task difficult. Have a look at the attachment so that you guys can get the picture of my problem.
http://s26.postimg.org/p8v0zhemx/image.png
Thank you so much in advance.
EDIT:
For future researchers using listview, this is the query for my MySQL.
You have to make a composite key if your db does not have a unique id. Google it.
The query i managed to pull out from my head.
"SELECT empno, line1, time1, line2, time2, line3, time3, line4, time4, line5, time5, line6, time6 FROM attendancelist WHERE empno = '" & ListPayroll.SelectedItems(0).Text & "' AND line1 = '" & ListPayroll.SelectedItems(0).SubItems(1).Text & "'"
It looks to me like your sample data table contains tons of attendance data that basically look like this:
employee workdate starttime endtime
00117 2014-02-03 08:15 17:30
00117 2014-02-04 09:00 17:30
00117 2014-02-05 null null
etc.
If the employee was absent on the given day, that's indicated by null values in starttime and endtime. If the employee was not employed at all on the particular date, you'd simply leave the row out of the table entirely. I think that's what the first five days of employee 00001 in your sample data's first row mean -- not present, not absent.
Your raw data is arranged in a pretty doggone inconvenient report layout that puts a week's work of days on each row. You can probably write a simple dotnet program to slurp up your six-day-week input table and insert six rows (or fewer) of this data from each row of that table.
Once you've loaded your data from that input table, you can switch over to maintaining it in your new table. That will be much easier for you to handle in a program. You will also be able to write a query program that will recreate your six-day-row report, if that's what your users prefer.
Arranged the way I have shown it, you'll get a nice little attendance table. If you know ahead of time you'll have at most one record per day per employee, you can use the columns I've shown, and use a composite primary key consisting of (employee, workdate).
If you might have more than one row per employee per date you'll need to add an id column, that can be an autoincrementing surrogate primary key.
If all you need is an arbitrary unique identifier for update and delete (as indicated in the comments), then add one:
ALTER TABLE my_table
ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY;
That is, of course, assuming you have that ability or can convince someone who does. It is a remarkably minor change. If column names are specified in the existing INSERT queries, it won't require a change to them. Someone ought to be willing to do it.
If you have the freedom to modify the schema, please consider revising it. This is very, very poorly designed (having repetitive columns and columns containing multiple pieces of information). If you cannot modify this one, creating a new, better designed schema and importing data from this schema may be another option you want to consider. (Creating a "new schema" could also be accomplished using a set of separate tables.)
Also be aware that with the current structure, you will need extremely heavy validation code side to prevent users from saving invalid data when they modify it.

MySQL Database Design Questions

I am currently working on a web service that stores and displays money currency data.
I have two MySQL tables, CurrencyTable and CurrencyValueTable.
The CurrencyTable holds the names of the currencies as well as their description and so forth, like so:
CREATE TABLE CurrencyTable ( name VARCHAR(20), description TEXT, .... );
The CurrencyValueTable holds the values of the currencies during the day - a new value is inserted every 2 minutes when the market is open. The table looks like this:
CREATE TABLE CurrencyValueTable ( currency_name VARCHAR(20), value FLOAT, 'datetime' DATETIME, ....);
I have two questions regarding this design:
1) I have more than 200 currencies. Is it better to have a separate CurrencyValueTable for each currency or hold them all in one table?
2) I need to be able to show the current (latest) value of the currency. Is it better to just insert such a field to the CurrencyTable and update it every two minutes or is it better to use a statement like:
SELECT value FROM CurrencyValueTable ORDER BY 'datetime' DESC LIMIT 1
The second option seems slower.. I am leaning towards the first one (which is also easier to implement).
Any input would be greatly appreciated!!
p.s. - please ignore SQL syntax / other errors, I typed it off the top of my head..
Thanks!
To your questions:
I would use one table. Especially if you need to report on or compare data from multiple currencies, it will be incredibly improved by sticking to one table.
If you don't have a need to track the history of each currency's value, then go ahead and just update a single value -- but in that case, why even have a separate table? You can just add "latest value" as a field in the currency table and update it there. If you do need to track history, then you will need the two tables and the SQL you posted will work.
As an aside, instead of FLOAT I would use DECIMAL(10,2). After MySQL 5.0, this will actually have improved results when it comes to currency handling with rounding.
It is better to have one table holding all currencies
If there is need for historical prices, then the table needs to hold them. A reasonable compromise in many situations is to split the price table into a full list of historical prices and another table which only has the current prices.
Using data type float can be troublesome. Please be sure you know what you are doing. If not, use a database currency data type.
As your webservice is transactional it is better if you'd have to access less tables at the same time. Since you will be reading and writing a lot, I would suggest having a single table.
Its better to insert a field to the CurrencyTable and update it rather than hitting two tables for a single request.