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)
Related
I have a table that looks like the following:
id | cars
1 | John's Honda
2 | Andrew's red lexus
3 | James has a bmw
I need to just get the last word of the "cars" column that shows the actual "car" name
I have tried the followings but I don't get the desired output
select substr(cars, -1)
from t
the code above just shows me the last charater of the column. Later, I tried the following:
select split(cars, ' ')[offset(1)]
from t
however, I got the "Array index 1 is out of bounds (overflow)" error. Can anyone help how this can be achieved with bigquery?
Consider below simple approach
select *,
array_reverse(split(cars, ' '))[offset(0)] as brand
from your_table
if applied to sample data in your question - output is
Note: there are really many ways to accomplish your case - so anoher one would be regexp_extract(cars, r'\b(\w+)$') as brand
This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 4 years ago.
I have looked at what was marked as the duplicate of this and it is not. I'm pulling from two tables, not one.
First, allow me to say I had nothing to do with the design of this database.
I have two tables that must be joined, and then an unknown amount of rows where the data must be concatenated into one giant string. They are joined by the Record ID.
Item table:
Item RecordID
---------------------
Car A 123
Car B 456
Car C 789
Yes, the words literally cut off in the middle. There should be nothing added between the values, and I also need to keep the commas and other special characters.
Details table:
RecordID Details
--------------------------------
123 black pain
123 t, radials
123 , green le
123 ather, spo
123 rt steerin
123 g wheel, b
123 uilt-in GP
123 S
456 standard
789 black leat
789 her, teles
789 coping ste
789 ering whee
789 l, seven c
789 up holders
789 , heavy du
789 ty mudflap
789 s
What I want to end up with is this:
ItemID RecordID Details
----------------------------------------------------------------------------
Car A 123 black paint, radials, green leather, sport steering wheel, built-in GPS
Car B 456 standard
Car C 789 black leather, telescoping steering wheel, seven cup holders, heavy duty mudflaps
I've looked at all the XML ones and can't figure out how to do this.
Thanks in advance.
There is no guarantee that your STUFF/ FOR XML PATH will produce the results you ask for unless you have an IDENTITY field in your Details table or some other value that you can sort by that will force the order of the Details text.
Usually you could use the STUFF command with an ORDER BY statement
SELECT
Item.Item AS ItemID,
Item.RecordID,
STUFF( (
SELECT
'' + Details
FROM
Details
WHERE
Details.RecordID = Item.RecordID
-- ORDER BY SomeLineIndicator
FOR XML PATH ('')
), 1, 0, '' ) AS Details
FROM
Item
I tried this on my box without the ORDER BY and just so happened to get the result you're asking for, but you really can't rely on these results without a field you can use to force the order.
Please read this post and the linked articles for more information about why you'd need a field for this and why you can't depend on an undetermined internal "index" to take care of it for you: Default row order in SELECT query - SQL Server 2008 vs SQL 2012
ROW 1:
<a:employees xmlns:a="abc.com/123" xmlns:b="xyz.net/456">
<a:emp>
<a:name>Scott</a:name>
<b:favorites>
<b:color>red</b:color>
<b:color>orange</b:color>
</b:favorites>
</a:emp>
<a:emp>
<a:name>John</a:name>
<b:favorites>
<b:color>blue</b:color>
<b:color>green</b:color>
</b:favorites>
</a:emp>
</a:employees>
Row2:
<a:employees xmlns:a="abc.com/123" xmlns:b="xyz.net/456" xmlns:c="pqr.edu/789>
<a:emp>
<a:name>Tiger</a:name>
<b:favorites>
<c:phone>apple</c:phone>
<c:phone>samsung</c:phone>
<b:color>purple</b:color>
<b:color>pink</b:color>
</b:favorites>
</a:emp>
<a:emp>
<a:name>peter</a:name>
<b:favorites>
<c:phone>nokia</c:phone>
<b:color>violet</b:color>
<b:color>indigo</b:color>
</b:favorites>
</a:emp>
</a:employees>
Above two xml documents are the rows of an xml column xml_col of table your_table.
I tried to parse the xml columns into relational data using the below query.
select x.*
from your_table y,
XMLTable(XMLNamespaces('abc.com/123' as "a",
'xyz.net/456 as "b",'pqr.edu/789 as "c"),'*:employees'
Passing y.xml_col
Columns
name varchar2(20) PATH '//*name',
phone varchar2(20) PATH '//*phone',
color varchar2(20) Path '//*:color')x
I am getting the below error message
"XMLTABLE in oracle - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence"
The problem is due to having multiple color and phone occurences. I want the multiple occurences to be in multiple rows. I dont mind even if the name value repeats.
Output should be similar to the below format.
Name Phone Color
Scott red
Scott orange
John blue
John green
Tiger Apple purple
Tiger samsung pink
peter nokia violet
peter indigo
Thanks in advance
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.
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.