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%';
Related
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.
I have a long list of string values that I would like to count, in one particular column of a table. I know this works for counting all unique values.
SELECT
code_id ,
COUNT(*) AS num
FROM
mydb
GROUP BY
code_id
ORDER BY
code_id
I only have a certain selection of values to count, therefore do now want all. My list is long, but for example, if I just wanted to count the numbers of strings 'ax1', 'c39', and 'x1a' in my code_id column? I've seen examples with multiple lines of code, one for each value which will be huge for counting many values. I'm hoping for something like :
SELECT
code_id ,
COUNT(* = ('ax1, 'c39', 'x1a')) AS num
FROM
mydb
GROUP BY
code_id
ORDER BY
code_id
Desired output would be
code_id count
ax1 39
c39 42
x1a 0
Is there an easy way, rather than a line of code for each value to be counted?
Create a CTE that returns all the string values and a LEFT join to your table to aggregate:
WITH cte AS (SELECT code_id FROM (VALUES ('ax1'), ('c39'), ('x1a')) c(code_id))
SELECT c.code_id,
COUNT(t.code_id) AS num
FROM cte c LEFT JOIN tablename t
ON t.code_id = c.code_id
GROUP BY c.code_id;
See the demo.
I think this should work.
SELECT
code_id ,
sum(1) AS num
FROM Mydb
WHERE code_id in ('ax1', 'c39', 'x1a')
GROUP BY code_id
ORDER BY code_id
I have a few tables that I'm joining and one of them can have an unknown number of matches, up to 6. Each match should be returned as a row value in the initial query. For example:
SELECT a.ID, a.match1, a.match2, a.match3, a.match4, a.match5, a.match6
FROM table1 a, (SELECT ID, match FROM table2 WHERE a.ID = table2.ID) b
WHERE a.ID = b.ID
That's probably not the right syntax but hopefully it shows what I need. So the nested query MAY return 1 match or 5. Each match should be the value for the corresponding column name, ie a.match1 = first match, b.match2 = second match, etc etc.
Please let me know if I need to explain further. I know this isn't the optimal schema to use but it's what I was told to work with.
Something that SQL doesn't like is an unknown number of columns.
As a quick hack, you could aggregate all matches in an array, and then have a query around it transforming the matches into a predefined (large) number of columns.
Like this:
with data as (
select $1 id
from (values(1),(2))
), data2 as (
select $1 id, $2 match
from (values(1, 'a1'),(1, 'a2'),(2, 'b1'),(2, 'b2'),(2, 'b3'))
)
select id, matches[0], matches[1], matches[2], matches[3]
from (
select a.id, array_agg(match) matches
from data a
join data2 b
on a.id=b.id
group by 1
);
Let's start from data:
DECLARE #Avengers TABLE ([Hero] varchar(32), [Preference] varchar(32));
INSERT INTO #Avengers VALUES
('Captain_America','gingers'),('Captain_America','blondes'),
('Captain_America','brunettes'),('Hulk','gingers'),('Hulk','blondes'),
('Hawkeye','gingers'),('Hawkeye','brunettes'),('Iron_Man','blondes'),
('Iron_Man','brunettes'),('Thor','gingers'),('Nick_Fury','blondes');
Now I would like to pass a #Preferences as a list of [Preference] (either comma separated or single column table parameter) without knowing how many parameters I am going to get and based on this to select [Hero] who prefers exactly these #Preferences as provided in parameter (list), by that I mean if I am after 'blondes' and 'gingers' then I am after 'Hulk' only
(NOT 'Captain_America' who prefers 'blondes', 'gingers' and 'brunettes').
I would like to get something like:
SELECT [Hero]
FROM #Avengers
WHERE *IS_ASSIGNED_ONLY_TO_THE_LIST*([Preference]) = #Preference
Well, I think I overcomplicated my code, but it works.
SELECT a.Hero, COUNT(*), MIN(p.N)
FROM #Avengers a
LEFT JOIN ( SELECT *, COUNT(*) OVER() N
FROM #Preferences) p
ON a.Preference = p.Preference
GROUP BY a.Hero
HAVING COUNT(*) = MIN(p.N)
AND COUNT(*) = COUNT(p.Preference)
;
I'm using #Preferences as a table.
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;