Multi keyfields DBLookupCombobox - delphi

I've a database with two tables: the first is named "Customers" and the second is named "Labels".
In Customers table I've this fields:
Customer prefix (CHAR 1)
Customer id (CHAR 6)
Customer name (VARCHAR 50)
In Labels table I've this fields:
Label id (AUTOINCREMENT FIELD)
Label description (VARCHAR 50)
Customer prefix (CHAR 1)
Customer id (CHAR 6)
Customer name (Lookup field)
In the first table the primary key is made by "Customer prefix" - "Customer id". The same fields are the foreign key in the second table.
In a Delphi form I've placed a TDBLookupCombobox in order to display the name of customer and I've set the listsource to Customers table and the datasource to Labels table, but I've to use to fields for setting Keyfield: Customer prefix and Customer id.
It seems there is no way to put two fields name in the KeyField of the DBLookupCombobox.
There is a way to use e multi fields key, like my example, with DBLookupCombobox?

It's not possible to use a TBLookupCombobox for a Lookupfield using multiple Keyfields,
as a consequence of this even the use of the editor within a DBGrid will fail.
If you take a look into DBCtrls.pas you will find that in TDBLookupControl.UpdateListfields
FKeyfield (TField) is extracted
FKeyField := GetFieldProperty(DataSet, Self, FKeyFieldName);
Since FKeyfield is a single field this will fail for a Lookupfield.
The same will happen for the Masterfield(TField) in UpdateDataFields:
FMasterField := GetFieldProperty(FDataLink.DataSet, Self, FDataField.KeyFields)

Related

How to assign incremental numbers but only if another column is different value

I am trying to assign a unique ID to companies that I have a list of. These companies have multiple products so the company name appears on multiple rows.
=ARRAYFORMULA(IF($B2<>"",IF((COUNTIF($B$1:$B1,$B2)>0),INDEX($A$1:$R2,MATCH($B2,$B$1:$B1,0),12),CONCATENATE("C00",ROW($C2))),""))
The above kind of works but it will assign C001 then it will see that Column C row value matches and skips but it ten assigns C009 if the next company name is 8 rows down rather than assigning C002 to this next company.
=ARRAYFORMULA(IF($B2<>"",IF((COUNTIF($B$1:$B1,$B2)>0),INDEX($A$1:$R2,MATCH($B2,$B$1:$B1,0),12),CONCATENATE("RET00",ROW($B2))),""))
I expect each different company name to have an incremental unique ID inputted to Company ID column rows.
Here is my data and expected result:
try it like this:
=ARRAYFORMULA(IFERROR(VLOOKUP(B2:B,
{UNIQUE(INDIRECT("B2:B"&COUNTA(B2:B)+1)),
TEXT(ROW(INDIRECT("B1:B"&COUNTUNIQUE(B2:B))), "C0#")}, 2, 0)))

Creating a 'join' table - sqlite3

I think I'm pretty close on this one, but can't get it to click.
I've got two simple tables set up.
Table A:
CREATE TABLE customer(
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
create_time TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
I've got two rows of data populating correctly in Table A.
Table B:
CREATE TABLE address(
...> id INTEGER PRIMARY KEY,
...> street_address_1 TEXT NOT NULL,
...> street_address_2 TEXT,
...> street_address_3 TEXT,
...> city TEXT NOT NULL,
...> state TEXT NOT NULL,
...> zip TEXT NOT NULL);
And I've successfully imported a CSV file into that table.
I'm trying to create a 3rd table that joins Table A to Table B with the use of Foreign Keys.
I can create the table with the code below, but when I try to select the table, I'm getting a blank, which means I'm obviously doing something wrong. I'm expecting to see data where the two tables overlap on mutual Id numbers, i.e. where the ID from customer = Id from address I'd like to see the data from both tables for those rows appear in Table C.
Table C (the join table):
CREATE TABLE customer_address(
id INTEGER PRIMARY KEY,
customer_id INTEGER,
address_id INTEGER,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL,
street_address_1 TEXT NOT NULL,
street_address_2 TEXT,
street_address_3 TEXT,
city TEXT NOT NULL,
state TEXT NOT NULL,
zip TEXT NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customer(id),
FOREIGN KEY (address_id) REFERENCES address(id)
);
Thanks!
I imported the data to the address table using this:
sqlite> .mode csv
sqlite> .import address.csv address
I manually typed in data to the first table using this:
insert into customer(first_name, last_name, email, password)
values('Ad','Mac','a.Mac#gmail.com','Mab'),('Brian','Obrien','bob#example.com','123456');
Don't duplicate the data in your join table (often called a bridge table). This should do for Table C:
CREATE TABLE customer_address(
id INTEGER PRIMARY KEY,
customer_id INTEGER,
address_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id),
FOREIGN KEY (address_id) REFERENCES address(id));
Duplicating columns is bad practice because it 1)defeats the purpose of using a relational model; 2)can lead to conflicting records if information is updated or deleted in one table, but not another.
Furthermore, you shouldn't have street_address_1, street_address_2, street_address_3 all in the same table. That's a violation of First Normal Form. Think of it this way, can a person have more than three addresses? Can they have two addresses in different cities? Do all three of those addresses have the same zip?

How do I maintain a Set of event properites in a window?

Is it possible to create a table with a primary key and a Set as a secondary column that would be like a list in a value of a hashtable?
something like this:
create table T (id int primary key, list HashSet )
where the list would hold all properties related to the primary key that happened over a window size.
EDIT:
This is the output I get. What I want is to keep count of unique Occurences arriving at id 1,2 and 3.
If Occurence 2 arrived 3 times at ID 1 I still only want 1 as unique, not 3
{unique=3, id=1}
{unique=3, id=2}
{unique=4, id=3}
****************
In java it is no problem, but I dont understand how to implement this in Esper. Im not even sure if using tables is the correct approach.
Tables can have aggregation-state-type columns. So the "window" aggregation is available. For example like this:
create table MyTable (id int primary key, theWindow window(*) #type(MyEvent))
into table MyTable select window(*) as theWindow from MyEvent group by id
Or the table could declare a list-type column "create table MyTable (id int primary key, somelist java.util.List)" and it is up to you to maintain the list via function calls in EPL.

Stored procedure in Oracle with Case and When

I am having a scenario, where I am having 5 different tables:
Table 1 - Product, Columns - ProductId, BatchNummer, Status, GroupId, OrderNummer
Table 2 - ProductGrop, Columns - GropId, ProductType, Description
Table 3 - Electronics, Columns - EId, Description, BatchNummer, OrderNummer, OrderData
Table 4 - Manual, Columns - MId, Description, Status, OrderNummer, ProcessStep
Table 5 - ProcessedProduct, columns same as Product with one extra column of datetime
Now, according to business flow, I need to populate all the data from Product table, and have to check if the underlying table (Electronics or Manual, which depends on ProductType column of ProductGoup) has ordernuumer value, then Insert a record in table 5 "ProcessedProduct" else skip the records.
For this requirement, i want to create a procedure. But I am stuck on how to check which underlying table (Electronics/Manual) shall i have to refer and how it can be achieved.
Moreover how should i write the loop for inserting the records.
Note: I cannot change the tables schema.
With a PL/SQL procedure you can just switch within a LOOP, but you don't need an imperative algoritm if you just need to check if OrderNummer is either into Electronics or Manuals.
Supposing the detail table is chosen by ProductType value either "Electronics" or "Manuals", you could:
INSERT INTO ProcessedProduct (ProductId, BatchNummer, Status, GroupId, OrderNummer, TS)
SELECT ProductId, BatchNummer, Status, GroupId, OrderNummer, SYSDATE
FROM Product p
INNER JOIN ProductGroup pg USING (GroupId)
WHERE EXISTS (
SELECT NULL FROM Electronics e
WHERE p.OrderNummer = e.OrderNummer
AND pg.ProductType = 'Electronics'
UNION
SELECT NULL FROM Manuals m
WHERE m.OrderNummer = m.OrderNummer
AND pg.ProductType = 'Manuals')
Plain SQL is always the fastest way, and "WHERE EXISTS" is usually the fastest condition.

LINQ to SQL and Data Projection, MVC

HI I have a table with some some values (IDs), and of course when i get the result i got just the int IDs, but i want to put it more user friendly, for example when its the number 1, i want to put the string "Avaible", when its 2 "Not avaible", im on an N tiers enviroment and i need to get this done on the Model, whats the best way to accomplish this, i have to declare another class to project the strings, or must i use something like a dictionary, Key -> Value.
right now i just have this
return from t in db.products where t.productID==productID select t;
If you are using Linq to SQL you need another table to contain product status:
Table Name: Product Status
Fields: ProductStatusID int Indentity Primary Key
ProductStatus nvarchar(50)
Add a field to your Products Table:
Field to Add: ProductStatusID int
Add some statuses to your new table, and set the ProductStatusID of each product to an appropriate status id.
Add a constraint that connects the two ProductStatusID fields together. The easiest way do this is to create a diagram in SQL Server Management Studio Express, drag both tables onto the diagram, and then drag the ProductStatusID field from the ProductStatus table to the Products table, and click OK on the dialog that opens.
Rebuild your Linq to SQL data classes. You do this by deleting and recreating the DBML file, and dragging your tables into the designer again.
When you get a products object (p) from your dataContext object, you should now see this:
p.ProductStatus <-- The text description of the product's status.
Linq to SQL will reach into your ProductStatus table, and lookup the appropriate status description.

Resources