How can I concatinate a subquery result field into the parent query? - sql-server

DB: Sql Server 2008.
I have a really (fake) groovy query like this:-
SELECT CarId, NumberPlate
(SELECT Owner
FROM Owners b
WHERE b.CarId = a.CarId) AS Owners
FROM Cars a
ORDER BY NumberPlate
And this is what I'm trying to get...
=> 1 ABC123 John, Jill, Jane
=> 2 XYZ123 Fred
=> 3 SOHOT Jon Skeet, ScottGu
So, i tried using
AS [Text()] ... FOR XML PATH('') but that was inlcuding weird encoded characters (eg. carriage return). ... so i'm not 100% happy with that.
I also tried to see if there's a COALESCE solution, but all my attempts failed.
So - any suggestions?

Answering an old post, just thought it needed an update for newer versions of SQL Server:
For SQL Server 2017 use STRING_AGG(expression, seperator)
GROUP_CONCAT is MySQL.
Prior to SQL 2017, you can also do something like (snipped from our current code base on SQL Server 2016):
SELECT CarId, NumberPlate,
(STUFF(( SELECT ', ' + b.Owner
FROM Owners b
WHERE b.CarId = a.CarId
FOR XML PATH('')
)
,1,2,'')) AS Owners
FROM Cars a
ORDER BY NumberPlate
Links to STRING_AGG
https://database.guide/the-sql-server-equivalent-to-group_concat/
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
Link to STUFF:
https://learn.microsoft.com/en-us/sql/t-sql/functions/stuff-transact-sql?view=sql-server-2017
and finally links to FOR XML:
https://learn.microsoft.com/en-us/sql/relational-databases/xml/for-xml-sql-server?view=sql-server-2017

Try the solution to this question:
How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?
:)

Use GROUP_CONCAT
SELECT CarId, NumberPlate
(SELECT GROUP_CONCAT(Owner)
FROM Owners b
WHERE b.CarId = a.CarId) AS Owners
FROM Cars a
ORDER BY NumberPlate

Related

Achieve the same result from this postgresql query in SQL Server 2017

I have the following query that runs in postgresql-9.6, I need to achieve the same output on a SQL Server DB.
Here is the query, I've replaced all fields from my DB with the string values that would come from them anyway (DB Fields are: "primary_key_fields", "primary_key_values", "table_name", "min_sequence"):
SELECT
UNNEST(STRING_TO_ARRAY(demo.primary_key_fields, ',')) AS primary_key_fields,
UNNEST(STRING_TO_ARRAY(demo.primary_key_values, ',')) AS primary_key_values,
table_name,
min_sequence,
ROW_NUMBER() OVER(partition by demo.primary_key_fields) AS rn
FROM (
SELECT
'Name,surname,age,location,id' AS primary_key_fields,
'Nash,Marley,27,South Africa,121' AS primary_key_values,
'person' AS table_name,
'1' AS min_sequence
UNION ALL
SELECT
'Name,surname,age,location,id' AS primary_key_fields,
'Paul,Scott,25,South America,999' AS primary_key_values,
'person' AS table_name,
'1' AS min_sequence
) demo
I'm expecting the following output:
Highly appreciate the assistance. I'm using SQL Server 2017.
No longer needed. This question can be closed. No solution was found, changed the source system to accomdate what was needed.

PYQT: Use a string list as a parameter for an MS SQL Query

I want a to write an SQL query in Microsoft SQL Server in PyQt of the type:
list=['Engineer', 'Doctor', 'Lawyer']
select * from Occupations where OccupationName in (list)
I have read various posts online but they seem to be for SQL Lite or MySQL databases. How would I do this for Microsoft SQL?
Also, in MS SQL the string values need to be enclosed in double quotes instead of single quotes used in the list items. How would I do this?
this topic should help you! T-SQL split string
btw:
You can write your params in CTE:
;with list as (
select 'Engineer' as [occupation] union
select 'Doctor' union
select 'Lawyer'
)
select * from Occupations where OccupationName in (select occupationfrom from list)
or very simple like this:
select * from Occupations where OccupationName in ('Engineer', 'Doctor', 'Lawyer')

Add header title above the query result in SQL Server

How can I add heading title for each query?
I want to display it as a report with a heading above the query result of the table.
Something like this
HEADING
_____________________________
Alexis M. Smith | PHP 140,500
Johnny K. Black | PHP 50,000
James P. Blonde | PHP 30,000
I am using sql server management 2012 Studio and vb.net 2010
I don't have Business Intelligence to create a report from the sql server to vb.net 2010
You may can't add header in your query result in SQL-Server and show the result in your application..
But if you are asking to show the result in SQL-Server only, then this might help you..
--You have to add one select query before your actual query like this.
select '' as 'Your Heading Here' where 1!=1
select * from your_table
Select b.a AS Heading
FROM
( Select Concat('Mr. Xyz', ' | ', 'PHP' , ' | ' , '50000') AS a
) B
This is the closest I could think.

How to make a morphological search?

Good day.
I have query:
SELECT *
FROM Firm
WHERE name LIKE 'АВТОМОБИЛЬ%'
Tell me please how use morphological search with this query on MsSQL 2008 ?
Declare #find NVarchar(20)='test'
SELECT *
FROM Firm
WHERE name LIKE '%'+#find+'%' OR phone LIKE '%'+#find+'%'
Just run the below Query
SELECT *
FROM Firm
WHERE name LIKE N'АВТОМОБИЛЬ%'
-- ^ Magical Code
SQL Fiddle

pull/capture data from sp_HelpConstraint

Within SQL Server 2005 T-SQL, I would really like to pull these columns:
constraint_type, constraint_name, and constraint_keys
from the output of sp_HelpConstraint. However it returns 3 result sets (2 if you pass in 'nomsg'), so you can't do this to capture it:
CREATE TABLE #Constraints
(
...
)
INSERT INTO #Constraints
(... )
EXECUTE sp_HelpConstraint 'TableName', 'nomsg'
The only ways I can think of doing this are not good ones:
just copy the code I need from sp_HelpConstraint
"fix" sp_HelpConstraint so 'nomsg' removes the last result set too
any ideas?
Hopefully, you're on SQL 2005+
sys.default_constraints etc to allow standard SELECT
Otherwise, you can use Information Schema Views on SQL 2000 +

Resources