Convert Sql row into column [duplicate] - sql

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 6 years ago.
I have this table:
Value | Name
300 | moshe
400 | yoni
500 | niv
And i would like to convert it into this:
nameColumn: moshe yoni niv
value: 300 400 500
The value is float type and name is nchar(20).
anyone?
thanks

Most databases have a PIVOT relational operator (link for SQL Server) to turn the unique values of a specified column from multiple rows into multiple column values in the output (cross-tab), effectively rotating a table.

Related

Combine 3 records into one by appending data [duplicate]

This question already has answers here:
Combine values from related rows into a single concatenated string value
(1 answer)
SQL Query to Group By and Concat rows
(1 answer)
Closed 2 years ago.
I've been asked to take an MSAccess query with results like this:
Name Age Skills
Jim 42 Access
Jim 42 Excel
Jane 38 Access
And turn it into this:
Name Age Skills
Jim 42 Access, Excel
Jane 38 Access
I have no idea where to start with something like this, or if it can be done. Doesn't matter if it's a query or if I have to write a function, so long as the results are the same. Can anyone help?

Split comma separated string and insert into new table with corresponding PK [duplicate]

This question already has answers here:
SQL Server split CSV into multiple rows
(4 answers)
Closed 4 years ago.
I have a table with values like this.
PK Values
1 abc,def,ghy,tyu
2 qwe,tyu,iop,fgt
I want to split the CSV and make a new table like this
Id Value
1 abc
1 def
1 ghy
1 tyu
2 qwe
2 tyu
2 iop
2 fgt
I already have split function but i need a query to align the values with corresponding PK
try this:
Select t.Id,f.SplitData AS Value from #MyTable t
CROSS APPLY dbo.fnSplitString([Values],',') f

How to do mathematical comparison to numeric strings in oracle sql [duplicate]

This question already has answers here:
"Safe" TO_NUMBER()
(9 answers)
Closed 4 years ago.
I have a column which stored numeric and string values. I want to filter those numeric values from that column. Suppose I need to get rows which less than 100.
mytable
id | value
-------------------
1 aa
2 103
3 cc
4 90
5 88
suppose above in above table , 'value' column type is varchar.
I want to select value<100 rows.
my result set should be like as follows,
id | value
-------------------
4 90
5 88
Here is my imaginery query, But it doesn't
select * from mytable where TO_NUMBER(value)<100;
My question is deferenct than TO_NUMBER("*") case. Important thing is i want do a mathamatical comarison, which is i want to select <100 numbers.
If the values are decimals, then you can filter out incorrect values by:
where case
when regexp_like(value, '^\d+$') and to_number(value) < 100 then 1
else 0
end = 1
SQL Fiddle Demo

Selecting where keywords match both values but in different rows [duplicate]

This question already has answers here:
Postgresql: Query returning incorrect data
(2 answers)
PostgreSQL: select all types that have an entry corresponding to all entries in another table
(2 answers)
Find a value which contains in ALL rows of a table
(1 answer)
Closed 5 years ago.
I am trying to return what id contains both keywords. For example my data is:
|fID|keyword|
|1 |word1 |
|1 |word2 |
|2 |word1 |
|3 |word2 |
if I do the SQL SELECT fID FROM table WHERE keyword = 'word1' AND keyword = 'word2';
it returns with 0 results matching I assume because it wants them to be in the same row when all I am wanting is if they both are connected with the same fID.
If I do OR instead of AND it shows the fIDs that dont have both in.
I would expect the result to output fID 1. I have been messing around with brakets in various places and group by but cannot get any success in this.
Untested, but you could try something like this:
SELECT fID
FROM table
WHERE keyword = 'word1' OR keyword = 'word2'
GROUP BY fID
HAVING COUNT(DISTINCT keyword) = 2

Convert row to columns SQL dynamically [duplicate]

This question already has answers here:
Simple way to transpose columns and rows in SQL?
(9 answers)
Pivot Dynamic Columns, no Aggregation
(1 answer)
Closed 7 years ago.
I have a table Dev with the data below
YYYMMDD Atest BTest CTest
20150525 100 200 300
20150526 110 210 310
20150527 120 220 320
I need output like below
xyz 20150525 201050526 20150527
Atest 100 110 120
BTest 200 210 220
CTest 300 310 320
How can i achieve above result set. My table Dev will grow and i need the result set table to build columns dynamically and display the data as required.
Any help is appreciated. If you suggest pivot, may i know what field should i use for aggregation and how to use it. Thanks.
First you will need to unpivot your table and after that pivot again. This is the key idea:
select * from TableName
unpivot(v for xyz in([Atest],[Btest],[Ctest]))u
pivot(max(v) for yyyymmdd in([20150525],[20150526],[20150527]))p
Fiddle http://sqlfiddle.com/#!3/a675c/1
As for dynamic sql, you can find many example of how you can use STUFF function to concatenate distinct dates in one string and construct dynamic query. For instance T-SQL dynamic pivot