In MSSQL filter rows based on an ID exists in a column as comma separated string - sql

I've Benchmarking table like this
BMID TestID BMTitle ConnectedTestID
---------------------------------------------------
1 5 My BM1 0
2 6 My BM2 5
3 7 My BM3 5,6
4 8 My BM4 10,12,8
5 9 My BM5 0
6 10 My BM6 3,6
7 5 My BM7 8,3,12,9
8 3 My BM8 7,10
9 8 My BM9 0
10 12 My BM10 9
---------------------------------------------
Explaining the table a little
Here the TestID and the connected TestID is playing the roles. If the user wants all the benchmarks for the TestID 3
It should return rows where testID=3 and also if any rows having connectedTestID column having that testID in it among the comma separated values
That means if the user specify the value 3 as the testID, it should return
---------------------------------------------
8 3 My BM8 7,10
7 5 My BM7 8,3,12,9
6 10 My BM6 3,6
--------------------------------------------
Hope its clear how those 3 rows returned. Means First row is because the testID 3 is there. the other two rows because 3 is in their connectedIDs cell

You should fix the data structure. Storing numeric ids in a comma-delimited list is a bad, bad, bad idea:
SQL Server doesn't have the best string manipulation functions.
Storing numberings as character strings is a bad idea.
Having undeclared foreign key relationships is a bad idea.
The resulting queries cannot make use of indexes.
While you are exploring what a junction table is so you can fix the problem with the data structure, you can use a query such as this:
where testid = 3 or
',' + ConnectedTestID + ',' like '%,3,%'

Related

SQL table structure for store value against list of combination

I have a requirement from client where I need to store a value against list of combination.
For example I have following LOBs and against each combination I need to store a value.
Auto
WC
Personal
I purposed multiple solutions he is not satisfied with anyone.
Solution 1: create single table, insert value against all possible combination(string) something like
LOB Value
Auto 1
WC 2
Personal 3
Auto,WC 4
Auto, personal 5
WC, Personal 6
Auto, WC, Personal 7
Solution 2: create lkp_lob, lob_group and lob_group_detail tables. Each group combination represent a group.
Lkp_lob
Lob_key Name
1 Auto
2 WC
3 Person
Lob_group (unique query constrain on lob_group_key and lob_key)
Lob_group_key Lob_key
1 1
2 2
3 3
4 1
4 2
5 1
5 3
6 2
6 3
7 1
7 2
7 3
Lob_group_detail
Lob_group_key Value
1 1
2 2
3 3
4 4
5 5
6 6
7 7
Any suggestion would be highly appreciated.
First of all I did not understood that terms you said.
But from database perspective it is always good to have multiple tables for each module. You will be facing less difficulties when doing CRUD. And will be more faster.

Can I split a dynamic semicolon delimited string into columns using T-SQL?

I have a table that looks like so:
Id SubNumber Values
1 1 1;4;8;3
2 2 8;9;7;10
3 3 41;45;23;0
I will not always only have 4 values and the number of "SubNumbers" can be greater than 3. Is there any way I can query this table to look like this?
Id SubNumber 1 2 3 4
1 1 1 4 8 3
2 2 8 9 7 10
2 3 41 45 23 0
The rows will always have the same number of values delimited by a semicolon but the amount separated by a semicolon can vary. So a table may even have 10 values or 1 or more.
The 2nd table doesn't have to have numbers to represent the values. It can even be blank or the default that is given by SQL when no name is provided.
This is not a duplicate of the example provided because this deals with separating a dynamic number of values into columns.

Sort data by numeric and alphabetical in sqlite3 [duplicate]

This question already has an answer here:
Sqlite query - order results with numbers last
(1 answer)
Closed 7 years ago.
I have table data like this:
ID name
----------
1 30
2 aaa
3 zzz
4 20
5 40
6 10
My result should be sorted data
ID name
5 40
1 30
4 20
6 10
3 zzz
2 aaa
Means numeric and alphabetic should be separate and it should be sorted in DESC or ASC.
If anyone have an idea to perform this operation in sqlite3 help me
SELECT * FROM yourTableName ORDER BY ID ASC;
If you want something more detailed than that, it would help to know what you are currently running or what you want the output to be.

How to fit multiple equal references into table structure?

How to fit multiple equal references into table structure? How could I do that? For example: I have list of classmates:
1 Peter
2 Jack
3 John
4 Mary
5 Birgit
6 Stella
7 Janus
8 Margo
9 Fred
Now I want to define fellowships. In first place, let's limit that every kid may belong to one fellowship. So we could have 3 fellowships:
[Peter, Jack]
[John, Mary, Birgit]
[Stella, Janus, Margo, Fred]
All members are equal, so they all should reference to other members. Is there better ways to define such relations than just to have table of pairs? Like:
1 2
3 4
3 5
4 5
4 3
5 3
5 4
6 7
6 8
6 9
7 6
7 8
7 9
8 6
8 7
8 9
9 6
9 7
9 8
If using table of pairs, is it better to describe relation both way (like above), or is it enough to have link just from one way to another? What are the benefits of both ways?
Table of pairs does not constrain any member into just one fellowsip, but how would it possible?
I was looking for SQL table solution, but maybe there are better tools for handling such data-structures, so I added nosql-tag too. I am looking for right tools for such data, but I am eager to know, how to fit it in SQL tables too.
Yes, there is another way. If you have "fellowships", then you do not have pair-wise relationships. STart with a Fellowships table that has a FellowshipsId.
Then you would have a FellowshipsKids table. This is called a junction table, and it would have one row for each member of each fellowship. It would have rows like this:
FellowshipId KidId
1 1
1 2
2 3
2 4
2 5
. . .
What you have is an m-n relationship between fellowships and kids -- one fellowship can have multiple kids, one kids can be in multiple fellowships. A junction table is the standard way of represent this in a relational database.

SQL comparing two tables with common id but the id in table 2 could being in two different columns

Given the following SQL tables:
Administrators:
id Name rating
1 Jeff 48
2 Albert 55
3 Ken 35
4 France 56
5 Samantha 52
6 Jeff 50
Meetings:
id originatorid Assitantid
1 3 5
2 6 3
3 1 2
4 6 4
I would like to generate a table from Ken's point of view (id=3) therefore his id could be possibly present in two different columns in the meetings' table. (The statement IN does not work since I introduce two different field columns).
Thus the ouput would be:
id originatorid Assitantid
1 3 5
2 6 3
If you really just need to see which column Ken's id is in, you only need an OR. The following will produce your example output exactly.
SELECT * FROM Meetings WHERE originatorid = 3 OR Assistantid = 3;
If you need to take the complex route and list names along with meetings, an OR in your join's ON clause should work here:
SELECT
Administrators.name,
Administrators.id,
Meetings.originatorid,
Meetings.Assistantid
FROM Administrators
JOIN Meetings
ON Administrators.id = Meetings.originatorid
OR Administrators.id = Meetings.Assistantid
Where Administrators.name = 'Ken'