There is an array #array = select username from usertable
and the parameter is passed to the other query
SELECT
*
FROM transactiontable TT
WHERE TT.name in (SELECT value FROM string_split(#array, ','))
What is the benifit of doing this?
Can't I just do SELECT * from transcationtbale TT WHERE TT.name in #array?
Also, what is SELECT value FROM expression?
Thanks
It seems that 'username' field in 'usertable' is a string like 'user1, user2, user3'. Is it right?
If it is not, then use such query:
SELECT * from transcationtbale TT WHERE TT.name in (select username from usertable)
STRING_SPLIT is used to separate string by specified separator. Please, read this description at MSDN.
Let me show an example:
SELECT * FROM STRING_SPLIT('Adam,Joseph,Jon',',')
But if you select from table, then use just:
SELECT UserName FROM UserTable
There is no benefit between them, it cannot be comparable, it is like compare apples and oranges.
Related
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
);
General except query is like this
(SELECT * FROM name_of_table_one
EXCEPT
SELECT * FROM name_of_table_two);
is there a way to write a query where I pass a list of values and perform except or intersect operation with a specific column of a table and select from that the list I had passed to DB.
Use a VALUES list.
select * from (values (1),(2),(100000001)) as f (aid)
except
select aid from pgbench_accounts
You can achieve that with the IN clause
"except":
SELECT * FROM your_table WHERE your_column NOT IN (list of values)
"intersect":
SELECT * FROM your_table WHERE your_column IN (list of values)
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 field with values for instance:
323.12.444.1
55.1231
4543.432.431
6.1
456.3234.54353.1124.1
321.3.425
2.3.1
5345.43.1
432.5646.2
So for records ended by .1 has to be gathered. What should be the query?
This should be faster than LIKE
SELECT * FROM table WHERE RIGHT(fieldname,2)='.1'
The LIKE with a % at the beginning is something one should avoid if possible...
select * from table where fieldname like '%.1'
I would suggest to use this:
SELECT *
FROM YourTable
WHERE REVERSE(SUBSTRING(REVERSE(col1),1,CHARINDEX('.',REVERSE(col1))-1)) = '1'
You can find any string you need without changing parameters inside query:
;WITH YourTable AS (
SELECT *
FROM (VALUES
('323.12.444.1'),
('55.1231'),
('4543.432.431'),
('6.1'),
('456.3234.54353.1124.1'),
('321.3.425'),
('2.3.1'),
('5345.43.1'),
('432.5646.2')
) as t(col1)
)
SELECT *
FROM YourTable
WHERE REVERSE(SUBSTRING(REVERSE(col1),1,CHARINDEX('.',REVERSE(col1))-1)) = '431'
Output:
4543.432.431
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;