Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
It is a simple test to do in SQL, I cant find any solution to do this.

I have a below table name summary, user

VB
+----+--------------------------------------------------------------------------------------------+
| id |                                          summary                                           |
+----+--------------------------------------------------------------------------------------------+
|  1 | asdffgggggg Anand   * edkkofffmfmmfmfm Bala          sdkdodkekeke Chandra dkkdkd "vinoth"* |
|  2 | asdffgggggg Dinesh  * edkkofffmfmmfmfm Frankin       sdkdodkekeke Elisia  dkkdkd  Ganesh.  |
|  3 | asdffgggggg Hansika  edkkofffmfmmfmfm [A.Ishwariya]* sdkdodkekeke Jack    dkkdkd "Lalitha" |
+----+--------------------------------------------------------------------------------------------+

+----+-------------+
| id |    name     |
+----+-------------+
|  1 | A.Ishwariya |
|  2 | Anand       |
|  3 | Bala        |
|  4 | Chandra     |
|  5 | Dinesh      |
|  6 | Elisia      |
|  7 | Frankin     |
|  8 | Ganesh      |
|  9 | Hansika     |
| 10 | Jack        |
| 11 | Lalitha     |
| 12 | Vinoth      |
+----+-------------+



I want to get all the names from the summary column ends with *

Output 1:

VB
╔════╦═════════════╗
║ id ║    name     ║
╠════╬═════════════╣
║  1 ║ Anand       ║
║  1 ║ Vinoth      ║
║  2 ║ Dinesh      ║
║  3 ║ A.Ishwariya ║
╚════╩═════════════╝


I want to get all the names from the summary column ends without *

Output 2:

VB
╔════╦═════════╗
║ id ║  name   ║
╠════╬═════════╣
║  1 ║ Bala    ║
║  1 ║ Chandra ║
║  2 ║ Frankin ║
║  2 ║ Elisia  ║
║  2 ║ Ganesh  ║
║  3 ║ Hansika ║
║  3 ║ Jack    ║
║  3 ║ Lalitha ║
╚════╩═════════╝


Any help will be much appreciated.
Posted
Comments
jaket-cp 17-Nov-15 10:51am    
From what I can see from your sample data, your main issue is initially to separate the summary column into 4 bits of information.
If there is a consistent way to define how to separate the data, then it should be quite straight forward to proceed to the next step.
But it does not appear as though these bits of information can be separated with any definable rules. Unless you take into consideration of the name, but then that will get very messy.

So the question is:
Can summary be split with defined rules?
Ragul M 18-Nov-15 5:50am    
You are saying it's not possible in SQL to accomplish !!
jaket-cp 18-Nov-15 6:03am    
It can be performed in TSQL (using loops and the like) but, I would not say it can be done in one sql statement.

From looking at the summary records, it will be not be straight forward.

Inspecting the first record, I assume they are split like so:
asdffgggggg Anand *
edkkofffmfmmfmfm Bala
sdkdodkekeke Chandra
dkkdkd "vinoth"*

As I can see, there are no easily definable rules to split the mini-records that do not have the * after the name.

If the real data is not like the sample data supplied and it has a split rule that can be used to get the summary separated data. Then it may be possible to get the result within one sql query.

Same question:
Can summary be split with defined rules?
Ragul M 19-Nov-15 4:13am    
Yes !! Summary be split with defined rules..
FAQ:
1) * will be found next to name only.
2) tab,\n,space '][' might found next to name.
3) No spaces in user table name column.
jaket-cp 19-Nov-15 4:23am    
How about situation when * does not exist for the name?
This is the one which I can see not easily definable.

Also the characters before and/or after name are not consistent (include . and " from your sample data).

Provided that your data is fixed, so the name is always in the same position it's pretty simple: SQL has a SUBSTRING function[^] and you can use it in a select:
SQL
SELECT SUBSTRING(MyColumn, 10, 15) FROM MyTable WHERE MyColumn LIKE '%*'

But...in your case, I think you would be a lot, lot better off changing your DB desgin, and splitting out the summary info into a separate table of "SummaryID", "prefix text", "Name" and using a join to generate the selects you want. Trying to do substring extraction of viable data in SQL is not simple, and is very, very likely to fail under a lot of conditions. Doing this in a "proper" programming language with more flexible string handling facilities is a lot simpler, and more reliable in the long run.
 
Share this answer
 
v2
Comments
Ragul M 17-Nov-15 6:24am    
First i need to thank you for your reply !
I want to know weather it's possible to do in sql or not ?
Try with below queries:

Query to Fetch records with star
SQL
SELECT usr.id, user.name FROM User usr
INNER JOIN Summary smr
ON usr.id = smr.id
WHERE smr.summary LIKE '%*'

Query to Fetch records without star
SQL
SELECT usr.id, user.name FROM User usr
INNER JOIN Summary smr
ON usr.id = smr.id
WHERE smr.summary LIKE '%[^*]'
 
Share this answer
 
v5
Comments
Ragul M 17-Nov-15 6:28am    
First i need to thank you for your reply !
Sir your query doesn't gave me exact output which i asked.
Kindly check the output again.
SQL
1. Query for get record with *

select id,name from users u inner join summary s
on u.id=s.id
where summary  like '%*'

2. Query for get record without  *

select id,name from users u inner join summary s
on u.id=s.id
where summary  not like '%*'
 
Share this answer
 
v3
Comments
Ragul M 18-Nov-15 5:48am    
No it's not what i need, it's totally different from what i have asked
Here is a way of getting the results you require.

Using like to find the corresponding names in summary.
Then getting the charindex of the name and the star * position after the name.
Using a bit of lead over to figure out if a star exists after the name but not before the next name in the summary.

Here is a cte query of how it can be done:
SQL
with Summary as(
--setup dummy data
	select 1 id,'asdffgggggg Anand   * edkkofffmfmmfmfm Bala          sdkdodkekeke Chandra dkkdkd "vinoth"*' summary
	union all select 2, 'asdffgggggg Dinesh  * edkkofffmfmmfmfm Frankin       sdkdodkekeke Elisia  dkkdkd  Ganesh.'
	union all select 3, 'asdffgggggg Hansika  edkkofffmfmmfmfm [A.Ishwariya]* sdkdodkekeke Jack    dkkdkd "Lalitha"'
), Name as (
	select 1 id,'A.Ishwariya' name
	union all select 2 id,'Anand'
	union all select 3 id,'Bala'
	union all select 4 id,'Chandra'
	union all select 5 id,'Dinesh'
	union all select 6 id,'Elisia'
	union all select 7 id,'Frankin'
	union all select 8 id,'Ganesh'
	union all select 9 id,'Hansika'
	union all select 10 id,'Jack'
	union all select 11 id,'Lalitha'
	union all select 12 id,'Vinoth'
), SummaryName as (
	select
		s.id SummaryId,
		s.summary,
		n.id NameId,
		n.name,
		lead(s.id) over(order by s.id) NextSummaryId,
		len(s.summary) LenSummary
	from Summary s
	inner join Name n
		on	s.summary like '%'+n.name+'%'
), SummaryNameStarPos as (
	select 
		top(1000)
		*,
		charindex(sn.name, sn.summary) NmPos,
		charindex('*', sn.summary, charindex(sn.name, sn.summary)) NextStarPos,
		isnull(lead(charindex(sn.name, sn.summary)) over(order by SummaryId, charindex(sn.name, sn.summary)), 0) NextNmPos
	from SummaryName sn
	--order by
	--	sn.SummaryId,
	--	NmPos
)
select
	*
from SummaryNameStarPos


--name with star
where NextStarPos > NmPos
and (NextStarPos < NextNmPos or (NextSummaryId <> SummaryId and NextStarPos <= LenSummary))

--name without star
where not (NextStarPos > NmPos
	and (NextStarPos < NextNmPos or (NextSummaryId <> SummaryId and NextStarPos <= LenSummary))
)

It works with the sample data you have supplied.
You may need to modify the where clauses a little with the original data, but you should be able to see what is being compared to get the final results.
Ensure the records are ordered correctly, where they should be ordered by summary and name position, otherwise the lead over will not work as expected.

Probably not an efficient way of doing it.
Also take into consideration of advise from solution 1 of splitting out the summary data.

Hope that helps out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900