Microsoft Access 2010 - Foreign Key as Dropdown - forms

I have two tables that I display in a form:
tblUsers -> user_id, firstname, lastname, group_id
tblGroups -> group_id, groupName, groupDesc
I can get the data that I want with:
SELECT tblUsers.firstname, tblUsers.lastname, tblGroups.groupName
FROM tblGroups INNER JOIN tblUsers ON tblGroups.[group_id] = tblUsers.[group_id];
But what I need is a form that shows the user information and a dropdown list for the group but showing the name of the group not the ID and so the group can be changed for a specific user e.g. Manager, Editor etc...
Thanks,
Gareth

Right-click on the ComboBox (assuming it's a ComboBox...) and open the Properties window.
Set the Row Source to:
Select Group_ID, GroupName from tblGroups
Set the Column Count to 2, because you want the combo to store both the ID and the Group name
Set the Column Widths to "0; 2" (without the quotes). This will essentially hide the ID because the column width of the ID field is 0.
Set the Bound Column to 1, because you want to bind to the ID column and not the Group Name column, because the Group ID is easier to query with.
Then you reference the combo with Me!MyComboboxName.Value to get the Group ID.

Related

Delete oldest data with Talend

I need to delete the oldest address of each user I have in a database with Talend. I assume that all the users in the database have more than one address. The user must be in the “student” user group.
The address is in the user_address table (example row included):
id user_id address_1 address_2 city country created_date
1 1 1 Rosebery Pl London UK 2017-03-12
The user group is in the user_group table (example row included):
user_id usergroup
1 Student
What components do I need? This is what I have so far but not sure it is correct.
tFileList ->Iterate -> tFileProperties -> Main -> tUnite -> Main -> tSortRow -> Main -> tSampleRow -> Main -> tFileDelet
First fetch the data in ASC order by using tDBInput component to give the oldest data on top:
select id, user_id, address_1, address_2, city, country
from student
order by created_date ASC
Get the First Match record using tMap
The image represents the component mapping to delete the oldest record in address table using tDBOutput

Postgres Select one record per matching condition

I have some issues while trying to get only one record per matching condition..
Let's suppose I have Certifications table with the following columns:
Id, EmployeeId, DepartmentId, CertificationTitle, PassedDate
An employee can have more then one record in this table but I need to get only one record per employee (based on latest PassedDate)
SELECT Id, EmployeeId, CertificationTitle
FROM certifications c
ORDER BY EmployeeId, PassedDate DESC
From this select I need somehow to get only the first record for each EmployeeId.
Does anyone have any ideas how I can achieve this, Is it possible?
The Id is the Primary Key on the table, so it is different on each record.
I need to keep all this columns specified in the Select query.
The Group By didn't worked for me, or maybe I did it wrong...
Use DISTINCT ON. This returns exactly the first ordered record of the group. You ordered correctly by PassedData DESC to get the most recent record first. The group for DISTINCT ON, naturally, is EmployeeID:
SELECT DISTINCT ON (EmployeeId),
Id,
EmployeeId,
CertificationTitle
FROM certifications c
ORDER BY EmployeeId, PassedDate DESC

Update count in row after Insert

I'm completely new to SQL and have a question. I am using is PostgreSQL.
I have two tables called "employees" and "offices"
The table "employees" have a list of unique employees with each having an OfficeID (The office where they work).
What I want to do is to "count" the number of appearances of the Office_ID and take that count into the table "offices" where the "office_ID" have a column called "number_of_employees".
Being completely new to SQL the only thing I have managed to even come close to this is fore example.
SELECT COUNT(*)
FROM employees
WHERE office_id = 203
But this only selects and gives the sum of rows with the id "203" that has to be manually entered into "number of empolyees"
What I want is a trigger function that updates the field "number_of_empolyees" when a new record is inserted into the table "empolyees"
A view is the way to go here.
I am assuming since you're completely new to SQL, you're unsure how to make it work (Edit: just seen your comment after posting :^D) .
The correct way to count employees for each office is:
SELECT office_id, COUNT(*) as employeeCount
FROM employees
GROUP BY office_id
Note how your WHERE office_id = XXX has been replaced by a GROUP BY office_id in order to count employees for all offices in a single query.
That being done, we can use it inside the view.
Be careful about the JOIN: I believe in your schema, an office may have no employee (for instance, right after you created it or right before you delete it). We will handle that part with a LEFT JOIN.
CREATE VIEW OfficeWithEmployeeCount AS
SELECT Offices.*, EmployeeCount
FROM Offices
LEFT OUTER JOIN (SELECT office_id, COUNT(*) as EmployeeCount FROM Employees GROUP BY office_id) T
ON Offices.office_id = T.office_id
Note: to avoid having NULL returned in EmployeeCount for empty offices, you may want to write:
CREATE VIEW OfficeWithEmployeeCount AS
SELECT Offices.*, COALESCE(EmployeeCount,0)
FROM ...

Get the value of visible column of JasperReports Server input control?

I have a report in JasperReports Server 5.2.0, in this report I have two input control
first one is:
COUNTRY (value column- COUNTRY_ID, visible column- COUNTRY_NAME)
and another one is
STATES (value column- STATE_ID and visible column - STATE_NAME).
These two parameters I am passing to the report, now at the end of the report I want to show the input parameter selection value so user can see what he has selected. But I am not able to get the visible values of parameters. I can print only value column value.
So my question is there any way to print the visible column values of input control in reports?
Till now , Visible Columns are not passed,where Value Columns are passed to Jrml File in Jasper.
To answer you question There is a way to print the visible column values.
It can be done using Sub-queries, by using 'Value Column' passed to the JRML file.
Then add the subquery to your main query and it`s done!!
In the above scenario You can display
State Name
SELECT States.STATE_NAME from States WHERE state_id = $P{STATE_ID} - If it is Single Select input control
SELECT GROUP_CONCAT(States.STATE_NAME) from States WHERE state_id = $X{IN,state_id,STATE_ID} - If it is Multi-Select input control
Country Name
SELECT Country.COUNTRY_NAME from COUNTRY WHERE country_id= $P{COUNTRY_NAME} - If it is Single Select input control
SELECT GROUP_CONCAT(Country.COUNTRY_NAME) from COUNTRY WHERE country_id = $X{IN,state_id,STATE_ID} - If it is Multi-Select input control
Suppose this your main query where Country and State are missing..
SELECT
`id`,
`product-id`,
`name`,
`description`,
`branch`,
`stock`,
`price`
FROM
`products`
WHERE
`name` LIKE "%car%"
The modified query will looks like..
SELECT
`id`,
`product-id`,
`name`,
`description`,
`branch`,
`stock`,
`price`,
(SELECT States.STATE_NAME from States WHERE state_id = $P{STATE_ID}) as state,
(SELECT Country.COUNTRY_NAME from COUNTRY WHERE country_id=P{COUNTRY_NAME}) as country
FROM
`products`
WHERE
`name` LIKE "%car%"

need help to write a trigger

I have three tables:
Orders:
orderid, valuedid, valuesdesc
Customers:
customerid, cutomdesc
Groups:
groupid, groupdesc
valueid - id of a customer or a group
valuesdesc - must filled with appropriate for inserted valueid description from Customers or Groups depend on what (customer or group) user selected in the client.
So, when client send an insert query for Orders it consists orderid for new order and valuedid. And on a client side I know what user selected: group or customer.
What I need: if a new row inserted in Orders, corresponding valuesdesc for valuedid from Customers or Groups inserted in valuesdesc column.
I have an idea to insert with new order record valuesdesc which would contain key - fake value to pick the right dictionary (customers or groups), but how to create that trigger I unfortunately don't know for now.
First, I must say this is a very uncommon design. When you are inserting a record into Orders, you use valuesdesc to hint at the table that valueid references. But as soon as valuesdesc gets filled by the trigger, valueid essentially loses any meaning. That is, you haven't reveal us you've got any other way to tell a reference to Customers from a reference to Groups. So, you could just as well drop valueid altogether and use valuesdesc to pass both the 'dictionary' hint and the reference within that table.
Other than that, because valueid can reference more than one table, you cannot use a foreign key constraint on it.
Anyway, in your present design you could try this:
CREATE TRIGGER Orders_UpdateValuesdesc
ON Orders
FOR INSERT
AS
UPDATE o
SET valuesdesc = COALESCE(c.customdesc, g.groupdesc)
FROM Orders o
INNER JOIN inserted ON o.orderid = i.orderid
LEFT JOIN Customers c ON i.valuesdesc = 'Customers'
AND i.valueid = c.customerid
LEFT JOIN Groups g ON i.valuesdesc = 'Groups'
AND i.valueid = g.customerid
The strings 'Customers' and 'Groups' are meant as the dictionary specifiers. You replace them with the actual values.
The trigger uses LEFT JOIN to join both Customers and Groups, then fills Orders.valuesdesc with either customdesc or groupdesc, depending on which one is not null.