SQL Query to Concat output - sql

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.

Related

SQL Server Data in one table, but missing from another

This isn't as simple as the title. (What is an appropriate title?) I think the easiest way to describe my issue is with an example.
My goal is a list of what color balloon’s each child is missing.
Let’s assume table 1 contains the following data in 2 columns:
Child BalloonColor
Sally Yellow
Sally White
Sally Blue
Bob Red
Bob Green
Bob White
This is table 2, also 2 columns.
ColorCode Color
Y Yellow
W White
R Red
B Blue
G Green
P Pink
I need a write a result set that states what color balloon each child needs to have all colors.
Sally, R, Red
Sally, G, Green
Sally, P, Pink
Bob, Y, Yellow
Bob, B, Blue
Bob, P, Pink
My example is small, but assume I have a 1000 children in my table 1 and 75 colors in table 2. How can I check each child, one at a time, is my ultimate question? A Not in query will only yield "P, Pink", but you can see I need it at the child level, not table level.
I'm not a developer, but can write good SQL statements.
MS SQL Server 2008 R2.
Thanks in advance, Mike.
SELECT
SQ.child_name,
BC.balloon_color
FROM
(
SELECT DISTINCT
child_name
FROM
Child_Balloons
) SQ
CROSS JOIN Balloon_Colors BC
WHERE
NOT EXISTS (
SELECT *
FROM Child_Balloons CB
WHERE
CB.child_name = SQ.child_name AND
CB.balloon_color = BC.balloon_color
)

SQL Script to pull duplicate data

Need help pointing in right direction using an SQL query to extract data out of this SYBASE database example:
12345 blue
12345 red
12345 green
56789 purple
56789 black
98765 brown
output must look like this:
12345 blue red green
56789 purple black
98765 brown
Was trying to use "union" or maybe "distinct"
Please point me in the right direction.
Oracle uses WM_CONCAT or LIST_AGG to do this anymore I think sysbase uses List()
Source documentation describing function
So.. using your SQL
Select Field1, list(Field2)
FROM table
Group by Field1
SELECT item_loc.niin, list(item_loc.location_number)
FROM item_loc
GROUP BY item_loc.niin
I just removed a space after list before the ( so List ( became List(
Now the nature of the error your getting indicates list isn't a function in your version of sybase... I'm still trying to find documentation on sybase 15.3 and proper syntax for it (or if it supports List)

With Rails, can I use a single table to store simple values (gender, hair color, eye color)?

Say I have a Person table that stores information about that person (weird right?). I have select boxes for things like gender, hair color, and eye color. Instead of creating separate tables with a description field for each, is there a good way to use a single table? Maybe a Resources table with a Name and Description fields? Is it just that simple?
Resources
=========
ID Name Description
--------------------
1 Gender Male
2 Gender Female
3 Eye Color Blue
4 Eye Color Green
5 Eye Color Brown
6 Hair Color Black
7 Hair Color Brunette
8 Hair Color Blonde
9 Hair Color Red
Person
=========
ID Name Gender Eye_Color Hair_Color
-----------------------------------------------
1 Ryan 1 3 8
Is this the recommended way or is there something better for this?
Yes it is that simple, IMO your approach is correct. But please note you approach will not work if you get to select Ex: multiple hair colors for one person.
But I believe keeping code simple until you get a requirement to change it, read about YAGNI when u have some time :)
You could do it that way and it would be a polymorphic association.
If you don't need to query this information but just be able to access it you can use serialize and just store all the values in one column.
So a person record would have a column, let's call it attributes, that would have "eye_color: blue, gender: male", etc...
I'd create a separate table called Physical_attributes and an assossiative one between Person and Physical_attributes, personal_physical_attributes, where I'd store the person's id, the Physical_attribute's id and the description for that Physical_attribute.

SQL Random Tables

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.

Reduce and Merge duplicate rows

I have a table with the following sampled data:
Name Color
Alice Green
Bob Black
Chris Green
Chris Black
David Red
Peter Blue
Simon Blue
Simon Red
Simon Green
Ultimately, I want to reduce the table by consolidating the Color column like:
Name Color
Alice Green
Bob Black
Chris Green, Black
David Red
Peter Blue
Simon Blue, Red, Green
such that Name can become unique.
The table has no PRIMARY KEY, I got as far as creating a new column using ROW_NUMBER to distinguishing duplicates but don't know what to do next.:
rownumber Name Color
1 Alice Green
1 Bob Black
1 Chris Green
2 Chris Black
1 David Red
1 Peter Blue
1 Simon Blue
2 Simon Red
3 Simon Green
Don't do this. Instead, normalize your tables further to e.g. a Person, Preference and a Color table (where Preference, if that is the right name for the relation, has foreign keys to Person and Color). This way, you avoid the risks of inconsistencies (you can make Person names unique if you like, but you should make Color names unique).
EDITED: if you're getting this from a join query I'll assume the data is reasonably consistent, so normalization isn't an issue. Would it be possible to change the join query to GROUP on Name instead? Much cleaner than hacking around a result set, really!
I have adopted the approach here
with a table variable to hold the temporary result set to work(hack) from.
Sorted!