Get multiple id on multiple value in string with sql? - sql

I have one table like
tbl
---------------------
id users name
---------------------
1 2,3 acc1
2 4 acc2
3 2,4,1 acc3
4 4,1 acc4
In this table I want to get id and name by users
i.e user [2] have which id and name
Suppose I pass user [2] then i get result id is 1 and 3 and name acc1 and acc3.

Try this will work for you
SELECT id,name FROM yourtablename WHERE `users` LIKE '%2%';

You can split those comma separated values using XML functions and then search in the result :
DECLARE #table TABLE(id INT, users VARCHAR(30), name VARCHAR(30))
INSERT INTO #table VALUES
(1,'2,3','acc1'),
(2,'4','acc2'),
(3,'2,4,1','acc3'),
(4,'4,1','acc4')
SELECT t.id,
t.name,
( c1.value('.', 'varchar(100)') )
FROM (SELECT id,
name,
CAST('<N>' + REPLACE(users, ',', '</N><N>') + '</N>' AS XML)
FROM #table) t(id, name, c)
CROSS APPLY c.nodes('/N') AS t1(c1)
WHERE ( c1.value('.', 'varchar(100)') ) = '2'

Use the LIKE function
SELECT id, name
FROM yourtable
WHERE (user = '2' OR user LIKE '2,%' OR user LIKE '%,2' OR user LIKE '%,2,%')

You shouldn't store delimited values in a database, but here's a solution for you, that will normalise the data:
;WITH CTE AS (
SELECT T1.[id], T2.my_Splits AS [user], T1.[name]
FROM (
SELECT *,CAST('<X>'+replace(T.users,',','</X><X>')+'</X>' as XML) as my_Xml
FROM Table1 T
) T1
CROSS APPLY (
SELECT my_Data.D.value('.','varchar(50)') as my_Splits
FROM T1.my_Xml.nodes('X') as my_Data(D)) T2)
SELECT *
FROM CTE
WHERE [user] = 2
And a working fiddle: http://sqlfiddle.com/#!6/dcec6/1

Related

How to get last record from Master-Details tables

I have a table that has 3 columns.
create table myTable
(
ID int Primary key,
Detail_ID int references myTable(ID) null, -- reference to self
Master_Value varchar(50) -- references to master table
)
this table has the follow records:
insert into myTable select 100,null,'aaaa'
insert into myTable select 101,100,'aaaa'
insert into myTable select 102,101,'aaaa'
insert into myTable select 103,102,'aaaa' ---> last record
insert into myTable select 200,null,'bbbb'
insert into myTable select 201,200,'bbbb'
insert into myTable select 202,201,'bbbb' ---> last record
the records is saved In the form of relational with ID and Detail_ID columns.
I need to select the last record each Master_Value column. follow output:
lastRecordID Master_Value Path
202 bbbb 200=>201=>202
103 aaaa 100=>101=>102=>103
tips:
The records are not listed in order in the table.
I can not use the max(ID) keyword. beacuse data is not sorted.(may
be the id column updated manually.)
attempts:
I was able to Prepare follow query and is working well:
with Q as
(
select ID ,Detail_ID, Master_Value , 1 RowOrder, CAST(id as varchar(max)) [Path] from myTable where Detail_ID is null
union all
select R.id,R.Detail_ID , r.Master_Value , (q.RowOrder + 1) RowOrder , (q.[Path]+'=>'+CAST(r.id as varchar(max))) [Path] from myTable R inner join Q ON Q.ID=R.Detail_ID --where r.Dom_ID_RowType=1010
)
select * into #q from Q
select Master_Value, MAX(RowOrder) lastRecord into #temp from #Q group by Master_Value
select
q.ID lastRecordID,
q.Master_Value,
q.[Path]
from #temp t
join #q q on q.RowOrder = t.lastRecord
where
q.Master_Value = t.Master_Value
but I need to simple way (one select) and optimal method.
Can anyone help me?
One method uses a correlated subquery to get the last value (which is how I interpreted your question):
select t.*
from mytable t
where not exists (select 1
from mytable t2
where t2.master_value = t.master_value and
t2.id = t.detail_id
);
This returns rows that are not referred to by another row.
For the path, you need a recursive CTE:
with cte as (
select master_value, id as first_id, id as child_id, convert(varchar(max), id) as path, 1 as lev
from mytable t
where detail_id is null
union all
select cte.master_value, cte.first_id, t.id, concat(path, '->', t.id), lev + 1
from cte join
mytable t
on t.detail_id = cte.child_id and t.master_value = cte.master_value
)
select cte.*
from (select cte.*, max(lev) over (partition by master_value) as max_lev
from cte
) cte
where max_lev = lev
Here is a db<>fiddle.

SQL : How to find the count of an particular category values from an column with string values

I have a SQL Table called "category" looks like this
id | category
--------------
1 | 3,2
2 | 1
3 | 4,3,2
4 | 2,1
5 | 1,4
6 | 2,3,4
There are multiple category id's in the column "category", I need to find the count of an particular category values.
Current method I am using is:
select count(distinct(Category)) AS coldatacount from table_name
It gives the count of all the distinct values WHERE as I need to get
the count of all the particular category_id's separately.
if you are trying to get the Category Ids in comma delimited, you can use the string_split function to get distinct category_id
with cte as (
select 1 as id, '3,2' as category union all
select 2, '1' union all
select 3, '4,3,2' union all
select 4, '2,1' union all
select 5, '1,4' union all
select 6, '2,3,4'
)select count(distinct(value)) as category from cte
cross apply string_split(cte.category, ',');
I assumed that #neeraj04 may be looking for count of all Id in the category, continuing with #METAL code:
CREATE TABLE YourTable
(
Id INT IDENTITY,
[Category] VARCHAR(50)
);
INSERT YourTable VALUES ('3,2'), ('1'), ('4,3,2'), ('2,1'), ('1,4'), ('2,3,4');
SELECT CAST(value AS INT) AS category -- Value is string ouptut
, COUNT([value]) AS IdCount
FROM YourTable yt
CROSS APPLY string_split(yt.Category, ',')
GROUP BY [value]
ORDER BY category;
This is a horrible data model. You should not be storing multiple values in a string. You should not be storing numbers as strings.
Sometimes we are stuck with other people's really, really bad decisions. One approach is to split the string and count:
select t.*, cnt
from t cross apply
(select count(*) as cnt
from string_split(t.category) s
) s;
The other is to count commas:
select t.*,
(1 + len(t.category) - len(replace(t.category, ',', '')) as num_elements
Select Count(Value) from (Select Value from Table_Name a Cross Apply
string_split(a.Category, ','))ab Where Value=1

Select first occurrence of list item in table

I have a list like this example:
abc, efg, rty
and a table with following data:
1 abcd
2 efgh
3 abcd
4 rtyu
5 efgh
now I want to find the first-row which start with list item in the table. my expected result is:
1 abcd
2 efgh
4 rtyu
This is a complete script to do the job
Declare #v_List Table
(
Text nvarchar(100)
)
Declare #v_Data Table
(
Number int,
Text nvarchar(100)
)
Insert Into #v_List values(N'abc')
Insert Into #v_List values(N'efg')
Insert Into #v_List values(N'rty')
Insert Into #v_Data values(1, N'abcd')
Insert Into #v_Data values(2, N'efgh')
Insert Into #v_Data values(3, N'abcd')
Insert Into #v_Data values(4, N'rtyu')
Insert Into #v_Data values(5, N'efgh')
;with CTE as
(
Select D.Number,
D.Text,
ROW_NUMBER() OVER (PARTITION BY L.Text Order By D.Number) as Row_No
From #v_Data D
Join #v_List L
On D.Text like L.Text + '%'
)
Select CTE.Number,
CTE.Text
From CTE
Where CTE.Row_No = 1
select * from TableName
where Id in
(
select min(Id) from
(
select Id,
case
when Val like 'abc%' then 1
when Val like 'efg%' then 2
when Val like 'rty%' then 3
else 0 end temp
from TableName
)t where temp > 0
group by temp
)
You can use a windowed ROW_NUMBER to generate a sequential number by each different value, then just display the first one only.
;WITH RowNumbersByValue AS
(
SELECT
T.ID,
T.Value,
RowNumber = ROW_NUMBER() OVER (PARTITION BY T.Value ORDER BY T.ID)
FROM
YourTable AS T
)
SELECT
R.ID,
R.Value
FROM
RowNumbersByValue AS R
WHERE
R.Value IN ('abcd', 'efgh', 'rtyu') AND
R.RowNumber = 1
For SQL Server I prefer this version, which does not require a subquery:
SELECT TOP 1 WITH TIES ID, Value
FROM yourTable
WHERE Value LIKE 'abc%' OR Value LIKE 'efg%' OR Value LIKE 'rty%'
ORDER BY ROW_NUMBER() OVER (PARTITION BY Value ORDER BY ID);
SELECT * INTO #temp FROM (VALUES
(1 ,'abcd'),
(2 ,'efgh'),
(3 ,'abcd'),
(4 ,'rtyu'),
(5 ,'efgh'))a([id], [name])
You can use min and group by function
SELECT MIN(id), name FROM #temp GROUP BY name
You may use this, there are so many ways to achieve this, use whichever suits you better.
using subquery
select id, col from
(select Row_number() over (partition by col order by id) as slno, id, col from yourtable)
as tb where tb.slno=1
using cte
; with cte as (
select row_number() over (partition by col order by id) as Slno, id, col from table)
select id, col from cte where slno=1
using min
select Min(id) , col from table group by col
Note:-
In the end of any above mentioned query you may apply your where clause to filter your records as needed.

Creating tables on-the-fly

It is often convenient in PosgreSQL to create "tables" on the fly so to refer to them, e.g.
with
selected_ids as (
select 1 as id
)
select *
from someTable
where id = (select id from selected_ids)
Is it impossible to provide multiple values as id this way? I found this answer that suggests using values for similar problem, but I have problem with translating it to the example below.
I would like to write subqueries such as
select 1 as id
union
select 2 as id
union
select 7 as id
or
select 1 as id, 'dog' as animal
union
select 7 as id, 'cat' as animal
in more condensed way, without repeating myself.
You can use arguments in the query alias:
with selected_ids(id) as (
values (1), (3), (5)
)
select *
from someTable
where id = any (select id from selected_ids)
You can also use join instead of a subquery, example:
create table some_table (id int, str text);
insert into some_table values
(1, 'alfa'),
(2, 'beta'),
(3, 'gamma');
with selected_ids(id) as (
values (1), (2)
)
select *
from some_table
join selected_ids
using(id);
id | str
----+------
1 | alfa
2 | beta
(2 rows)
You can pass id and animal field in WITH like this
with selected_ids(id,animal) as (
values (1,'dog'), (2,'cat'), (3,'elephant'),(4,'rat')--,..,.. etc
)
select *
from someTable
where id = any (select id from selected_ids)
You should use union and IN statement like this:
with
selected_ids as (
select 1 as id
union
select 2 as id
union
select 3 as id
....
)
select *
from someTable
where id in (select id from selected_ids)
after reviewing wingedpanther's idea and looking for it, you can use his idea IF those id's are continuously like this:
with
selected_ids as (
SELECT * FROM generate_series(Start,End) --(1,10) for example
)
select *
from someTable
where id in (select id from selected_ids)
If they are not continuously , the only way you can do that is by storing those ID's in a different table(maybe you have it already and if not insert it)
And then:
select *
from someTable
where id in (select id from OtherTable)

How to display all but first table row?

Is it possible to display all but first row from a table in sql server 2005?
I have this data:
---------------------------------
| ID | Name |
---------------------------------
| 1 | John Smith |
| 2 | John Doe |
| 3 | John Thatcher |
---------------------------------
In my query I need to be able to get 'John Doe' and 'John Thatcher'. I Don't need 'ID' column to be displayed, so I can't use ROW_NUMBER here like follows:
select Name from Customers where ROW_NUMBER() over (order by Id)>1
Please advice.
Thank you.
UPDATE:
Clarification: I would like my query to return only Name column but I can't use table expressions, because I'm using the query as part of string concatenation:
select stuff((select ', '+pfn.FullName from PlaintiffsFullNameView pfn where pfn.SuitId=s.Id for xml path('')),1,1,'') as "CoPlaintiffs"
Now I need to transform this query to return all but first plaintiff in a concatenated manner.
UPDATE 2:
Sorry for messed up explanation, let me try it anew:
I have a suits table and a plaintiffs table. (one to many)
I have a requirement to display each suit with all coplaintiffs concatenated.
"Coplaintiff" is any but first suit plaintiff. I can concatenate all plaintiffs and display them along with corresponding suit data (all in one row), but I can't to figure out how to concatenate all coplaintiffs and display them as string in a row column.
SELECT Name
FROM (
SELECT Name, ROW_NUMBER() OVER (ORDER BY id) AS rn
FROM Customers
) q
WHERE rn > 1
ORDER BY
id
Update:
From your explanation:
SELECT Suit.*,
FirstPlantiff.*,
(
SELECT cp.Name AS [text()]
FROM Plantiff cp
WHERE cp.id <> FirstPlantiff.id
AND cp.SuitID = Suid.ID
ORDER BY
cp.id
FOR XML PATH('')
) AS Coplantiffs
FROM Suit
CROSS APPLY
(
SELECT TOP 1 *
FROM Plantiff p
WHERE p.SuitID = Suit.ID
ORDER BY
p.id
) FirstPlantiff
SELECT Name
FROM Customers
WHERE ID <> (SELECT TOP 1 ID
FROM Customers
ORDER BY ID)
Or since the Id never changes you could just do where ID <> 1
Your query with ROW_NUMBER gives an error, because you can't use ROW_NUMBER in the WHERE clause. So you'd need another subquery:
select stuff((
select ',' + FullName
from (
select pfn.FullName, row_number() over (order by pfn.id) as rn
from #suits s
inner join #plaintiffs pfn on s.id = pfn.SuitId
) sub
where rn <> 1
for xml path('')
), 1, 1, '') subsub
Alternatively, you could select the id of the first row in a subquery:
select stuff((
select ',' + pfn.FullName
from #suits s
inner join #plaintiffs pfn on s.id = pfn.SuitId
where s.id = 1
and pfn.id not in (
select min(id) from #plaintiffs where SuitId = s.id)
for xml path('')
), 1, 1, '') sub
Here's the code segment to generate test data:
declare #suits table (id int identity, CaseName varchar(max))
insert into #suits (CaseName) values ('The People v.s. Donald Duck')
declare #plaintiffs table (id int identity,
SuitId int, FullName varchar(max))
insert into #plaintiffs (SuitId,Fullname)
select 1, 'John Smith'
union all select 1, 'John Doe'
union all select 1, 'John Thatcher'
Your query should work, there's no need for Id to be returned for it to be used in the WHERE condition.
Also, maybe this page can help.
SELECT * FROM Customers
EXCEPT SELECT TOP 1 * FROM Customers
Try these
Solution 1:
select name
from #tbl
where id <> 1
Solution 2:
select top(select count(name) -1 from #tbl) name
from #tbl
order by id desc