Concatenate on JobID [duplicate] - sql-server

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate a selected column in a single query?
I know this is fairly simple in MySQL, but i don't know how to do it in SQLServer. I have a table containing two columns. UserID & TagID. I'd like to concatenate TagID into a comma separated string, where the result is grouped by UserID. How can i do that?

I would ythink something lik ethis might be what you are looking for :
SELECT ''+Table1.UserID+','+Table2.TagID+'' AS CombinedCSVColumn
, SUM(Table1.Value) AS Value
From dbo.Table1 as Table1
INNER JOIN db.Table2 as Table2
ON Table1.EmployeeID = Table2.EmployeeID
GROUP BY ''+Table1.UserID+','+Table2.TagID+''
That is if they come from two Tables. For one table is similar.
Regards
Mac

Related

SQL Server- Get all column names in the table [duplicate]

This question already has answers here:
How can I get column names from a table in SQL Server?
(22 answers)
Closed 6 months ago.
I have a table name as 'tblOrder', having three columns. I would like to print the column names.
I want a script that should return a result as:
OrderId Name OrderTrackingNo
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('tblOrder')
Try this

SQL server - Query optimization [duplicate]

This question already has answers here:
IN vs OR in the SQL WHERE clause
(8 answers)
Closed 5 years ago.
I would like to know from below queries, which one would give better performance and how?
select * from TableA where (Name = 'ABC' or Name = 'DEF' or Name = 'GHI')
or
select * from TableA where Name in ('ABC','DEF','GHI')
Internally both IN and OR operators perform the same action. More importantly you need to see if you have proper index on the Name column.You can have a non-clustered index on the Name column and have one clustered index on this table.

How to Pivot table in SQL Server? [duplicate]

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 5 years ago.
I have a table that currently looks like what is below. I'd like to only have one row per date and then have each EditType value be a separate column with the count so I can easily view the different types of edits based on the day. Can this be done using a pivot?
Or... would it be better to back up a step before I've grouped the rows in order to get the counts, like what is shown below?
The desired output would be something like this:
SELECT
Gdb
, Tbl
, EditDate
, [I]
, [U]
, [D]
FROM #TABLE
PIVOT (
COUNT(EditType)
FOR EditType IN ([U], [I], [D])
) AS PIVOTED

merge values in two rows in an sql table appear in two columns of one row [duplicate]

This question already has answers here:
Understanding PIVOT function in T-SQL
(7 answers)
Closed 5 years ago.
I need help in SQL. I have a table with this structure.
:
What I need is a query to return the data like this.
:
Thank you very much in advance!
Kind regards,
bgergoe
This has been asked and answered dozens of times but it is super simple to write out the code. Conditional aggregation is pretty straight forward and the code is far less obtuse than PIVOT. Here is how you would go about this.
SELECT ID
, MAX(CASE WHEN Field = 'name' THEN Value END) AS name
, MAX(CASE WHEN Field = 'phone number' THEN Value END) AS PhoneNumber
FROM YourTable
GROUP BY ID

how to self join a table to get more info [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate many rows into a single text string?
How to use GROUP BY to concatenate strings in SQL Server?
I have a table customer (id, person_id, account number), id is primary key. For example
id person_id account number
1 0001 acc00011
2 0001 acc00012
3 0002 acc00021
Now I want to write a query to self join the table to get all the account numbers for each person. The result table should be
person_id account
0001 acc00011, acc00012
0002 acc00021
How do I write the query?
You can't do that unless you want accounts concatenated in a single string by person_id.
See this questions:
How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?
Concatenate many rows into a single text string?
How to use GROUP BY to concatenate strings in SQL Server?
Use a correlated subquery with FOR XML PATH and GROUP BY for the concatenation. Use STUFF to remove preceding comma from results.
See example here: http://sqlfiddle.com/#!3/e5bd0/5

Resources