How to group 3 columns into a single one in SQL - sql

I have a table that has three columns: myColumn, myColumn2 and myColumn3. The three columns can have a different value but they are all the same type.
I need to do a join such that I merge all the items in the three columns and then group by
If I had a single column, it would be done like this:
select distinct (myColumn), COUNT(myColumn) as 'TotalF'
from FormularioCorto
where idEvento = #idEvento and myColumn <> ''
group by myColumn
The question is:
How do I do a TSQL statement that will allow for me to join the three columns and then group by?
Edit: here is the sample data
I have a table that has 3 columns with the same type
columna columnb columnc
a a b
b a b
c c d
I need to run a tsql that will merge the 3 columns and group by, to get a result like
newcolumn count
a 3
b 3
c 2
d 1
Any ideas?
Thanks in advance

It is hard to tell without some sample data and desired output but a guess would be something like:
SELECT MyCol as MyColumn, Count(*) as 'TotalF'
FROM
(
SELECT myColumn MyCol from FormularioCorto
WHERE idEvento = #idEvento AND myColumn <> ''
UNION ALL
SELECT myColumn2 MyCol from FormularioCorto
WHERE idEvento = #idEvento AND myColumn2 <> ''
UNION ALL
SELECT myColumn3 MyCol from FormularioCorto
WHERE idEvento = #idEvento) AND myColumn3 <> ''
) A
GROUP BY MyCol;
Or for just the example data and result:
SELECT NewColumn, Count(*) as Count FROM
(
SELECT columna as NewColumn FROM SomeTable
UNION ALL
SELECT columnb as NewColumn FROM SomeTable
UNION ALL
SELECT columnc as NewColumn FROM SomeTable
) A
Group by NewColumn

I think you mean concatenate rather than JOIN. JOIN has a very specific meaning with SQL. Use the + operator to concatenate string fields.

CONCAT(`myColumn`, `myColumn2`, `myColumn3`) as joinedField
The CONCAT() function concatenates fields and strings.
Info:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
Note: You should reconsider your structure though, as this will prove to be painfully slow as you add more records.

select cast(myColumn as nvarchar) + CAST(COUNT(myColumn)as nvarchar) as 'TotalF'
from FormularioCorto
where idEvento = #idEvento and myColumn <> ''
group by myColumn

If I am understanding your question right, then you simply want 2 columns (one identifying the id you are counting and one giving the count for the identifier. If this is correct, simply use your existing query for each column and union the results.
For example;
SELECT myColumn AS Id, COUNT(myColumn) AS IdCount
FROM FormularioCorto
where idEvento = #idEvento and myColumn <> ''
GROUP BY myColumn
UNION
SELECT myColumn2 AS Id, COUNT(myColumn2) AS IdCount
FROM FormularioCorto
where idEvento = #idEvento and myColumn2 <> ''
GROUP BY myColumn2
etc etc.

Try this:
SELECT myColumn1 + myColumn2 + myColumn3 AS AllThreeFields
FROM FormularioCorto
WHERE 1=1
GROUP BY myColumn1 + myColumn2 + myColumn3
It won't be fast but it should work. If you need to delimit the data in the columns with other characters you can try this:
SELECT myColumn1 + ' ' + myColumn2 + ' ' + myColumn3 AS AllThreeFields
FROM FormularioCorto
WHERE 1=1
GROUP BY myColumn1 + myColumn2 + myColumn3
If the column types are not the same (you said they were), you will need to cast them to a similar type.

Related

How to avoid duplicates in the STRING_AGG function SQL Server

I was testing a query in SQL in which I need to concatenate values ​​in the form of a comma-separated list, and it works, I just have the problem of duplicate values.
This is the query:
SELECT t0.id_marcas AS CodMarca,
t0.nombremarcas AS NombreMarca,
t0.imagenmarcas,
(SELECT String_agg((t2.name), ', ')
FROM exlcartu_devcit.store_to_cuisine t1
INNER JOIN exlcartu_devcit.cuisine t2
ON t1.cuisine_id = t2.cuisine_id
WHERE store_id = (SELECT TOP 1 store_id
FROM exlcartu_devcit.store
WHERE id_marcas = t0.id_marcas
AND status = 1)) AS Descripcion,
t0.logo,
t0.imagen,
(SELECT TOP 1 preparing_time
FROM exlcartu_devcit.store
WHERE id_marcas = t0.id_marcas
AND status = 1) AS Tiempo,
t0.orden,
(SELECT TOP 1 Avg(minimum_amount)
FROM exlcartu_devcit.store_delivery_zone
WHERE id_marcas = t0.id_marcas) AS MontoMinimo
FROM exlcartu_devcit.[marcas] t0
I thought the solution could be just adding a DISTINCT to the query to avoid repeated values ​​in this way ...
(SELECT STRING_AGG(DISTINCT (t2.name), ', ') AS Descripcion
But apparently the STRING_AGG() function does not support it, any idea how to avoid repeated values?
Simplest way is just select from select, like this:
with dups as (select 1 as one union all select 1 as one)
select string_agg(one, ', ') from (select distinct one from dups) q;
vs original
with dups as (select 1 as one union all select 1 as one)
select string_agg(one, ', ') from dups;

Possible to Search Partial Matched Strings from same table?

I have a table and lets say the table has items with the item numbers:
12345
12345_DDM
345653
2345664
45567
45567_DDM
I am having trouble creating a query that will get all of the _DDM and the corresponding item that has the same prefix digits.
So in this case I'd want both 12345 and 12345_DDM etc to be returned
Use like to find rows with _DDM.
Use EXISTS to find rows with numbers also having a _DDM row.
working demo
select *
from tablename t1
where columnname LIKE '%_DDM'
or exists (select 1 from tablename t2
where t1.columnname + '_DDM' = t2.columnname)
Try this query:
--sample data
;with tbl as (
select col from (values ('12345'),('12345_DDM'),('345653'),('2345664'), ('45567'),('45567_DDM')) A(col)
)
--select query
select col from (
select col,
prefix,
max(case when charindex('_DDM', col) > 0 then 1 else 0 end) over (partition by prefix) [prefixGroupWith_DDM]
from (
select col,
case when charindex('_DDM', col) - 1 > 0 then substring(col, 1, charindex('_DDM', col) - 1) else col end [prefix]
from tbl
) a
) a where [prefixGroupWith_DDM] = 1

combining 3 tables where combination of 2 columns is not unique

There are 3 (will be up to 6 in the future) tables with the same columns.
I need to unify them, i.e. union on same columns. In addition to this - rows shall not be unique, based on 2 column combination! There are a couple of examples on the net, but all of them show how to exclude unique column values based on WHERE for one column. In my case there are 2 columns (Col1 and Col2 combination).
Here are the schematics:
and
And here is how I imagined final query (for 3-tables) would look like:
SELECT
*
FROM
(
SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3
)
GROUP BY
Col1, Col2
HAVING
COUNT (*) > 1
What would be a correct way?
P.S. FYI single-column solutions
Multiple NOT distinct
How to select non "unique" rows
How to Select Every Row Where Column Value is NOT Distinct
EDIT:
I have used the code from accepted answer and added additional search criteria:
ON (SOUNDEX(Merged.[name_t1]) = SOUNDEX(Multiples.[name_t1]) OR Merged.[name_t1] LIKE '%' + Multiples.[name_t1] + '%' OR Multiples.[name_t1] LIKE '%' + Merged.[name_t1] + '%')
AND (SOUNDEX(Merged.[name_t2]) = SOUNDEX(Multiples.[name_t2]) OR Merged.[name_t2] LIKE '%' + Multiples.[name_t2] + '%' OR Multiples.[name_t2] LIKE '%' + Merged.[name_t2] + '%')
search col1 and col2:
-by SOUNDEX
-by col1 like (col1 from other table)
-by (col1 from other table) like col1
Here's the basics of a CTE-based approach :
With Merged AS
( -- CTE 1 : All Data in one table
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
)
, Multiples AS
( -- CTE 2 : Group by the fields in common
SELECT Col1, Col2
FROM Merged
GROUP BY Col1, Col2
HAVING Count(*)>1 -- only want Groups of 2 or more
)
SELECT
Merged.*
FROM Merged INNER JOIN Multiples
-- Only return rows from Merged if they're in Multiples
ON Merged.[Col1]=Multiples.[Col1]
AND Merged.[Col2]=Multiples.[Col2]
Something like that works with my own example MS-SQL data, and it looks like SQLite syntax is the same. HTH!

T-SQL Comma delimited value from resultset to in clause in Subquery

I have an issue where in my data I will have a record returned where a column value will look like
-- query
Select col1 from myTable where id = 23
-- result of col1
111, 104, 34, 45
I want to feed these values to an in clause. So far I have tried:
-- Query 2 -- try 1
Select * from mytableTwo
where myfield in (
SELECT col1
from myTable where id = 23)
-- Query 2 -- try 2
Select * from mytableTwo
where myfield in (
SELECT '''' +
Replace(col1, ',', ''',''') + ''''
from myTable where id = 23)
-- query 2 test -- This works and will return data, so I verify here that data exists
Select * from mytableTwo
where myfield in ('111', '104', '34', '45')
Why aren't query 2 try 1 or 2 working?
You don't want an in clause. You want to use like:
select *
from myTableTwo t2
where exists (select 1
from myTable t
where id = 23 and
', '+t.col1+', ' like '%, '+t2.myfield+', %'
);
This uses like for the comparison in the list. It uses a subquery for the value. You could also phrase this as a join by doing:
select t2.*
from myTableTwo t2 join
myTable t
on t.id = 23 and
', '+t.col1+', ' like '%, '+t2.myfield+', %';
However, this could multiply the number of rows in the output if there is more than one row with id = 23 in myTable.
If you observe closely, Query 2 -- try 1 & Query 2 -- try 2 are considered as single value.
like this :
WHERE myfield in ('111, 104, 34, 45')
which is not same as :
WHERE myfield in ('111', '104', '34', '45')
So, If you intend to filter myTable rows from MyTableTwo, you need to extract the values of fields column data to a table variable/table valued function and filter the data.
I have created a table valued function which takes comma seperated string and returns a table value.
you can refer here T-SQL : Comma separated values to table
Final code to filter the data :
DECLARE #filteredIds VARCHAR(100)
-- Get the filter data
SELECT #filteredIds = col1
FROM myTable WHERE id = 23
-- TODO : Get the script for [dbo].[GetDelimitedStringToTable]
-- from the given link and execute before this
SELECT *
FROM mytableTwo T
CROSS APPLY [dbo].[GetDelimitedStringToTable] ( #filteredIds, ',') F
WHERE T.myfield = F.Value
Please let me know If this helps you!
I suppose col is a character type, whose result would be like like '111, 104, 34, 45'. If this is your situation, it's not the best of the world (denormalized database), but you can still relate these tables by using character operators like LIKE or CHARINDEX. The only gotcha is to convert the numeric column to character -- the default conversion between character and numeric is numeric and it will cause a conversion error.
Since #Gordon, responded using LIKE, I present a solution using CHARINDEX:
SELECT *
FROM mytableTwo tb2
WHERE EXISTS (
SELECT 'x'
FROM myTable tb1
WHERE tb1.id = 23
AND CHARINDEX(CONVERT(VARCHAR(20), tb2.myfield), tb1.col1) > 0
)

SQL Server dynamic column list in contains function

I have a database table in SQL Server 2008 with 5 nvarchar(max) columns. We're using the CONTAINS function to look for text in these columns.
We can look in all five columns using this kind of query:
SELECT *
FROM SomeTable ST
WHERE CONTAINS( (ST.ColumnA, ST.ColumnB, ST.ColumnC, ST.ColumnD, ST.ColumnE) , '"Strawberry"')
Now we want to search for text for one or more of these columns dynamically. For example, only look in ColumnB and ColumnE. I tried using a CASE statement, but I couldn't.
The only solution I can think of is dynamic SQL, but I'd prefer to avoid this. (The full query is very complicated.) Is there a way to do this without using dynamic SQL?
The only way I can think of to avoid using dynamic SQL involves using a temp table and if statements and stored proc. Have a stored proc with parameters for each of the columns that include the text to search that columns for. Create a temp table or table variable to store interim results.
Have five different if statements one for each possible column. In each if insert to the temp table if the variable is not null. something like:
IF #ColA is not null
BEGIN
INSERT INTO #temp
SELECT * FROM SomeTable ST
WHERE CONTAINS( (ST.ColumnA) , #ColA)
END
At the end select from the temp table to show your result.
This only works well though if you don't have very many columns and it would be unlikely that more will be added. Frankly, the fact that you have multiple columns you need to do full text search for the same search string indicates to me that you may have a basic problem with the database design.
I just can think in a solution like this, but this doesn't use full text Search... let me know if it is useful for you.
SELECT *
FROM SomeTable ST
WHERE 1=1
AND ((#colA = '') OR ST.ColumnA LIKE #colA)
AND ((#colB = '') OR ST.ColumnB LIKE #colB)
AND ((#colC = '') OR ST.ColumnC LIKE #colC)
AND ((#colD = '') OR ST.ColumnD LIKE #colD)
AND ((#colE = '') OR ST.ColumnE LIKE #colE)
The search_on parameters would be bit (0, 1) so, when set on 1, the search on that column would be activated.
You could UNION the 5 individual cases, then PIVOT and concatenate. I'm not sure it's any better than dynamic SQL.
You would end up with something like:
SET #FindKey = '%B%E%' -- This is your search which field criteria
WITH RESULTS1 AS (
SELECT PK, 'A' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%A%' AND CONTAINS(ST.ColumnA, '"Strawberry"')
UNION
SELECT PK, 'B' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%B%' AND CONTAINS(ST.ColumnB, '"Strawberry"')
UNION
SELECT PK, 'C' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%C%' AND CONTAINS(ST.ColumnC, '"Strawberry"')
UNION
SELECT PK, 'D' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%D%' AND CONTAINS(ST.ColumnD, '"Strawberry"')
UNION
SELECT PK, 'E' AS Col
FROM SomeTable ST
WHERE #FindKey LIKE '%E%' AND CONTAINS(ST.ColumnE, '"Strawberry"')
)
,RESULTS2 AS (
SELECT PK, ISNULL([A], '') + ISNULL([B], '') + ISNULL([C], '') + ISNULL([D], '') + ISNULL([E], '') AS FoundKey
FROM RESULTS PIVOT ( MIN(Col) FOR Col IN ([A], [B], [C], [D], [E]) ) AS pvt
)
SELECT *
FROM SomeTable
INNER JOIN RESULTS2
ON RESULTS2.PK = SomeTable.PK
WHERE RESULTS2.FoundKey LIKE #FindKey