how to concatenate multiple column values into a single column? - asp.net

i have the following table friend
id | first_name | last_name | gender | age | mobile
1 | bobby | roe | male | 21 | 541-5780
how to concatenate multiple column (first_name & last_name) values into a single column to get the following result?
full_name
bobby roe
i have writen the following query but it does not work
declare #full_name varchar(max)
select #full_name = COALESCE(#full_name + ', ', '') + first_name, last_name
from friend
select #full_name

More than one way to achieve this:
SELECT CONCAT(first_name, ' ' ,last_name) AS full_name;
For earlier versions (Where CONCAT is not a built in function):
SELECT first_name + ISNULL(' ' + last_name, '') as Full_Name from [YourTable]
This as well should give you the same result
SELECT COALESCE(first_name, '') + COALESCE(last_name, '') as FullName FROM [YourTable]

Related

misuse of aggregate: group_concat()

I have a problem with sqlite3 in this string
select group_concat(persons.name, ', ') as Актеры
from films_persons join persons on persons.id = films_persons.person_id
where films_persons.film_id = 1 and role = Актеры
films_persons looks like this:
film_id | person_id | role
1 | 1 | "Actors" (or "Актеры" in my case)
persons:
id | name | birth_date
1 | Leonardo | 11.11.1974
I need to group all persons.name in 1 string, and this call must return "Leonardo".
Full name of error is "sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) misuse of aggregate: group_concat()".
In mysql, you need to do:
group_concat(persons.name separator ', ')
instead of:
group_concat(persons.name, ', ')
https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=20172b9b99dfcf7dcdf7ccf00efd49f4
But it appears from your error that you are using sqlite, not mysql, in which case your syntax is correct: https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=691caef008dbade5c1dde83e4e495c7f

SQLITE - combine a couples names (first and last name included)

I have two tables: People and Clients. Here is a basic sample.
'People' columns:
cliend_id|first_name|last_name
------------------------------
1 Dan Black
1 Jenn Black
2 Rob White
2 Lisa Green
'Clients' columns:
client_id|address
1 43 address
2 89 address
In the case where there is two partners (people) living in the same house (which in combination make a client), I am trying to combine the two to output something like "Black, Jenn & Dan"
I have the following
SELECT
clients.address_google Address,
group_concat(people.last_name || ", " || people.first_name, " & ") Name
FROM clients
INNER JOIN people ON people.client_id = clients.client_id
Which outputs a single line - Address and Name. My question is specific to the Name part.
If the last name is the same I would want "Black, Dan & Jenn"
If the last name is not the same, I would want "White, Rob & Green, Lisa"
Is there a way to test if the last name is the same, and if so omit it for the second person?
As a starter: you need to add a group by clause to the query to make it a valid aggregation query.
Then: if there always is 1 or 2 personn at a given address (not more), as shown in your sample data, then an option is a conditional expression:
select
c.address_google address,
case when min(p.last_name) <> max(p.last_name)
max(p.last_name) || ', ' || group_concat(p..first_name, ' & ')
else group_concat(p.last_name || ', ' || p.first_name, ' & ')
end as name
from clients c
inner join people p on p.client_id = c.client_id
group by c.address_google
You can use ROW_NUMBER() window function to get the 1st of the clients that has a last_name same as others:
SELECT
c.address_google Address,
GROUP_CONCAT(CASE WHEN p.rn = 1 THEN p.last_name || ", " ELSE '' END || p.first_name, " & ") Name
FROM clients c
INNER JOIN (
SELECT *, ROW_NUMBER() OVER (PARTITION BY client_id, last_name ORDER BY first_name) rn
FROM people
) p ON p.client_id = c.client_id
GROUP BY c.client_id, c.address_google;
See the demo.
Results:
| Address | Name |
| ---------- | ------------------------ |
| 43 address | Black, Dan & Jenn |
| 89 address | Green, Lisa & White, Rob |

Oracle 11g: Replace part of string using dictionary mapping

Is there any nice trick to change values in string using dictionary mapping? For example I have table1 FIDDLE
+---------------------------+
| ROW1 |
+---------------------------+
| This is an example string |
| This String has typ0s |
+---------------------------+
And some mapping table dict1 FIDDLE:
+-------------------------+
| OLD | NEW |
+-------------------------+
| THIS | THAT |
| IS | ARE |
| EXAMPLE | SOURCE |
| STRING | NUMBER |
+------------+------------+
I need some SELECT statement that will split values in table1.row1 and change words using mapping dictionary dict1 so received values will be ( changing no existing dictionary values to upper is optional):
+---------------------------+
| TRANS_ROW1 |
+---------------------------+
| THAT ARE AN SOURCE NUMBER |
| THAT NUMBER HAS TYP0S |
+---------------------------+
PS. Spliting using REGEXP expression will be so nice..
WITH dict1 AS
(SELECT 'THIS' fr,
'THAT' t
FROM dual
UNION ALL
SELECT 'IS' fr,
'ARE' t
FROM dual
UNION ALL
SELECT 'EXAMPLE' fr,
'SOURCE' t
FROM dual
UNION ALL
SELECT 'STRING' fr,
'NUMBER' t
FROM dual),
table1 AS
(SELECT 'This is an example string' AS str,
1 AS sn
FROM dual
UNION ALL
SELECT 'This String has typ0s' AS str,
2 sn
FROM dual),
src AS
(SELECT regexp_substr(upper(s.str), '[^ ]+', 1, LEVEL) str2,
s.*,
rownum nn
FROM table1 s
CONNECT BY instr(TRIM(' ' FROM str), ' ', 1, LEVEL - 1) > 0
AND PRIOR sn = sn
AND PRIOR dbms_random.value IS NOT NULL),
repl AS
(SELECT nvl2(dict1.t, dict1.t, src.str2) lex,
sn,
nn
FROM src
LEFT JOIN dict1
ON dict1.fr = src.str2)
SELECT listagg(lex, ' ') within GROUP(ORDER BY nn),
sn
FROM repl
GROUP BY sn
It works now as you ask. Enjoy.
EDIT: FIDDLE with solution

Convert sqlserver data to Pivot table [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
ID ---- ACCOUNT ---- SECTOR ---- AMOUNT_CURRENCY1 ------ AMOUNT_CURRENCY2
1 --- account1 ---- sector1 --- 100 ------ 200
2 --- account1 ---- sector2 --- 150 ------ 250
3 --- account2 ---- sector1 --- 250 ------ 300
4 --- account2 ---- sector2 --- 90 ------ 180
I need data to be like this
sector1 ------------ sector2
| amount1 | amount2 | amount1 | amount2
Account1 | 100 | 200
Account2 | 250 | 250
I need to put the result in asp.net gridview to edit
I'm using the following script:
1- to get columns:
DECLARE #ColumnHeaders VARCHAR(MAX)
SELECT #ColumnHeaders =
COALESCE(
#ColumnHeaders + ',[!sector:' + cast(sector_ID as nvarchar)+ ':' + sector_name + ']',
'[!sector:' + cast(sector_ID as nvarchar)+ ':' + sector_name+ ']'
)
FROM vw_Transaction
group by sector_ID, sector_name
2- pivot:
DECLARE #TableSQL NVARCHAR(MAX)
SET #TableSQL = N'
SELECT *
FROM (SELECT trans_id, account_name, sector_id, sector_name,
amount_currency1, amount_currency2, ''!sector:'' + cast(sector_ID as nvarchar)+ '':'' + sector_name as col
FROM dbo.vw_Transaction
WHERE
trans_id=' + CAST(#trans_id as varchar) +'
) AS PivotData
PIVOT (
MAX(amount_currency1)
FOR col IN (
' + #ColumnHeaders + '
)
) AS PivotTable'
EXECUTE(#TableSQL)
the problem, i have 2 fields that need to pivot, amount_currency1 and amount_currency2
Since you are trying to PIVOT on two values, you must first UNPIVOT the AMOUNT_CURRENCY1 and AMOUNT_CURRENCY2 columns:
You will want to add something like this to your query:
select account, value, sector+ '_'+col as col
from
(
select sector,
account,
AMOUNT_CURRENCY1,
AMOUNT_CURRENCY2
from vw_Transaction
) src
unpivot
(
value
for col in (AMOUNT_CURRENCY1, AMOUNT_CURRENCY2)
) unpiv
Then your final query will be similar to this:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT DISTINCT ','
+ quotename(t.sec+'_'+c.name)
from
(
select sector sec
from vw_Transaction
) t
cross apply sys.columns as C
where C.object_id = object_id('vw_Transaction') and
C.name not in ('id', 'account', 'sector')
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT account,' + #cols + ' from
(
select account, value,
sector+ ''_''+col as col
from
(
select sector,
account,
AMOUNT_CURRENCY1,
AMOUNT_CURRENCY2
from vw_Transaction
) src
unpivot
(
value
for col in (AMOUNT_CURRENCY1, AMOUNT_CURRENCY2)
) unpiv
) x
pivot
(
max(value)
for col in (' + #cols + ')
) p '
execute(#query)
See SQL Fiddle with Demo
Result:
| ACCOUNT | SECTOR1_AMOUNT_CURRENCY1 | SECTOR1_AMOUNT_CURRENCY2 | SECTOR2_AMOUNT_CURRENCY1 | SECTOR2_AMOUNT_CURRENCY2 |
------------------------------------------------------------------------------------------------------------------------
| account1 | 100 | 200 | 150 | 250 |
| account2 | 250 | 300 | 90 | 180 |

How Show tablename with resultset in query?

I have a used query as follows:
SELECT LTRIM(RTRIM(ClaimsAdminName)) + ' | ' + LTRIM(RTRIM(ClaimsAdminID)) AS Clientname
FROM tblClaimsAdmin
WHERE (ClaimsAdminName LIKE #Prefix)
UNION
SELECT LTRIM(RTRIM(EmployerFName + ' ' + EmployerLName)) + ' | ' + LTRIM(RTRIM(EmployerID)) AS Clientname
FROM tblEmployer
WHERE (EmployerFName LIKE #Prefix)
The result returned if I enter 's%' is :
s | 8
Sumit Singh | 16
Now is there any way so that i can return the tablename with the result set.Like from which table the value is coming.
I hope I am clear with my query.
Please help!
Thanks
Swaroop Kumar.P
select 'table1' as tablename, otherfields
from table1
union all
select 'table2', otherfields
from table2

Resources