Alternative to Group_concat in MS SQL Server [duplicate] - mysql

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
How to make a query with group_concat in sql server [duplicate]
(4 answers)
Closed 3 years ago.
I have a simple query that does group concat very well in SQL workbench.
id hobby
1 dance
1 sing
2 play
2 sing
Expected output:
id hobby
1 dance,sing
2 play,sing
How can I get this output using MS SQL Server?

Related

Retrieve different data from one column in MySQL [duplicate]

This question already has answers here:
Count boolean field values within a single query
(4 answers)
Closed 4 months ago.
Suppose a table have a column with values like:
column
3
1
1
3
4
1
2
I want to retrieve that how many times there is 1 in column and how many times > 1.
Is there anyway to do this within one query?
I want the data to be fetched like this:
one | greater_than_one
3 | 4
Thanks in advance.
Simple:
select sum(col1=1) as one ,
sum(col1>1) as greater_than_one
from test_tbl;
https://dbfiddle.uk/348f5YOK

How to get lag value without using the lag function in MySQL [duplicate]

This question already has answers here:
Simulating lag function on MySQL
(1 answer)
Lead and Lag function in Mysql
(1 answer)
how to do lag operation in mysql
(1 answer)
MySQL lag/lead function?
(1 answer)
Closed 5 years ago.
If I have
id minutes
1 2:40:00
1 3:40:00
1 5:40:00
2 6:40:00
2 7:40:00
2 8:40:00
With the lag function
SELECT id, minutes as earlier_time, lag(minutes) OVER(PARTITION BY id
ORDER BY id, minutes) as later_time FROM table1
I get
id earlier_time
1 2:40:00 3:40:00
1 3:40:00 5:40:00
1 5:40:00 NULL
2 6:40:00 7:40:00
2 7:40:00 8:40:00
2 8:40:00 NULL
Is there a way to do that without the lag function?

using substring with regex mysql [duplicate]

This question already has answers here:
How to extract two consecutive digits from a text field in MySQL?
(7 answers)
How to extract the nth word and count word occurrences in a MySQL string?
(9 answers)
Closed 6 years ago.
I am trying to get year (yyyy) from text/string in the column "title" but for some reason can't get following query to work:
select substring(title,LOCATE(REGEXP '[(][0-9]{4}[)]',title),4) from videos
title column has following values:
abc abc 2001 abc
xyz (2002) xyz
Any assistance will be appreciated.

Order by before group by in subquery working in mysql 5.5 but not in mysql 5.7 [duplicate]

This question already has answers here:
Error related to only_full_group_by when executing a query in MySql
(18 answers)
Closed 6 years ago.
I have a problem when use Order By and Group By in a query string.
There is a table careers that contains data as shown below:
id se_id enrollment_start
-------------------------
1 1 2005-07-01
2 2 2008-10-12
3 2 2006-05-09
4 1 2016-11-10
5 3 2015-02-04
6 3 2010-08-11
I want to get se_id which has highest enrollment_start.
This is a sql statement I used. It works in mysql 5.5 but not in mysql 5.7:
SELECT
tmp.*
FROM
(SELECT
*
FROM
careers
ORDER BY enrollment_start DESC) tmp
GROUP BY tmp.se_id
This is groupwise maximum problem, and there are many topics cover about it. But I don't want the answer for that problem, I want to know why above statement woking in mysql 5.5 but it doesn't work in mysql 5.7 and is there any method to fix it? Thank you.
If you want to get se_id which has highest enrollment_start. use LIMIT:
SELECT *
FROM careers
ORDER BY enrollment_start DESC
LIMIT 1

mysql update field's character [duplicate]

This question already has answers here:
MySQL string replace
(6 answers)
Closed 7 years ago.
I have following record in MySQL database:
id name alias
1 vijay hazare vijay_hazare
2 ravi shastri ravi_shastri
3 rahul dravid rahul_dravid
I want them like this.
id name alias
1 vijay hazare vijay.hazare
2 ravi shastri ravi.shastri
3 rahul dravid rahul.dravid
I have 6000 records in my database and I want to change _ to .(dot) in "alias" field to all table.
You can use REPLACE() function with UPDATE statement:
UPDATE yourtable SET `alias`=REPLACE(`alias`,'_','.');