How to select all columns from views that matches with provided string - sql-server

From given views/table I want to select all the columns that matches provided string.
For example, I want to select market, Tech, Spectrum and all the columns that end with A and their corresponding values.
I tried:
select *
from table_name/Views_name
where 'string' IN (select name from sys.columns where object_id = object_id)
but didn't return the required result.

Related

STRING_SPLIT on value from database

I'm trying to setup a SQL query where I want to get data by tags. The problem is that "Tags" column stores data in comma separated format, for example "tag1,tag2".
Code example:
SELECT * FROM News
WHERE (SELECT value FROM STRING_SPLIT(Tags, ','))
IN (SELECT value FROM STRING_SPLIT('tag1,tag2,tag3', ','));
Is it possible?
You can use a subquery where you join the result sets together:
SELECT N.*
FROM News N
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT(N.Tags, ',') s1 JOIN
STRING_SPLIT('tag1,tag2,tag3', ',') s2
ON s1.value = s2.value
);

Returned by a subquery

GOAL
I have a table A with column NAME, I would like that whenever column NAME of table A, is equal to column NAME of table B, is selected in column CODE_CLIENT of table B
QUERY
SELECT
CASE WHEN name = (select name from tb_get_names)
THEN (select code_client from tb_get_names)
ELSE 'null'
END AS Result FROM tb_get_clients
OUTPUT
ERROR: more than one row returned by a subquery used as an expression
Each of the 2 subqueries you use, return all the names or all the code_clients of the table tb_get_names. This is why you get the error.
You need a LEFT JOIN:
SELECT
c.name,
n.code_client AS Result
FROM tb_get_clients AS c LEFT JOIN tb_get_names AS n
ON n.name = c.name

Need help MSSQL query to bring table with two columns into one showing unique value of field 1 and corresponding values in field 2 (in that order)?

Picture of current state and desired state
Need help MSSQL query to bring table with two columns into one showing unique value of field 1 and corresponding values in field 2 (in that order)? See picture attached in link above
Your request sounds quite strange, but if you want to do it, one way is to use rollup + grouping_id like this:
select case when grouping_id(field2) = 1 then field1 else field2 end
from yourtable
group by field1, rollup(field2)
order by field1, grouping_id(field2) desc, field2
Usually you'd use this for subtotals, but using it with case you can add additional rows to the data.
This will return:
x
a
b
c
d
y
e
f
g
z
h
i
Edit: How to list table + column names:
select case when grouping_id(c.name) = 1 then t.name else c.name end
from sys.tables t join sys.columns c on t.object_id = c.object_id
group by t.name, rollup(c.name)
order by t.name,c.name

SELECT row if concatenated value contains text

select dbo.strconcat(r.Name) as Names, r.Category
from Reference r
group by r.Category
For this query I need get categories which have at least one name (r.Name) LIKE any text.
Example: condition (LIKE '%123%')
123,2,4 - correct Names
Is this what you are looking for:
;WITH A
AS
(SELECT dbo.strconcat
(r.Name
) AS Names, r.Category
FROM Reference r
GROUP BY r.Category
)
SELECT *
FROM A
WHERE Names LIKE '%123%';

Need to return all columns from a table when using GROUP BY

I have a table let's say it has four columns
Id, Name, Cell_no, Cat_id.
I need to return all columns whose count of Cat_id is greater than 1.
The group should be done on Cell_no and Name.
What i have done so far..
select Cell_no, COUNT(Cat_id)
from TableName
group by Cell_Number
having COUNT(Cat_id) > 1
But what i need is some thing like this.
select *
from TableName
group by Cell_Number
having COUNT(Cat_id) > 1
Pratik's answer is good but rather than using the IN operator (which only works for single values) you will need to JOIN back to the result set like this
SELECT t.*
FROM tableName t
INNER JOIN
(SELECT Cell_no, Name
FROM TableName
GROUP BY Cell_no , Name
HAVING COUNT(Cat_id) > 1) filter
ON t.Cell_no = filter.Cell_no AND t.Name = filter.Name
you just need to modify your query like below --
select * from tableName where (Cell_no, Name) in (
select Cell_no, Name from TableName
Group by Cell_no , Name
having COUNT(Cat_id) > 1
)
as asked in question you want to group by Cell_no and Name.. if so you need to change your query for group by columns and select part also.. as I have mentioned
This version requires only one pass over the data:
SELECT *
FROM (SELECT a.*
,COUNT(cat_id) OVER (PARTITION BY cell_no)
AS count_cat_id_not_null
FROM TableName a)
WHERE count_cat_id_not_null > 1;

Resources