Solved

query syntax (INNER JOIN)

Posted on 2016-07-25
18
61 Views
Last Modified: 2016-07-31
I have a table that saves comma separated values, such as :  1,3,5
This values are the ID of records on a different table, called "Admintags"

When I add a record for a 'casecomment' one of the columns is called 'tags'  and this is where I save those comma separated values.

I have the following query:

 SELECT  
			a.CaseID ,
            a.tags,
            a.Comments
          
    FROM    dbo.Casecomments a

Open in new window


That will return the following values:
caseid  | tags  | comment
30638  |  1, 5  | Client will be traveling to Mexico City from Dec 15th until Jan 2nd.

What I need the query to return is the value of those tags from the 'tags' table. The tags table is very simple, it only has two columns
id = the ids saved above
tag_text = the text value of the tag, this is what I need in return:

30638 |  Travel date, Billing | Client will be traveling to Mexico City from Dec 15th until Jan 2nd.

which are the corresponding values of 1 and 5 in the "Admintags" table.

1	Travel Date
2	Agreement
3	Billing
4	Contact method
5	Email 

Open in new window


How can I do an inner join to display those values instead of the ID's that I have in the casecomments table ?
0
Comment
Question by:amucinobluedot
  • 9
  • 8
18 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
MySQL does not have split function so you will havetowrite your own. You could find substring_index to be of use
0
 

Author Comment

by:amucinobluedot
Comment Utility
I have a split function. I just don't know how to use it in this instance. Below is my function:

USE [BlueDot]
GO
/****** Object:  UserDefinedFunction [dbo].[Split]    Script Date: 07/25/2016 19:29:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[Split] (@list nvarchar(max), @delim nchar(1) = ',')

returns table as

return
   with csvtbl(start, [stop]) as (
     select start = convert(bigint, 1), [stop] = charindex(@delim collate slovenian_bin2, @list + @delim)
     union all
     select start = [stop] + 1, [stop] = charindex(@delim collate slovenian_bin2, @list + @delim, [stop] + 1)
     from   csvtbl
     where  [stop] > 0
  )
  select ltrim(rtrim(substring(@list, start, case when [stop] > 0 then [stop] - start else 0 end))) as value
  from   csvtbl
  where  ([stop] > 0)
  and (ltrim(rtrim(substring(@list, start, case when [stop] > 0 then [stop] - start else 0 end))) <> '')

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Your SPLIT function syntax looks like SQL Server. Are you using SQL Server or MySQL?
0
 

Author Comment

by:amucinobluedot
Comment Utility
MS SQL 2008R2
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
try this.
;WITH CTE AS (
SELECT a.CaseID ,
           t.tag_text,
            a.Comments
  FROM Casecomments a
 CROSS APPLY Split(tags,',') s
  JOIN tags t ON s.value = t.id)
SELECT DISTINCT CaseID,
       rtrim(substring(isnull((select ','+tag_text from CTE t2 where t1.CaseiD = t2.CaseID for xml path('')),' '),2,2000)) tags,
       Comments
  FROM CTE t1
0
 

Author Comment

by:amucinobluedot
Comment Utility
Something may be wrong in the select, this is what I changed it to since the table name was incorrect.

WITH    CTE
          AS ( SELECT   a.CaseID ,
                        t.tag_text ,
                        a.Comments
               FROM     Casecomments a
                        CROSS APPLY Split(tags, ',') s
                        JOIN dbo.AdminTags t ON s.value = t.id
             )
    SELECT DISTINCT
            CaseID ,
            RTRIM(SUBSTRING(ISNULL(( SELECT ',' + tag_text
                                     FROM   CTE t2
                                     WHERE  t1.CaseID = t2.CaseID
                                   FOR
                                     XML PATH('')
                                   ), ' '), 2, 2000)) tags ,
            Comments
    FROM    CTE t1; 

Open in new window


I get this error:

Msg 321, Level 15, State 1, Line 6
"tags" is not a recognized table hints option. If it is intended as a parameter to a table-valued function or to the CHANGETABLE function, ensure that your database compatibility mode is set to 90.

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Is it not a column in your table, Casecomments? I am passing the column tags to your Split function to split it into multiple records.
0
 

Author Comment

by:amucinobluedot
Comment Utility
Also, is there a way to use the split function I already have ?
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
I used your split function only. To be specific, you can use with 3-part name.
WITH    CTE
          AS ( SELECT   a.CaseID ,
                        t.tag_text ,
                        a.Comments
               FROM     Casecomments a
                        CROSS APPLY BlueDot .dbo.Split(a.tags, ',') s
                        JOIN dbo.AdminTags t ON s.value = t.id
             )
    SELECT DISTINCT
            CaseID ,
            RTRIM(SUBSTRING(ISNULL(( SELECT ',' + tag_text
                                     FROM   CTE t2
                                     WHERE  t1.CaseID = t2.CaseID
                                   FOR
                                     XML PATH('')
                                   ), ' '), 2, 2000)) tags ,
            Comments
    FROM    CTE t1; 

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
like this
WITH    CTE
          AS ( SELECT   a.CaseID ,
                        t.tag_text ,
                        a.Comments
               FROM     Casecomments a
                        CROSS APPLY BlueDot.dbo.Split(a.tags, ',') s
                        JOIN dbo.AdminTags t ON s.value = t.id
             )
    SELECT DISTINCT
            CaseID ,
            RTRIM(SUBSTRING(ISNULL(( SELECT ',' + tag_text
                                     FROM   CTE t2
                                     WHERE  t1.CaseID = t2.CaseID
                                   FOR
                                     XML PATH('')
                                   ), ' '), 2, 2000)) tags ,
            Comments
    FROM    CTE t1; 

Open in new window

0
 

Author Comment

by:amucinobluedot
Comment Utility
I got this error now:

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '.'.
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
In my SSMS, It didn't return any error. May be any invisible character. Can you paste in any text editor and copy from there.
0
 

Author Comment

by:amucinobluedot
Comment Utility
I did and still get the same error. error
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
You may have some unprintable characters like LF or CR in the script. Notepad++ may be helpful to get them.
0
 

Author Comment

by:amucinobluedot
Comment Utility
That is what I used
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
Can you give a try on this.
;WITH CTE 
     AS (SELECT a.CaseID, 
                t.tag_text, 
                a.Comments 
           FROM Casecomments a 
                CROSS APPLY BlueDot.dbo.Split(a.tags, ',') s 
                JOIN dbo.AdminTags t 
                  ON s.value = t.id) 
SELECT DISTINCT CaseID, 
                Rtrim(Substring(Isnull((SELECT ',' + tag_text 
                                          FROM CTE t2 
                                         WHERE t1.CaseID = t2.CaseID 
                                        FOR XML PATH('')), ' '), 2, 2000)) tags, 
                Comments 
  FROM CTE t1; 

Open in new window

0
 

Author Comment

by:amucinobluedot
Comment Utility
Still get the same error  :$
Is there another way to split the numbers and join them to the admincatalog table ?
0
 

Author Comment

by:amucinobluedot
Comment Utility
I decided to store the actual tags in text instead of the numbers so I don't have to do a join and split each value, I can simply display the text tags as they are in the database. I will split points. Thanks for trying.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now