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
Related
This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
MSSQL - GROUP_CONCAT
(1 answer)
I want to do group_concat in SQL Server
(1 answer)
Closed 2 years ago.
I have a bit of an issue. I have a dataset with around 6000 records stored in a MSSQL 2016 database, where I need to concatenate each column, sorted on row basis and grouped by an ID.
It is no problem to sort the data within the column itself, but the position of the concatenated values is the key in the system that needs the data. So, if I merge four rows with the same ID, each row value must be in the same position throughout the whole resultset row.
To make it even more messy, I must be able to apply some logic to determine which row should be in position one.
If I use the the sort functionality in GROUP_CONCAT_DS (I'm using a semicolon delimiter), it sorts on the column values, which make the index position of each merge inconsistent related to the rows that holds the data.
Example data:
id col1 col2
--------------------
1 foo bar
1 lorem ipsum
2 hi cowboy
Expected output:
id, concat_col1, concat_col2
----------------------------
1 foo;lorem bar;ipsum
2 hi cowboy
Absolutely unacceptable output:
id concat_col1 concat_col2
----------------------------
1 bar;foo lorem;ipsum
2 hi cowboy
In pseudo-SQL, this is a simplified example of what I want to achieve:
SELECT
[id]
,dbo.GROUP_CONCAT_D([col1], ';') AS [concat_col1]
,dbo.GROUP_CONCAT_D([col2], ';') AS [concat_col2]
FROM [hrm].[vEmploymentsPositions]
GROUP BY [id]
ORDER BY [id] asc, [col1] desc, [col2] asc
... But for loical reason the sort doesn't work that way.
How can I approach this issue and make a robust solution that fit our needs?
This question already has answers here:
How to return a incremental group number per group in SQL
(3 answers)
Closed 4 years ago.
I am busy creating a VB.Net Windows Application. I am using a microsoft sql server database with a table called skedulering. I am trying to update a column (i.e Groep) with unique values based on another column (i.e. Kode). This value takes the first three characters of the Kode value and adds a integer to it. I think what I mean is best explained with the following :
All the red bordered rows have the same Groep value. Can anyone please help me to create the sql statement?
Regards
use dense_rank() to generate a running no
UPDATE t
SET Groep = left(Kode, 3) + convert(varchar(10), rn)
FROM
(
SELECT Kode, Groep, rn = dense_rank() over (partition by left(Kode, 3) order by Kode)
FROM yourtable
) t
This question already has an answer here:
How FOR XML PATH('') works when concatenating rows
(1 answer)
Closed 4 years ago.
I have a SQL Server output which returns the following values with one column:
RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
Output :
RepresentID
---------------
111122222
3333344444
5555566666
000000090909
7777788888
9999999999
121212131313
141414151515
need to concatenate all the rows in to one row.
use xml path like below
SELECT STUFF((
SELECT
','+cast(representid as varchar(30))
FROM
table
FOR
XML PATH('')
),1,1,''
) AS comma_seperated_list;
On sql2017 , you can use string_aggregate like below
select string_agg(cast(representid as varchar(40)),',')
from table
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
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