Concat multiple line in one [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate many rows into a single text string?
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
I'm working with MS Sql Server 2008,
I have the following table
----------------
Uid | Alias |
--------------- |
1 | Pierre |
1 | Patrick |
1 | Jean |
2 | Alice |
2 | Diana |
and I want to display it in this manner:
------------------------|
Uid | Alias |
------------------------|
1 | Pierre Patrick Jean|
2 | Alice Diana |
Any idea will be appreciate.

Please try:
select b.Uid,
(select a.Alias +' ' from TableName a WHERE a.Uid=b.Uid group by a.Alias FOR XML PATH(''))as Names
from TableName b
group by b.Uid

Related

how to limit selection with select [duplicate]

This question already has answers here:
LIMIT 10..20 in SQL Server
(15 answers)
Closed 2 years ago.
I tried using limit order to get a result like
student_id | name | major
1 | kate | bio
2 | david | chem
3 | jake | math
instead of
student_id | name | major
1 | kate | bio
2 | david | chem
3 | jake | math
4 | ... | ...
5 | ... | ...
which means 3 results instead of 5 but i got a syntax error and couldn't find solutions. any suggestions?
SQL Server uses SELECT TOP or OFFSET/FETCH to limit rows. So, your query should look like:
select top (3) t.*
from t
order by student_id;
Note that the ORDER BY is important if you care about which rows you want. SQL tables represent unordered sets. You need an ORDER BY if you care about the ordering.

Merge more than two rows into one row and place right data into right column [duplicate]

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
How to convert rows into columns in SQL Server?
(2 answers)
Closed 3 years ago.
First of I'm sorry if the question heading seems confusing to you guys but let me show you what I want.
Let's say I have a table called topic.
Topic_ID | Parent_Topic_ID | Topic_Text | lang_culture
---------|-----------------|----------------------|--------------
1 | 0 | Newton's Law | en-US
2 | 1 | First Law | en-US
Now I have another table named translated_topic where the same topic is stored in different languages
Topic_ID | text | lang_culture
---------|-------------------- |--------------
1 | न्यूटन का नियम | hi-IN
1 | loi de newton | fr-FR
2 | पहला कानून | hi-IN
2 | Première loi | fr-FR
Now, I want the show output like this...
Topic_ID | Topic_Text | lang_culture | hi-In | fr-FR
---------|--------------------|--------------|------------------ |-------------
1 | Newton's Law |en-US | न्यूटन का नियम | loi de newton
2 | First Law | en-US | पहला कानून |Première loi
How can I achieve this output in SQL-server?
What I am getting now,
SELECT td.topic_id,
td.Topic_Text AS MainText,
td.lang_culture,
tt.text,
tt.lang_culture
FROM dbo.topic td
LEFT JOIN dbo.translated_topic tt
ON td.topic_id = tt.topic_id;
But after that don't know how to get my desired result?
You can you PIVOT as below-
SELECT *
FROM
(
SELECT A.Topic_ID,A.Parent_Topic_ID,A.Topic_Text,A.lang_culture lang_culture,B.lang_culture B,B.text
FROM Table1 A
INNER JOIN Table2 B ON A.Topic_ID = B.Topic_ID
)P
PIVOT
(
MAX(text)
FOR B IN ([hi-IN],[fr-FR])
) PVT

T-SQL statement [duplicate]

This question already has an answer here:
SQL server T-SQL statement
(1 answer)
Closed 5 years ago.
I have one table its name - UserData and its contain one column name -
UserInfo.
UserInfo column contains following values likes
demo.acc.in
swiss.com.au
austa.edu.co
I want to extract the following information from it...
Output Output
acc in
com au
edu co
Thanks in advance for help
You can use parsename() for this example.
select parsename(UserInfo,2), parsename(UserInfo,1)
Note that parsename() reads from right to left, so the 1st element is the last element when reading from left to right.
rextester: http://rextester.com/FUGXQ46552
create table t (UserInfo varchar(32))
insert into t values
('demo.acc.in')
,('swiss.com.au')
,('austa.edu.co')
,('sqlzim.austa.edu.co')
select
p1=parsename(UserInfo,1)
, p2=parsename(UserInfo,2)
, p3=parsename(UserInfo,3)
, p4=parsename(UserInfo,4)
from t
+----+-----+-------+--------+
| p1 | p2 | p3 | p4 |
+----+-----+-------+--------+
| in | acc | demo | NULL |
| au | com | swiss | NULL |
| co | edu | austa | NULL |
| co | edu | austa | sqlzim |
+----+-----+-------+--------+

Selecting rows joined to two specific other rows [duplicate]

This question already has answers here:
How to filter SQL results in a has-many-through relation
(13 answers)
Closed 8 years ago.
I have a standard Twitter-style schema with users who can follow and be followed by other users. I'd like to select the users who follow two other specific users.
We have four users in the users table.
--------------
| id | name |
--------------
| 1 | Alan |
| 2 | Peter |
| 3 | Clare |
| 4 | Julia |
--------------
The relationships table describes who follows who. followed_id and follower_id are foreign keys for users.
Julia follows Alan and Peter. Peter follows Alan and Julia. Clare follows Alan.
----------------------------------
| id | followed_id | follower_id |
----------------------------------
| 1 | 1 | 4 |
| 2 | 2 | 4 |
| 3 | 1 | 2 |
| 4 | 4 | 2 |
| 5 | 1 | 3 |
----------------------------------
I'd like to select only the users who follow both Alan and Peter (i.e. the result should be Julia alone). How do I do this?
I think you need to join two times relationships table with person table like the following query
SELECT DISTINCT relationships.follower_id,followers.name from relationships
INNER JOIN users as followers on relationships.follower_id = followers.id
INNER JOIN users as followed on relationships.followed_id = followed.id
where followed.name IN('Alan','Peter');

SQL multiple characteristics in one line [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 8 years ago.
I have a question regarding how to merge lines with same characteristics:
I have this data:
Client | Product | Date
Hannah | TV | 1 Jan
Tom | Laptop | 3 Feb
Peter | iPod | 2 Jan
Hannah | Laptop | 5 Feb
Tom | iPod | 5 Feb
And I want to create this:
Client | Product-History|
Hanna | TV-Laptop |
Tom | Laptop-iPod |
Peter | iPod |
Anyone know if this is possible in SQL?
If you require actual SQL code to make it easier to respond let me know, it's the first time I ask a question.
Thanks!
Edit: I am using SQL Server
You can Try This query:
SELECT client,group_concat(product) as Product-History
from YOUR_TABLE group by client
select client,group_concat(product)
from tablename group by client