Query to get rows if value contains result of another query

  • ChrisM@Work (3/5/2013)


    This is the simplest way to write it. If it generates dupes, then we'll change one side to a derived table. Can you use something a little more realistic than "entries" and "entries1", or is that what they are really called?

    SELECT *

    FROM entries e

    INNER JOIN entries1 e1

    ON e1.work LIKE e.work+'%'

    AND e1.[date] BETWEEN '01-Jan-2012' and '20-Jan-2013'

    Thanks.. that worked amazing.. I have another problem. I have a query "select * from reminder where alloted_to IN('seh','jyo','nvt',...)" and i have some values for alloted to column like 'seh\jyo','sehvt' etc..Current query does not return those rows contains 'seh\jyo','sehvt'. I know i can use "alloted_to like '%seh%' or alloted_to like '%jyo%' or ..." But if i have many values then its difficult to write 'alloted_to like' condition to all. Is there any other way?

    thanks

  • winmansoft (3/5/2013)


    ChrisM@Work (3/5/2013)


    This is the simplest way to write it. If it generates dupes, then we'll change one side to a derived table. Can you use something a little more realistic than "entries" and "entries1", or is that what they are really called?

    SELECT *

    FROM entries e

    INNER JOIN entries1 e1

    ON e1.work LIKE e.work+'%'

    AND e1.[date] BETWEEN '01-Jan-2012' and '20-Jan-2013'

    Thanks.. that worked amazing.. I have another problem. I have a query "select * from reminder where alloted_to IN('seh','jyo','nvt',...)" and i have some values for alloted to column like 'seh\jyo','sehvt' etc..Current query does not return those rows contains 'seh\jyo','sehvt'. I know i can use "alloted_to like '%seh%' or alloted_to like '%jyo%' or ..." But if i have many values then its difficult to write 'alloted_to like' condition to all. Is there any other way?

    thanks

    Of course! Read this [/url]first, it's a great introduction. Next it would help us a lot if you could prepare the ddl and dml for the two tables.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thank you

    I found that i can do this

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    DECLARE @Elements TABLE

    (

    Value VARCHAR(8000)

    )

    DECLARE @N INT

    SET @Parameter = ','+@Parameter +','

    SET @N = 1

    WHILE @N < LEN(@Parameter)

    BEGIN

    INSERT INTO @Elements

    VALUES (SUBSTRING(@Parameter,@N+1,CHARINDEX(',',@Parameter,@N+1)-@N-1))

    SELECT @N = CHARINDEX(',',@Parameter,@N+1)

    END

    SELECT m.*

    FROM reminder.dbo.Reminder m,@Elements s

    where m.AllotedTo LIKE '%'+s.Value+'%'

    Anything to optimize the query?

  • winmansoft (3/6/2013)


    Thank you

    I found that i can do this

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    DECLARE @Elements TABLE

    (

    Value VARCHAR(8000)

    )

    DECLARE @N INT

    SET @Parameter = ','+@Parameter +','

    SET @N = 1

    WHILE @N < LEN(@Parameter)

    BEGIN

    INSERT INTO @Elements

    VALUES (SUBSTRING(@Parameter,@N+1,CHARINDEX(',',@Parameter,@N+1)-@N-1))

    SELECT @N = CHARINDEX(',',@Parameter,@N+1)

    END

    SELECT m.*

    FROM reminder.dbo.Reminder m,@Elements s

    where m.AllotedTo LIKE '%'+s.Value+'%'

    Anything to optimize the query?

    Yes - do this instead:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    SELECT *

    INTO #Elements

    FROM dbo.DelimitedSplit8K (@Parameter,',')

    SELECT m.*

    FROM reminder.dbo.Reminder m

    INNER JOIN #Elements e ON e.Item = m.AllotedTo

    Ensure that there's an index available to optimise the join. If in doubt, post the execution plan attached as a .sqlplan file.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (3/7/2013)


    yes - do this instead:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    SELECT *

    INTO #Elements

    FROM dbo.DelimitedSplit8K (@Parameter,',')

    SELECT m.*

    FROM reminder.dbo.Reminder m

    INNER JOIN #Elements e ON e.Item = m.AllotedTo

    Ensure that there's an index available to optimise the join. If in doubt, post the execution plan attached as a .sqlplan file.

    What is dbo.DelimitedSplit8K??Its giving error-Invalid object name 'dbo.DelimitedSplit8K'.

  • winmansoft (3/7/2013)


    ChrisM@Work (3/7/2013)


    yes - do this instead:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    SELECT *

    INTO #Elements

    FROM dbo.DelimitedSplit8K (@Parameter,',')

    SELECT m.*

    FROM reminder.dbo.Reminder m

    INNER JOIN #Elements e ON e.Item = m.AllotedTo

    Ensure that there's an index available to optimise the join. If in doubt, post the execution plan attached as a .sqlplan file.

    What is dbo.DelimitedSplit8K??Its giving error-Invalid object name 'dbo.DelimitedSplit8K'.

    It's in the article I suggested you read, about four posts back.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (3/7/2013)


    winmansoft (3/7/2013)


    ChrisM@Work (3/7/2013)


    yes - do this instead:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    SELECT *

    INTO #Elements

    FROM dbo.DelimitedSplit8K (@Parameter,',')

    SELECT m.*

    FROM reminder.dbo.Reminder m

    INNER JOIN #Elements e ON e.Item = m.AllotedTo

    Ensure that there's an index available to optimise the join. If in doubt, post the execution plan attached as a .sqlplan file.

    What is dbo.DelimitedSplit8K??Its giving error-Invalid object name 'dbo.DelimitedSplit8K'.

    It's in the article I suggested you read, about four posts back.

    I Have created DelimitedSplit8K function and query is working..But are you sure i will get better performance from this than other one??

  • winmansoft (3/7/2013)


    ChrisM@Work (3/7/2013)


    winmansoft (3/7/2013)


    ChrisM@Work (3/7/2013)


    yes - do this instead:

    DECLARE @Parameter VARCHAR(8000)

    SET @Parameter = 'seh,jyo,mhf'

    SELECT *

    INTO #Elements

    FROM dbo.DelimitedSplit8K (@Parameter,',')

    SELECT m.*

    FROM reminder.dbo.Reminder m

    INNER JOIN #Elements e ON e.Item = m.AllotedTo

    Ensure that there's an index available to optimise the join. If in doubt, post the execution plan attached as a .sqlplan file.

    What is dbo.DelimitedSplit8K??Its giving error-Invalid object name 'dbo.DelimitedSplit8K'.

    It's in the article I suggested you read, about four posts back.

    I Have created DelimitedSplit8K function and query is working..But are you sure i will get better performance from this than other one??

    Not without access to your data, no.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 8 posts - 16 through 22 (of 22 total)

You must be logged in to reply to this topic. Login to reply