Parsing column data in SQL Syntax to an SQL Query - sql

I am trying to solve a business flow issue at my work and I have an idea that I hope is technically feasible in SQL. What I would like to try and do is store different formulas in SQL syntax into database columns. Within SQL queries I would set variables to equal these columns so that the content of the columns is parsed as a part of the query and the different SQL statements pops up depending on the select specifications.
Short and sweet: I have some widgets to sell and whether or not a client can get these widgets depends on what other widgets they have bought, should have and shouldn’t have, country, customerid, their widget version, widget category and a couple of other things.
My question is how would someone proceed with this? I’m sure someone has made a similar setup before but which methods would be useful to study for my case? Are there case studies where I can find inspiration? I have searched for this without any luck. Hopefully someone who have solved similair issues before would be able to point me in a direction.
Thanks to whom‘ever is able to answer and has had the interest to read my post.
Best regards
Zaid

**OK this is more of a comment than an answer but the formatting doesn't work if I enter it as a comment!
To very vaguely answer your question I would have a Widgets table ie
WidgetID | Widget Name
1 Widget1
2 Widget2
3 Widget3
4 Widget4
Then have a WidgetRequirements table which is
WidgetRequiredID | WidgetID | RequiredWidgetID
1 1 2
2 1 3
3 1 4
4 2 4
5 3 1
6 3 4
This tells you that WidgetID 1 needs Widgets 2, 3 and 4 in order to be "active".
Widget 2 only needs widget4 to be active and Widget3 needs widgets 1 and 4.
This should get you started, expand on this theory.

Related

Can I make a integer field with bit information and use index?

Im trying to solve a puzzle more info. So I have pieces and solutions created with those pieces.
Pieces: piece_id
Solution: solution_id
If I have three pieces will have 8 solutions 2^3. My idea is use solution_id to indicate what pieces are part of it.
solution_id pieces
0 000 -- no pieces (not really a solution)
1 001 -- only the piece_id = 1
2 010 -- only the piece_id = 2
3 011 -- have piece 1 and 2
4 100
5 101
6 110
7 111 -- all pieces (not really a solution because need solve two parts)
I will need two solutions, but solution2 cant have any pieces on solution1
The questions are:
Can I do bitwise operation between integers to know if two solutions share any pieces or need a bit array?
Can I use one index to improve the perfomance of this join?
There is a better way to do this?
SELECT s1.solution_id, s2.solution_id
FROM solutions
WHERE s1.solution_id & s2.solution_id = 0
You seem to know the answers to your questions:
Yes, you can use bitwise operations.
You probably cannot use indexes to optimize this query.
However, you could store the data differently. The better data structure is:
solution_id piece
1 1
2 2
3 1
3 2
. . .
Then you could use joins and more traditional database operations.

in theory: would this be possible: SQL query for efficient use of build parts

I have a couple of tables, which I have simplified to the extreme for the purpose of this example.
Table 1, 'Units'
ID UnitName PartNumber
1 UnitX 1
2 UnitX 1
3 UnitX 2
4 UnitX 3
5 UnitX 3
6 UnitY 1
7 UnitY 2
8 UnitY 3
Table 2, 'Parts'
ID PartName Quantity
1 Screw 2
2 Wire 1
3 Ducttape 1
I would like to query on these tables which of these units would be Possible to build, AND if so, which one could be built first ideally to make efficient use of these parts.
Now the question is: can this be done using SQL, or is a background application required/more efficient?
So in this example, it is easy, because only one unit (unit Y) can be built.. But I guess you get the idea. (I'm not looking for a shake and bake answer, just your thoughts on this.)
Thanks
As you present it, it is efficient to use sql. As you described PartNumber column of table Units is a foreign key on ID column of Parts table, so a simple outer join or selecting units that the PartNumber are "NOT IN" the Parts table would give you the units that can not be build.
However if your db schema consists of many non normalised tables, or is very complex without indexes, other "bad" things etc
it could be examined whether specific application code is faster. But i really doubt it for the particular case, the query seems trivial.

Jasper / DynamicJasper: How to invert Column Headers

I have a Jasper report with the following output format:
Item | Price | Quantity
----------------------------
1 100 5
2 150 8
3 200 11
How do I make that table to this format:
Item 1 2 3
Price 100 150 200
Quantity 5 8 11
The column headers have now become row headers.
I'm actually using DynamicJasper, but of course, it's still relies on Jasper.
What special setting or property should I set to achieve the format I'm looking for.
Also, what do you call this format? Inverted Headers? Inverted Columns? It's hard to Google this issue since the keywords I'm using doesn't seem to be correct. Google always gives me a different answer.
Please check if Crosstabs serve your purpose
As it was suggested before, either check out crosstab, or you can check their CrosstabBuilder/LayoutManager classes and probably override/extend some to adopt to your needs

MySQL: Getting connected (similar) data with lef/right fields

In MySQL Im having two tables:
PRODUCTS (id, Name)
SEEALSO (id, prodLeft, prodRight)
SEEALSO defines which PRODUCTS are related together and are represented as binded fileds "prodLeft"-"prodRight".
For Example:
PRODUCTS:
1 Desk
2 Table
3 Chair
4 Doors
5 Tree
6 Flower
SEEALSO
1 1 2
2 2 3
3 3 4
4 5 6
From that we can see binding of Desk-Table-Chair-Doors and Tree-Flower.
I would now want to write SQL statement where I could specifie PRODUCT name (e.g. Chair) and i would get result of binded fields that are connected with it (e.g. Chair: Desk-Table-Chair-Doors).
From this point on i would like to know if this is even possible for my data presentation concept in SEEALSO and if it is if you could help me solve my problem.
As you're wondering whether it's even possible, you could look into this information on Nested Sets, which is the MySQL way of doing this (I gather).
I could not give you a worked sample, as I'm no MySQL expert: perhaps this will help you enough given the general nature of your question.

Database design - alternatives for Entity Attribute Value (EAV)

see How to design a product table for many kinds of product where each product has many parameters for similar topic.
My question: i want to design a database, that will be used for a production facility of different types of products where each product has its own (number of) parameters.
because i want the serial numbers to be in one tabel for overview purposes i have a problem with these different paraeters .
One solution could be EAV, but it has its downsides, certainly because we have +- 5 products with every product +- 20.000 serial numbers (records). it looks a bit overkill to me...
I just don't know how one could design a database so that you have an attribute in a mastertable that says: 'hey, you could find details of this record in THAT detail-table".
'in a way that you qould easely query the results)
currenty i am using Visual Basic & Acces 2007. but i'm going to Visual Basic & MySQL.
thanks for your help.
Bob
I would go with something like this:
product [productid, title, price, datecreated, datemodified, etc]
attribute [attributeid, title]
productattribute [productid, attributeid, value, unit]
Example:
[product]
productid title price datecreated datemodified
1 LCD TV 99.95 2010-01-01 2010-01-01
2 Car 12356 2010-01-01 2010-01-02
3 B/W TV 12.95 1960-01-01 1960-01-01
[attribute]
attributeid title
10 Colors
11 Dimensions
12 Passengers
[productattribute]
productid attributeid value unit
1 10 16 million
1 11 32 inch
2 12 4 adults
3 10 2 colors
3 11 6 inch
It seems you probably need to learn more about the available design patterns when dealing with this sort of problem as there isn't a one-size-fits-all solution.
I recommend picking up a copy of Patterns of Enterprise Application Delvelopment to help you on your way. Sorry that I'm not able to answer your question directly (hopefully someone else here on SO can) but I think the answer given in the question you linked to is about as good as it gets.