SQL Random Tables - sql

I want to implement graph coloring using databases.
There is a table that will store all the vertices (1,2,3...) and a table that stores the name of all colors(red,blue,green,etc..).
Now a want to create a coloring table with columns vertex and color which will take all possible combinations from the above tables and then check the constraints in each of those tables. Whichever table satisfies the constraints of graph coloring is a solution.
Now how to create tables for each combinations??
Guys please help. Stuck on it from a while...
An example instance:
vertex
1
2
3
Colors
red
blue
coloring
a)
1 red
2 blue
3 red
b)
1 red
2 red
3 blue
c)
1 blue
2 red
3 red
.
.
.
6 tables

I'm not sure I understand your question, so I'll make some assumptions. Assuming you have a table called Vertex, with the following rows:
1
2
3
... and a table called Color, with the following rows:
Red
Green
Blue
... you can generate a table of all possible combinations with a simple unconstrained join, like this:
SELECT *
INTO VertexColor
FROM Vertex, Color
The result will be a new table, with the following rows:
1, Red
1, Green
1, Blue
2, Red
2, Green
2, Blue
3, Red
3, Green
3, Blue
Happy to help further if this does not answer your question.

SELECT Vertices.vertex, Colors.Color from Vertices
CROSS JOIN Color from Colors
EDIT: Seeing the new comments: This doesn't sound like a problem that is well suited for SQL, mainly because your number of columns in your resultset is dependent on the number of rows in your vertices table. That's not something that is easy in SQL (you probably need a multistep process, using dynamic sql through sp_execute). Since the ordering of the colums carries significance, you can't return a resultset containing only each vertex - color pair either, because the order in which the rows are returned may vary. To me it sounds like a problem better handled outside the database engine. You can still use the above cross join to get a preliminary dataset, where you filter out some conditions you have on the set.

Related

Pivoting DB2 data on multiple conditions

I'm trying to get a query together that selects products based on a combination of data (body type, materials and colors,etc.) and I've gotten close but I'm still missing a step to get exactly what I want. I've been looking more at pivoting data and I've tinkered with this query but I'm still left with this issue.
Basically, I have multiple products that only have one material and color attributed to them and they each have a unique stock keeping ID, but some of the body types will have 2 materials and colors per stock keeping unit.
My current results in this db fiddle https://www.db-fiddle.com/f/u4zKAdw3H4hFLbfnzEeZS2/1
are as shown:
For the most part the results are there but BodB has the same color for both materials and should be one single row like BoDB | Fabric | Black | Leather | Black
So if one body code is attached to 2 different materials but the same color with sequence 1 and 2, it should be a single row effectively grouped by the body and the color (the materials wouldn't be the same, only the color. And there will only ever be up to 2 materials and colors on a given stock keeping unit
The fiddle is ready to go with these results, any help is much appreciated

Does this explanation of Left Joins make sense?

This is the query I'm trying to visually represent and diagrams and small explanation I made.
I realize it would be much easier not using Venn Diagrams for joins but I don't understand it enough to use anything else like Cartesian products.
I just want to make sure it makes sense because I'm having a hard time understanding the joining of 3 or more tables.
You would want something like this:
You will have the entirety of the blue (author) area.
You will have the red (Allocation) area that overlaps the blue (Author) area and for the rest of the blue area that does not overlap the red area then the Allocation column values will be NULL.
You will have the green (Book) area that overlaps the intersection of the red (Allocation) and blue (Author) area (where all 3 colours overlap) and for the rest of the blue area that does not overlap the green area then the Book column values will be NULL.
The areas of the red (Allocation) and green (Book) circles that do not overlap the blue (Author) circle will be excluded from the result set.
For a 3 table left join:
1.For table Author, it looks at every entry and checks if it exists in
table Allocation. If it does not, it returns null.
2. For those entries which were not null in the previous join, checks
if they exist in table Book. If it does not, it returns null.
Hope this helps. I agree that the diagram might not be the best representation for 3 or more tables joined.

Access - Sort by a combination of fields

I had (see below) a table of (fictional) stars and then two tables showing its equally fictional satellites which could be planets or asteroid belts. tblStars, tblAsteroids and tblPlanets respectively. Each of the two satellite tables had a position field which was unique across the two tables - by this I mean that the star with ID 1 had only one satellite in position 1, 2 etc which could have been in either of those two tables but not both. I wanted to sort the satellites in order of position on my reports but couldn't see a way of sorting across the combination of those fields:
tblAsteroid:
Asteroid ID Position
1 1
2 3
tblPlanet:
Planet ID Position Biome
1 2 Ice
Giving:
Position AsteroidOrPlanet Biome
1 Asteroid N/A
2 Planet Ice
3 Asteroid N/A
For the avoidance of doubt, I recognise that this problem was caused by a flaw in my database design and I should have had a tblSatellite which contained that position and was in a 1 to many with tblStar and in 1 to 0-1's with tblAsteroid and tblPlanet. I've since fixed this, I'm just wondering if it would have been possible.
To get a combined list, you need a UNION query anyway. This you can sort by a common field.
SELECT Position, 'Asteroid' AS AsteroidOrPlanet, 'N/A' AS Biome
FROM tblAsteroid
UNION ALL
SELECT Position, 'Planet' AS AsteroidOrPlanet, Biome
FROM tblPlanet
ORDER BY Position

SQL Query to find a record with a certain number of specific characters (4 vertical bars)

In the database I'm working with, we have a certain naming convention that should be followed and I'm trying to fix errors.
For example, one of the correctly named records would be:
Blue Insurance | Blue Agency | Blue Agency | BL26 | Blue Insurance
Is there any way to search for all records that do not have 4 vertical bars in them?
Thanks!
select * from tablename
where length(colname) - length(replace(colname,'|','')) <> 4
Change length according to the database being used.

SQL Query to Concat output

I have an addition SQL question, hopefully someone here can give me a hand.
I have the following mysql table:
ID Type Result
1 vinyl blue, red, green
1 leather purple, orange
2 leather yellow
and i am seeking the following output:
ID Row One Row Two
1 vinyl blue, red, green leather purple, orange
2 leather yellow
the thing is... type is not static... there are many different types and not all of them have the same ones. They need to follow in order.
Please post a show create table of your table. It's not clear what you mean in fact.
Maybe what you need is GROUP_CONCAT after all:
mysql> select ID, GROUP_CONCAT(type,' ',result) from test;
Let us know.