string-aggregation

How to sort the result from string_agg()

瘦欲@ 提交于 2019-12-03 04:39:18
问题 I have a table: CREATE TABLE tblproducts ( productid integer, product character varying(20) ) With the rows: INSERT INTO tblproducts(productid, product) VALUES (1, 'CANDID POWDER 50 GM'); INSERT INTO tblproducts(productid, product) VALUES (2, 'SINAREST P SYP 100 ML'); INSERT INTO tblproducts(productid, product) VALUES (3, 'ESOZ D 20 MG CAP'); INSERT INTO tblproducts(productid, product) VALUES (4, 'HHDERM CREAM 10 GM'); INSERT INTO tblproducts(productid, product) VALUES (5, 'CREAM 15 GM');

Get Count of different values in comma separated row in mysql

拈花ヽ惹草 提交于 2019-12-02 14:28:06
问题 A table Jobs which have 2 column JobId, City when we save job a job location may be multiple city like below ----------------------------- JobId City ------------------------------- 1 New York 2 New York , Ohio , Virginia 3 New York , Virginia how i count jobid in perticular city like i want count of jobid in New York city i want result like New York 3 Ohio 1 Virginia 2 回答1: Your database is poorly designed and you are going to have a lot of trouble down the line. Using the current structure

Get Count of different values in comma separated row in mysql

老子叫甜甜 提交于 2019-12-02 10:27:53
A table Jobs which have 2 column JobId, City when we save job a job location may be multiple city like below ----------------------------- JobId City ------------------------------- 1 New York 2 New York , Ohio , Virginia 3 New York , Virginia how i count jobid in perticular city like i want count of jobid in New York city i want result like New York 3 Ohio 1 Virginia 2 Your database is poorly designed and you are going to have a lot of trouble down the line. Using the current structure you can get the count using the find_in_set function but that you should avoid . Your table is as create

Join two tables with a column with multiple entries for the other table

你。 提交于 2019-12-02 07:16:16
问题 I have the following problem. I want to join two tables. The first table has entries like the following: T1 PK Info 1 one 2 two 3 three The second table is build like this: T2 PK FKT1 1 1,3 2 1,2,3 3 2 My Result should show the following PK2 FKT1 InfoT1 1 1,3 One,Three 2 1,2,3 One,two,Three 3 2 Two I just cant get an idea how to solve this. Is this possible only using sql selects or is a function needed? kind regards 回答1: It's not that difficult, but - as you were told, you'd rather NOT do

Join two tables with a column with multiple entries for the other table

孤者浪人 提交于 2019-12-02 06:43:46
I have the following problem. I want to join two tables. The first table has entries like the following: T1 PK Info 1 one 2 two 3 three The second table is build like this: T2 PK FKT1 1 1,3 2 1,2,3 3 2 My Result should show the following PK2 FKT1 InfoT1 1 1,3 One,Three 2 1,2,3 One,two,Three 3 2 Two I just cant get an idea how to solve this. Is this possible only using sql selects or is a function needed? kind regards It's not that difficult, but - as you were told, you'd rather NOT do that. SQL> with 2 t1 (pk, info) as 3 (select 1, 'one' from dual union 4 select 2, 'two' from dual union 5

Constructing a string out of several records with 2 columns

半世苍凉 提交于 2019-12-02 04:27:14
问题 I have prepared a simple SQL Fiddle for my question - In a word game written in Pl/pgSQL for PostgreSQL 10.2 player moves are stored in the table: CREATE TABLE words_scores ( mid bigint NOT NULL REFERENCES words_moves ON DELETE CASCADE, gid integer NOT NULL REFERENCES words_games ON DELETE CASCADE, uid integer NOT NULL REFERENCES words_users ON DELETE CASCADE, word text NOT NULL CHECK(word ~ '^[A-Z]{2,}$'), score integer NOT NULL CHECK(score >= 0) ); Here it is filled with some test data:

Group_Concat with join equivalent in SQL SERVER

天大地大妈咪最大 提交于 2019-12-02 03:50:10
I am having trouble in my code. I used to be on MySQL but I've migrated my codes into SQL Server. Now I am having trouble with this GROUP_CONCAT function. I found some 'potential' equivalent but it's just not resulting on what is expected. Here is the query: SELECT GROUP_CONCAT(c.namecheck SEPARATOR '; ') AS GROUPNAME FROM db_name a left JOIN db_employee b ON a.nameId = b.empID left join db_civ c ON b.nameNum = c.civNum I tried some. But as I've said, its does not output the result that what I'm expecting (as I countercheck the query in MySQL) Expected output should be ----------- |GROUPNAME|

How to concatenate multiple rows order by sequence in Oracle10g

妖精的绣舞 提交于 2019-12-01 23:38:45
If I have a data like this: GROUP | SEQUENCE | COMMAND ------------------------------ ONE | 3 | <message2>MESSAGE</message2> ONE | 1 | <?xml version="1.0" encoding="UTF-8"?> ONE | 2 | <message1>MESSAGE</message1> TWO | 2 | <message2>MESSAGE</message2> TWO | 1 | <?xml version="1.0" encoding="UTF-8"?> ........ TWO | 10 | <message9>MESSAGE</message9> How can I concatenate the command to be like this: GROUP | COMMAND ----------------- ONE | <?xml version="1.0" encoding="UTF-8"?>,<message1>MESSAGE</message1>,<message2>MESSAGE</message2> TWO | <?xml version="1.0" encoding="UTF-8"?>,<message1>MESSAGE

Get unique values using STRING_AGG in SQL Server

久未见 提交于 2019-12-01 16:39:43
问题 The following query returns the results shown below: SELECT ProjectID, newID.value FROM [dbo].[Data] WITH(NOLOCK) CROSS APPLY STRING_SPLIT([bID],';') AS newID WHERE newID.value IN ('O95833', 'Q96NY7-2') Results: ProjectID value --------------------- 2 Q96NY7-2 2 O95833 2 O95833 2 Q96NY7-2 2 O95833 2 Q96NY7-2 4 Q96NY7-2 4 Q96NY7-2 Using the newly added STRING_AGG function (in SQL Server 2017) as it is shown in the following query I am able to get the result-set below. SELECT ProjectID, STRING

How to avoid duplicates in the STRING_AGG function

☆樱花仙子☆ 提交于 2019-12-01 05:40:07
问题 My query is below: select u.Id, STRING_AGG(sf.Naziv, ', ') as 'Ustrojstvena jedinica', ISNULL(CONVERT(varchar(200), (STRING_AGG(TRIM(p.Naziv), ', ')), 121), '') as 'Partner', from Ugovor as u left join VezaUgovorPartner as vup on vup.UgovorId = u.Id AND vup.IsDeleted = 'false' left join [TEST_MaticniPodaci2].dbo.Partner as p on p.PartnerID = vup.PartnerId left join [dbo].[VezaUgovorUstrojstvenaJedinica] as vuu on vuu.UgovorId = u.Id left join [TEST_MaticniPodaci2].hcphs.SifZavod as sf on sf