How to add a record into table, that is related to another table? - sql

Good day!
I have two tables.
TABLE 1:
GENERIC [GE_ID / number] [GE_DATEIN / date] [GE_PERSON / number] + ...
TABLE 2:
WORKFORCE [WF_ID / number] [WF_NAME / text] [WF_SHIFT / number] + ...
Column [GE_PERSON] from table #1 is related to the column [WF_ID] from table #2 as many-to-one relationship. I have a simple form to add a data to the table #1 with several drop down boxes. One of these drop down boxes contains a list of names taken from table #2 (column [WF_NAME]) by SQL statement.
So when I am ready to add a record to table #1, I know the person name chosen from drop down box, but column [GE_PERSON] is numeric and I have to add a number equal to the column [WF_ID] which is pointing on name in table #2 (column [WF_NAME]).
QUESTION: how should I build the SQL statement (INSERT INTO) to make this work?
Thank you!

MS Access' combobox control allows the bound column to be hidden from the user while showing identifiable data columns that correspond to hidden field. See this section of MS tutorial. Once you do so, you will have a relevant number for your querying needs.
Brief steps to hide and set primary key as bound column:
For the [WF Names] combobox, include [WF_ID] in the Row Source query under Data tab of Property Sheet as first column:
SELECT [WF_ID], [WF_NAME]
FROM [WORKFORCE]
Under Data tab make [WF_ID] the bound column by placing 1.
Under Format tab, set Column Count to 2 (or more for each field in query) but give Column Width to first column nothing, something like: 0; 2
Now the combobox's value is the corresponding [WF_ID] of the selected [WF_NAME] in drop down list, so any query pointing to the control will be a valid number:
INSERT INTO [GENERIC] ([GE_PERSON])
VALUES (Forms!formname!WFNamesComboBox)
SELECT * FROM [WORKFORCE]
WHERE [WF_ID] = Forms!formname!WFNamesComboBox

Related

Using a property control to use unique values in a column for a Spotfire calculated column

I am creating a calculated column (C) that uses a column property which is set through unique values in a column (B) which itself is set through a column selector. When I change the the data in this Column B (and column type remains the same) my calculated column (C) is not being updated appropriately. I know I am likely making a simple error but I can't figure it out.
Column B = [${Surface.Property}] #This property is set with a drop down menu where I choose which column (e.g., Column A or Column D (each have their own unique values but they are both strings)
Column C =
MostCommon(case when [Column B]="[${MyData}.{Column B}.{Surface.Selector}]" Then Min([Depth]) OVER (Intersect([Location],[Column B]))
else NULL
END) OVER (Intersect([Location]))
This series of calculations works perfect if choose Column A in the drop down menu "Surface.Property" which is use to set the column/values of Column B. However, when I change and select Column D as Surface.Property, the calculation no longer works. Im guessing this is the result of of "unique values" change but I'm not sure how to make this part of the calculation [Column B]="[${MyData}.{Column B}.{Surface.Selector}]", accept "new" values.
Any thoughts would help and hopefully this is somewhat clear!
Figured it out, it turns out I need to insert the column property into my calculated column As Value rather than As Text.

How to retrieve all columns from a table but one column should look up the value in another table (SQL-server)

I have two tables in a SQL DB, Item and ItemPlacement. I need all the data from Item but there is one column (ItemPlace), which has a key value instead of the name of the item placement. The table ItemPlacement holds the corresponding key in the ID column and the name of the placement in the PlacementName column. How do I get all the records from Item but with replaced values in the column ItemPlace from the column ItemPlacement.PlacementName? None of the examples I have come across writes SELECT * FROM Item ... Which Id really need as I don't know the numbers of columns in the Item table (using the statement dynamically on other databases).
basic example
SELECT abc.col1, abc.col2, def.col3
from [table a] abc
join [table b] def
on def.col = abc.col

Updating a column in a table from another table using postgreSQL

I have table named ´cities´ with 3 columns named:
state, name, pop
And a table cities1 with:
state, name
The columns state and name are common for both tables. I want the column pop alone from cities to be inserted in cities1 table.
How this can be done using postgreSQL?
First you need to add a column pop of specific type to your cities1 table, and then fill it.
ALTER TABLE cities1 ADD COLUMN pop [datatype here]
Update pop column in table cities1 with values from cities where state,name are the common columns.
UPDATE cities1
SET pop = cities.pop
FROM cities
WHERE cities1.state = cities.state
AND cities1.name = cities.name

Dynamic list for drop-down menu

In colunm "A" I have a list that will be filled up by the user; it starts at "A5" but I don't know where it will end.
In column "B" I'd like to make cells look like dropdown menus where items in the list will be the values from column "A".
And whenever the user adds a value to column "A" this should be automatically added to the list.
Create named range (say colA) with formula:
=$A$5:INDEX($A:$A,MATCH(2,1/($A:$A<>"")))
and then use Data validation with named range colA:
In Column A, set up your starting data list as a Table (select your list and then Insert/Table).
Give your new table a nice name (select the Table then go to Table Tools/Design/Table Name).
Tables in Excel automatically expand when a new value is added to an existing Table.
Next, select this same starting data list and create a Named Range. Go to Formulas/Define Name.
Give your list a Name (for example, "List"), and in Refers To enter your Table name. (e.g. type "=Table1" if that's the name of your Table)
I am assuming that for your Column B, you want Data Validation - it puts the dropdowns in each cell as you suggest.
Select the range of cells that you are going to validate, then go to Data/Data Validation.
Under Settings, choose Allow = List.
Under Source type the name of your Named Range that you set up in Column A. (e.g. type "=List").
Add an Input and Output message to your Validation (optional but helpful).
Click OK to close.
Credit to Chandoo.org where I learned this.

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.