I'm handling this problem here in company: We have different customers, that need different fields in the same table, but we do not want to have a table with 300 columns, which is inneficient, hard to use and so on. Example:
table_products have this fields: product_id, product_name, product_cost.
then, the first client 'X' needs the field product_registerid.
the client 'Y' needs the field product_zipareaid.
That happens by different causes. Example: they are from different states, that have different rules.
At this moment we came up with this solution, which i don't like:
product_id, product_name, product_cost, product_personal. In this product_personal we have saved values like '{product_registerid:001;product_zipareaid:001-000131}'.
I came up with a theoretic solution: extend the table, and the sql will know when i do a query in the extended table, and shou me the column with the main table's column. Something like:
table_products with columns product_id, product_name, product_cost.
table_products_x with column product_registerid.
table_products_y with column product_zipareaid.
And the querys would return:
1.
select * from table_products where product_registerid = 001:
product_id, product_name, product_cost, product_registerid
1, iphone, 599, 001.
2.
select * from table_products where product_zipareaid = 000-000110:
product_id, product_name, product_cost, product_zipareaid
1, iphone, 599, 000-000110.
So, im accepting different suggestions for solving our problem.
Thank you in advance!
One approach would be to add a single Extended Properties table, that would look something like this:
Product_id (FK)
Client_id
PropertyName
PropertyValue
And so it would be populated with values like:
Product_id Client_id PropertyName PropertyValue
1 x product_registerid 001
1 y product_zipareaid 000-000110
Then you just join table_products to Extended_properties on Product_Id and put the Client_id(s) you want in the WHERE clause.
Note that you'll probably end up wanting to use a PIVOT query to get multiple extended properties for each client.
Related
I'm using Access 2016 to view data from a table on our SQL server. I have a massive audit log where the record being viewed is represented by a "FolderID" field. I have another table that has values for the FolderID (represented as "fid") along with columns identifying the record's name and other ID numbers.
I want to be able to replace the FolderID value in the first table with CUSTOMER_NAME value from the second table so I know what's being viewed at a glance.
I've tried googling different join techniques to build a query that will accomplish this, but my google-fu is weak or I'm just not caffeinated enough today.
Table 1.
EventTime EventType FolderID
4/4/2019 1:23:39 PM A 12345
Table 2
fid acc Other_ID Third_ID CUSTOMER_NAME
12345 0 9875 12345678 Doe, John
Basically I want to query Table 2 to search for fid using the value in Table 1 for FolderID, and I want it to respond with the CUSTOMER_NAME associated with the FolderID/fid. The result would look like:
EventTime EventType FolderID
4/4/2019 1:23:39 PM A Doe, John
I'm stupid because I thought I was too smart to use the freaking Query Wizard. When I did, and it prompted me to create relationships and actually think about what I was doing, it came up with this.
SELECT [table1].EventTime, [table1].EventType, [table1].FolderID, [table1].ObjRef, [table1].AreaID, [table1].FileID, [table2].CUSTOMER_NAME, [table2].fid FROM [table2]
LEFT JOIN [table1] ON [table2].[fid] = [table1].[FolderID];
You can run this query and check if it helps!.
Select EventTime, EventType , CUSTOMER_NAME AS FolderID FROM Table1, Table2 Where Table1.FolderID = Table2.fid;
Basically, 'AS' is doing what you want here as you can rename your column to whatever you want.
We have to combine 3 dinstinct sql databases into one. I've copied all tables into a single Database, and now I'd have to assign then toghether. the situation is this:
the database has 3 Tables. Call them TB1 TB2 TB3
each table has its own ID column
each table contains different informations about the same item, except for the shelf property. for example, tb1 contains shelf, size and color, tb2 contains shelf, quantity and serial number, tb3 contains shelf, price and material
a shelf can contain multiple items. So same shelf does not mean same item. But a single Item cannot be on 2 shelfes.
the ID numbers of the tables do not match. so for example ID 30 of tb1 is not the same item as ID 30 on tb2.
A item present in one table MIGHT not be present in other tables.
each table contains about 1000 rows
What I need to do is to come up with a tool that allows the user to quickly create connections between tables. My current idea is to make a form with 3 Datagridviews one next to the other, containing the 3 databases. Then when I select a row on the first Datagridview it automatically scrolls to the rows in the other two datagridviews where the shelfnumber is the same. (if there is one..) the user selects one row in each table and hits the save Button, the three ID numbers of the single tables are saved into a new table.
But maybe there is a better solution to this. maybe something graphical? easier to use then selecting single rows in each table?
Thanks
The lack of a common Primary Key across the tables makes this difficult - as I'm sure you discovered.
I'd try something like this and see how it looks in the DGV
SELECT 'tb1' Table, ID, shelf, size, color, NULL QTY, NULL SN, NULL Price, NULL Material from T1
UNION
SELECT 'tb2' Table, ID, shelf, NULL, NULL, quantity,serial_number, NULL, NULL from T2
UNION
SELECT 'tb3' Table, ID, shelf, NULL, NULL, NULL, NULL, price, material from T3
You might be able to add a SORT BY ID, Table to the bottom.
Each SELECT needs the same number of columns. Only the first SELECT is used for column headers.
I am trying to create one spx which based upon my ID which is 1009 will move 9 columns data to new table:
The old table has 9 columns:
CD_Train
CD_Date
CD_Score
Notes_Train
Notes_Date
Notes_Score
Ann_Train
Ann_Date
Ann_Score
userid - common in both tables
ID - 1009 - only exists in this table
and my new table has:
TrainingID,
TrainingType,
Score,
Date,
Status,
userid
TrainingType will have 3 values: Notes, CD, Ann
and other fields like score will get data from notes_score and so on
and date will get data from notes_date,cd_date depending upon in which column cd training went
status will get value from Notes_Train, cd_train and so on
based upon this, I am lost how should I do it
I tried querying one sql of users table and tried to do the join but I am losing the ground how to fix it
No idea yet, how to fill your column trainingId but the rest can be done by applying some UNION ALL clauses:
INSERT INTO tbl2 (trainingType,Date,Score,Status,userid)
Select 'CD' , CD_date, CD_score, CD_Train, userid FROM tbl1 where CD_date>0
UNION ALL
SELECT 'Notes', Notes_Date, Notes_Score, Notes_Train, userid FROM tbl1 where Notes_date>0
UNION ALL
SELECT 'Ann', Ann_Date, Ann_Score, ANN_Train, userid
FROM tbl1 where Ann_date>0
I don't know as yet whether all columns are filled in each row. That is the reason for the where clauses which should filter out only those rows with relevant data in the three selected columns.
Let's suppose we have records of product with columns ItemCode and ItemReplacementCode.
Since we are talking about one table:
ItemCode is not a primary key
ItemReplacementCode isn't foreign key.
They are just simply varchars columns.
What I would like to see in select result is:
id, productname, itemcode, replacementCode
99, dell ###, a1234X, null
10034, dell ###, 1233bX, a1234X
10024, dell ###, 1232X, 1233bX
95, dell ###, 999ws, null
96, sony ###, 327b, null
and so on. Please note that itemCode and replacementCode aren't alphabetically friendly for sorting.
Can you guide me how to select products so they will be grouped and ordered by replacement code (please note, not grouped by scalar).
Certainly we are talking about kind of building graph of "relatives" retrieved by one sql statement among with "orphan" products which have no replacement.
Don't hesitate to ask for clarification.
References to similar discussions are appreciated.
I'm open to add additional hibernate relations only if they will not harm performance.
I am not 100% sure of what you are looking for, but here are some options.
To just order by replacement code
SELECT id, productname, itemcode, replacementCode
FROM ItemTable
ORDER BY replacementCode
I see that you have NULL values in the replacement code. If you want to order by the Item, but use the replacement code if there is one try this
SELECT ISNULL(replacementCode ,itemcode) AS NewItemCode ,id, productname
FROM ItemTable
ORDER BY ISNULL(replacementCode ,itemcode)
I have three tables
Table 1: Items
ItemID | DaysLastSold
Table2: Listings
ItemID | ListingID
Table3: Sales
ListingID | DateItemClosed
I got this query to work:
SELECT min(DATEDIFF(day, DateItemClosed, getdate())) as DaysLastSold
from Sales
where QtySold > 0
and ListingID in (SELECT ListingID from Listings where ItemID = 8101 )
What I'm trying to do is basically place this query into the DaysLastSold Column in the Items table. So when ever the column is selected it recalculates DaysLastSold using the ItemID in the neighboring column.
If you want to persist that information you could create an indexed view that is made up of your calculated value and an ItemID. Obviously this would not be a column in your original table though. You could then join in on this view when you need the information.
Personally I would probably just do it inline when you need it. If you are concerned about performance, post the execution plan here and we may be able to make some suggestions.