Find similar items

Find similar items

am 30.01.2008 23:24:56 von Tem

I have a single table that contains information of photos

ie.
ID PhotoName PhotoTags
23 my cat cat animal pet
24 cell phone electronic communication
25 tiger animal zoo

What would be a possible way to write a query that returns similar items
- share similar tags, similar photo name


can this be done with a sql query?

Thank you
Tem

Re: Find similar items

am 30.01.2008 23:28:59 von Tom Moreau

It can, but you need a better design. Each individual tag should be in a
row by itself in a PhotoTags table, with a foreign key to the Photos table.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Tem" wrote in message
news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
I have a single table that contains information of photos

ie.
ID PhotoName PhotoTags
23 my cat cat animal pet
24 cell phone electronic communication
25 tiger animal zoo

What would be a possible way to write a query that returns similar items
- share similar tags, similar photo name


can this be done with a sql query?

Thank you
Tem

Re: Find similar items

am 30.01.2008 23:32:40 von TheSQLGuru

need to join the table on itself for this. note it will be SLOOOOWWWW if
the table is huge.

select t1.*, t2.*
from yourtable t1 join yourtable t2 on t1.photoname = t2.photoname
and t1.phototags = t2.phototags


--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"Tem" wrote in message
news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>I have a single table that contains information of photos
>
> ie.
> ID PhotoName PhotoTags
> 23 my cat cat animal pet
> 24 cell phone electronic communication
> 25 tiger animal zoo
>
> What would be a possible way to write a query that returns similar items
> - share similar tags, similar photo name
>
>
> can this be done with a sql query?
>
> Thank you
> Tem
>

Re: Find similar items

am 30.01.2008 23:38:31 von Joe Celko

>> can this be done with an SQL query? <<

Yes, but why not buy a document or textbase package which is designed
to work with this type of data?

Re: Find similar items

am 31.01.2008 00:24:37 von Tem

How would I write that query?

Thanks

"Tom Moreau" wrote in message
news:e4ur0$4YIHA.1204@TK2MSFTNGP03.phx.gbl...
> It can, but you need a better design. Each individual tag should be in a
> row by itself in a PhotoTags table, with a foreign key to the Photos
> table.
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Tem" wrote in message
> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
> I have a single table that contains information of photos
>
> ie.
> ID PhotoName PhotoTags
> 23 my cat cat animal pet
> 24 cell phone electronic communication
> 25 tiger animal zoo
>
> What would be a possible way to write a query that returns similar items
> - share similar tags, similar photo name
>
>
> can this be done with a sql query?
>
> Thank you
> Tem
>
>

Re: Find similar items

am 31.01.2008 00:25:10 von Tem

any recommendations?

"--CELKO--" wrote in message
news:aed4d570-d9ac-4890-9cf7-e3634f216d70@s12g2000prg.google groups.com...
>>> can this be done with an SQL query? <<
>
> Yes, but why not buy a document or textbase package which is designed
> to work with this type of data?

Re: Find similar items

am 31.01.2008 00:25:32 von Tem

thanks ill try it

"TheSQLGuru" wrote in message
news:13q1untmhbnam3a@corp.supernews.com...
> need to join the table on itself for this. note it will be SLOOOOWWWW if
> the table is huge.
>
> select t1.*, t2.*
> from yourtable t1 join yourtable t2 on t1.photoname = t2.photoname
> and t1.phototags = t2.phototags
>
>
> --
> Kevin G. Boles
> Indicium Resources, Inc.
> SQL Server MVP
> kgboles a earthlink dt net
>
>
> "Tem" wrote in message
> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>I have a single table that contains information of photos
>>
>> ie.
>> ID PhotoName PhotoTags
>> 23 my cat cat animal pet
>> 24 cell phone electronic communication
>> 25 tiger animal zoo
>>
>> What would be a possible way to write a query that returns similar items
>> - share similar tags, similar photo name
>>
>>
>> can this be done with a sql query?
>>
>> Thank you
>> Tem
>>
>
>

Re: Find similar items

am 31.01.2008 02:36:13 von Mikhail Berlyant

If you are on 2005, check Term Extraction and Term Lookup Transformations
You can build pretty much "smart" service on top of this
If you are interested in fuzzy matching - you can take a look at Fuzzy
Lookup and Fuzzy Grouping Transformations

--
Mikhail Berlyant
Senior Data Architect
MySpace.com

"Tem" wrote in message
news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>I have a single table that contains information of photos
>
> ie.
> ID PhotoName PhotoTags
> 23 my cat cat animal pet
> 24 cell phone electronic communication
> 25 tiger animal zoo
>
> What would be a possible way to write a query that returns similar items
> - share similar tags, similar photo name
>
>
> can this be done with a sql query?
>
> Thank you
> Tem
>

Re: Find similar items

am 31.01.2008 07:11:40 von Tem

this is very cool!

"Mikhail Berlyant" wrote in message
news:OK5Wrm6YIHA.1208@TK2MSFTNGP05.phx.gbl...
> If you are on 2005, check Term Extraction and Term Lookup Transformations
> You can build pretty much "smart" service on top of this
> If you are interested in fuzzy matching - you can take a look at Fuzzy
> Lookup and Fuzzy Grouping Transformations
>
> --
> Mikhail Berlyant
> Senior Data Architect
> MySpace.com
>
> "Tem" wrote in message
> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>I have a single table that contains information of photos
>>
>> ie.
>> ID PhotoName PhotoTags
>> 23 my cat cat animal pet
>> 24 cell phone electronic communication
>> 25 tiger animal zoo
>>
>> What would be a possible way to write a query that returns similar items
>> - share similar tags, similar photo name
>>
>>
>> can this be done with a sql query?
>>
>> Thank you
>> Tem
>>
>
>

Re: Find similar items

am 31.01.2008 07:58:25 von Tem

Table Tags
ID TagName PhotoId
1 cat 23
2 animal 23
3 pet 23
4 animal 25

select PhotoId from Tags where PhotoId = 23 and (other photoId that has 23's
tags)

Need some help with this sql statement

"Tem" wrote in message
news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
> I have a single table that contains information of photos
>
> ie.
> ID PhotoName PhotoTags
> 23 my cat cat animal pet
> 24 cell phone electronic communication
> 25 tiger animal zoo
>
> What would be a possible way to write a query that returns similar items
> - share similar tags, similar photo name
>
>
> can this be done with a sql query?
>
> Thank you
> Tem
>

Re: Find similar items

am 31.01.2008 14:13:26 von Tom Moreau

So is the requirement that there must be a match on ANY tags of PhotoId = 23
(in which case, PhotoID = 25 WILL match) or is it that you want a match on
ALL tags of PhotoId = 23 (in which case PhotoId WILL NOT match)?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Tem" wrote in message
news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
Table Tags
ID TagName PhotoId
1 cat 23
2 animal 23
3 pet 23
4 animal 25

select PhotoId from Tags where PhotoId = 23 and (other photoId that has 23's
tags)

Need some help with this sql statement

"Tem" wrote in message
news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
> I have a single table that contains information of photos
>
> ie.
> ID PhotoName PhotoTags
> 23 my cat cat animal pet
> 24 cell phone electronic communication
> 25 tiger animal zoo
>
> What would be a possible way to write a query that returns similar items
> - share similar tags, similar photo name
>
>
> can this be done with a sql query?
>
> Thank you
> Tem
>

Re: Find similar items

am 31.01.2008 17:47:29 von Mikhail Berlyant

Below is VERY SIMPLIFIED example :
you create dictionary table and populate it with distinct terms from all
phrases you have:

TagID TagName
1 cat
2 animal
3 pat
4 electronic
5 communication
6 zoo

Next you join this Dictionary table with Phrase table and end up with Tags
table (in more real example you would have here frequency field, but for now
you can skip this for simplicity sake):

PhotoID TagID
23 1
23 2
23 3
24 4
24 5
25 2
25 6

Your final query would look like:

SELECT PhotoID, COUNT(*) AS Score
FROM Tags
WHERE TagID in (SELECT TagID FROM Tags WHERE PhotoID = 23)
GROUP BY PhotoID
ORDER BY Score DESC

I hope you will get an idea

--
Mikhail Berlyant
Senior Data Architect
MySpace.com

"Tem" wrote in message
news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
> Table Tags
> ID TagName PhotoId
> 1 cat 23
> 2 animal 23
> 3 pet 23
> 4 animal 25
>
> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
> 23's tags)
>
> Need some help with this sql statement
>
> "Tem" wrote in message
> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>> I have a single table that contains information of photos
>>
>> ie.
>> ID PhotoName PhotoTags
>> 23 my cat cat animal pet
>> 24 cell phone electronic communication
>> 25 tiger animal zoo
>>
>> What would be a possible way to write a query that returns similar items
>> - share similar tags, similar photo name
>>
>>
>> can this be done with a sql query?
>>
>> Thank you
>> Tem
>>

Re: Find similar items

am 31.01.2008 23:08:55 von Tem

What im trying to say is

since 23 has the tags "cat animal pet" it should return other photoIds that
contain "cat OR animal OR pet" (in any order)

1 cat 23
2 animal 23
3 pet 23
4 animal 25
5 dog 25

If possible put the closest matching ones "cat AND animal AND pet" at the
top.
Hence getting similar photos

"Tom Moreau" wrote in message
news:ORBGKsAZIHA.1204@TK2MSFTNGP03.phx.gbl...
> So is the requirement that there must be a match on ANY tags of PhotoId =
> 23
> (in which case, PhotoID = 25 WILL match) or is it that you want a match on
> ALL tags of PhotoId = 23 (in which case PhotoId WILL NOT match)?
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Tem" wrote in message
> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
> Table Tags
> ID TagName PhotoId
> 1 cat 23
> 2 animal 23
> 3 pet 23
> 4 animal 25
>
> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
> 23's
> tags)
>
> Need some help with this sql statement
>
> "Tem" wrote in message
> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>> I have a single table that contains information of photos
>>
>> ie.
>> ID PhotoName PhotoTags
>> 23 my cat cat animal pet
>> 24 cell phone electronic communication
>> 25 tiger animal zoo
>>
>> What would be a possible way to write a query that returns similar items
>> - share similar tags, similar photo name
>>
>>
>> can this be done with a sql query?
>>
>> Thank you
>> Tem
>>
>

Re: Find similar items

am 31.01.2008 23:10:16 von Tem

So with this design I would have 3 tables?

Photos Tags TagFreq

is that right?

"Mikhail Berlyant" wrote in message
news:#v7s4jCZIHA.4284@TK2MSFTNGP03.phx.gbl...
> Below is VERY SIMPLIFIED example :
> you create dictionary table and populate it with distinct terms from all
> phrases you have:
>
> TagID TagName
> 1 cat
> 2 animal
> 3 pat
> 4 electronic
> 5 communication
> 6 zoo
>
> Next you join this Dictionary table with Phrase table and end up with Tags
> table (in more real example you would have here frequency field, but for
> now you can skip this for simplicity sake):
>
> PhotoID TagID
> 23 1
> 23 2
> 23 3
> 24 4
> 24 5
> 25 2
> 25 6
>
> Your final query would look like:
>
> SELECT PhotoID, COUNT(*) AS Score
> FROM Tags
> WHERE TagID in (SELECT TagID FROM Tags WHERE PhotoID = 23)
> GROUP BY PhotoID
> ORDER BY Score DESC
>
> I hope you will get an idea
>
> --
> Mikhail Berlyant
> Senior Data Architect
> MySpace.com
>
> "Tem" wrote in message
> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
>> Table Tags
>> ID TagName PhotoId
>> 1 cat 23
>> 2 animal 23
>> 3 pet 23
>> 4 animal 25
>>
>> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
>> 23's tags)
>>
>> Need some help with this sql statement
>>
>> "Tem" wrote in message
>> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>> I have a single table that contains information of photos
>>>
>>> ie.
>>> ID PhotoName PhotoTags
>>> 23 my cat cat animal pet
>>> 24 cell phone electronic communication
>>> 25 tiger animal zoo
>>>
>>> What would be a possible way to write a query that returns similar items
>>> - share similar tags, similar photo name
>>>
>>>
>>> can this be done with a sql query?
>>>
>>> Thank you
>>> Tem
>>>
>
>

Re: Find similar items

am 31.01.2008 23:16:58 von Mikhail Berlyant

So far, these are only I see you need

--
Mikhail Berlyant
Senior Data Architect
MySpace.com

"Tem" wrote in message
news:eIFlRYFZIHA.4172@TK2MSFTNGP02.phx.gbl...
> So with this design I would have 3 tables?
>
> Photos Tags TagFreq
>
> is that right?
>
> "Mikhail Berlyant" wrote in message
> news:#v7s4jCZIHA.4284@TK2MSFTNGP03.phx.gbl...
>> Below is VERY SIMPLIFIED example :
>> you create dictionary table and populate it with distinct terms from all
>> phrases you have:
>>
>> TagID TagName
>> 1 cat
>> 2 animal
>> 3 pat
>> 4 electronic
>> 5 communication
>> 6 zoo
>>
>> Next you join this Dictionary table with Phrase table and end up with
>> Tags table (in more real example you would have here frequency field, but
>> for now you can skip this for simplicity sake):
>>
>> PhotoID TagID
>> 23 1
>> 23 2
>> 23 3
>> 24 4
>> 24 5
>> 25 2
>> 25 6
>>
>> Your final query would look like:
>>
>> SELECT PhotoID, COUNT(*) AS Score
>> FROM Tags
>> WHERE TagID in (SELECT TagID FROM Tags WHERE PhotoID = 23)
>> GROUP BY PhotoID
>> ORDER BY Score DESC
>>
>> I hope you will get an idea
>>
>> --
>> Mikhail Berlyant
>> Senior Data Architect
>> MySpace.com
>>
>> "Tem" wrote in message
>> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
>>> Table Tags
>>> ID TagName PhotoId
>>> 1 cat 23
>>> 2 animal 23
>>> 3 pet 23
>>> 4 animal 25
>>>
>>> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
>>> 23's tags)
>>>
>>> Need some help with this sql statement
>>>
>>> "Tem" wrote in message
>>> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>>> I have a single table that contains information of photos
>>>>
>>>> ie.
>>>> ID PhotoName PhotoTags
>>>> 23 my cat cat animal pet
>>>> 24 cell phone electronic communication
>>>> 25 tiger animal zoo
>>>>
>>>> What would be a possible way to write a query that returns similar
>>>> items
>>>> - share similar tags, similar photo name
>>>>
>>>>
>>>> can this be done with a sql query?
>>>>
>>>> Thank you
>>>> Tem
>>>>
>>
>>

Re: Find similar items

am 31.01.2008 23:37:57 von Tom Moreau

Then this should do it:

select
t1.PhotoID
, count (*)
from
Tags t1
join
Tags t2 on t2.TagName = t1.TagName
where
t2.PhotoId = 23
and
t1.PhotoId <> 23
group by
t1.PhotoID
order by
count (*) desc

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Tem" wrote in message
news:OBhmhXFZIHA.4860@TK2MSFTNGP03.phx.gbl...
What im trying to say is

since 23 has the tags "cat animal pet" it should return other photoIds that
contain "cat OR animal OR pet" (in any order)

1 cat 23
2 animal 23
3 pet 23
4 animal 25
5 dog 25

If possible put the closest matching ones "cat AND animal AND pet" at the
top.
Hence getting similar photos

"Tom Moreau" wrote in message
news:ORBGKsAZIHA.1204@TK2MSFTNGP03.phx.gbl...
> So is the requirement that there must be a match on ANY tags of PhotoId =
> 23
> (in which case, PhotoID = 25 WILL match) or is it that you want a match on
> ALL tags of PhotoId = 23 (in which case PhotoId WILL NOT match)?
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Tem" wrote in message
> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
> Table Tags
> ID TagName PhotoId
> 1 cat 23
> 2 animal 23
> 3 pet 23
> 4 animal 25
>
> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
> 23's
> tags)
>
> Need some help with this sql statement
>
> "Tem" wrote in message
> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>> I have a single table that contains information of photos
>>
>> ie.
>> ID PhotoName PhotoTags
>> 23 my cat cat animal pet
>> 24 cell phone electronic communication
>> 25 tiger animal zoo
>>
>> What would be a possible way to write a query that returns similar items
>> - share similar tags, similar photo name
>>
>>
>> can this be done with a sql query?
>>
>> Thank you
>> Tem
>>
>

Re: Find similar items

am 01.02.2008 04:59:00 von Tem

The query did not return anything


ID TagName PhotoID
1 cat 23
2 animal 23
3 pet 23
4 animal 25
5 dog 25
6 car 26
7 phone 26

if in the query @PhotoID = 25, it should return 23
if in the query @PhotoID = 26, it should return nothing




"Tom Moreau" wrote in message
news:uwDG#nFZIHA.4448@TK2MSFTNGP03.phx.gbl...
> Then this should do it:
>
> select
> t1.PhotoID
> , count (*)
> from
> Tags t1
> join
> Tags t2 on t2.TagName = t1.TagName
> where
> t2.PhotoId = 23
> and
> t1.PhotoId <> 23
> group by
> t1.PhotoID
> order by
> count (*) desc
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
>
>
> "Tem" wrote in message
> news:OBhmhXFZIHA.4860@TK2MSFTNGP03.phx.gbl...
> What im trying to say is
>
> since 23 has the tags "cat animal pet" it should return other photoIds
> that
> contain "cat OR animal OR pet" (in any order)
>
> 1 cat 23
> 2 animal 23
> 3 pet 23
> 4 animal 25
> 5 dog 25
>
> If possible put the closest matching ones "cat AND animal AND pet" at the
> top.
> Hence getting similar photos
>
> "Tom Moreau" wrote in message
> news:ORBGKsAZIHA.1204@TK2MSFTNGP03.phx.gbl...
>> So is the requirement that there must be a match on ANY tags of PhotoId =
>> 23
>> (in which case, PhotoID = 25 WILL match) or is it that you want a match
>> on
>> ALL tags of PhotoId = 23 (in which case PhotoId WILL NOT match)?
>>
>> --
>> Tom
>>
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>> SQL Server MVP
>> Toronto, ON Canada
>> https://mvp.support.microsoft.com/profile/Tom.Moreau
>>
>>
>> "Tem" wrote in message
>> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
>> Table Tags
>> ID TagName PhotoId
>> 1 cat 23
>> 2 animal 23
>> 3 pet 23
>> 4 animal 25
>>
>> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
>> 23's
>> tags)
>>
>> Need some help with this sql statement
>>
>> "Tem" wrote in message
>> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>> I have a single table that contains information of photos
>>>
>>> ie.
>>> ID PhotoName PhotoTags
>>> 23 my cat cat animal pet
>>> 24 cell phone electronic communication
>>> 25 tiger animal zoo
>>>
>>> What would be a possible way to write a query that returns similar items
>>> - share similar tags, similar photo name
>>>
>>>
>>> can this be done with a sql query?
>>>
>>> Thank you
>>> Tem
>>>
>>
>

Re: Find similar items

am 01.02.2008 05:36:12 von Tem

Please ignore my last post

Count still does not work

ID TagName PhotoID
1 cat 23
2 animal 23
3 pet 23
4 animal 25
5 dog 25
6 car 26
7 phone 26
8 cat 27
9 animal 27

if in the query @PhotoID = 25, it should return
PhotoId Count
23 1


and if in the query @PhotoID = 27, it should return
PhotoId Count
27 2
25 1







"Tem" wrote in message
news:OSrbKbIZIHA.4448@TK2MSFTNGP03.phx.gbl...
> The query did not return anything
>
>
> ID TagName PhotoID
> 1 cat 23
> 2 animal 23
> 3 pet 23
> 4 animal 25
> 5 dog 25
> 6 car 26
> 7 phone 26
>
> if in the query @PhotoID = 25, it should return 23
> if in the query @PhotoID = 26, it should return nothing
>
>
>
>
> "Tom Moreau" wrote in message
> news:uwDG#nFZIHA.4448@TK2MSFTNGP03.phx.gbl...
>> Then this should do it:
>>
>> select
>> t1.PhotoID
>> , count (*)
>> from
>> Tags t1
>> join
>> Tags t2 on t2.TagName = t1.TagName
>> where
>> t2.PhotoId = 23
>> and
>> t1.PhotoId <> 23
>> group by
>> t1.PhotoID
>> order by
>> count (*) desc
>>
>> --
>> Tom
>>
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>> SQL Server MVP
>> Toronto, ON Canada
>> https://mvp.support.microsoft.com/profile/Tom.Moreau
>>
>>
>> "Tem" wrote in message
>> news:OBhmhXFZIHA.4860@TK2MSFTNGP03.phx.gbl...
>> What im trying to say is
>>
>> since 23 has the tags "cat animal pet" it should return other photoIds
>> that
>> contain "cat OR animal OR pet" (in any order)
>>
>> 1 cat 23
>> 2 animal 23
>> 3 pet 23
>> 4 animal 25
>> 5 dog 25
>>
>> If possible put the closest matching ones "cat AND animal AND pet" at the
>> top.
>> Hence getting similar photos
>>
>> "Tom Moreau" wrote in message
>> news:ORBGKsAZIHA.1204@TK2MSFTNGP03.phx.gbl...
>>> So is the requirement that there must be a match on ANY tags of PhotoId
>>> =
>>> 23
>>> (in which case, PhotoID = 25 WILL match) or is it that you want a match
>>> on
>>> ALL tags of PhotoId = 23 (in which case PhotoId WILL NOT match)?
>>>
>>> --
>>> Tom
>>>
>>> ----------------------------------------------------
>>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>>> SQL Server MVP
>>> Toronto, ON Canada
>>> https://mvp.support.microsoft.com/profile/Tom.Moreau
>>>
>>>
>>> "Tem" wrote in message
>>> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
>>> Table Tags
>>> ID TagName PhotoId
>>> 1 cat 23
>>> 2 animal 23
>>> 3 pet 23
>>> 4 animal 25
>>>
>>> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
>>> 23's
>>> tags)
>>>
>>> Need some help with this sql statement
>>>
>>> "Tem" wrote in message
>>> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>>> I have a single table that contains information of photos
>>>>
>>>> ie.
>>>> ID PhotoName PhotoTags
>>>> 23 my cat cat animal pet
>>>> 24 cell phone electronic communication
>>>> 25 tiger animal zoo
>>>>
>>>> What would be a possible way to write a query that returns similar
>>>> items
>>>> - share similar tags, similar photo name
>>>>
>>>>
>>>> can this be done with a sql query?
>>>>
>>>> Thank you
>>>> Tem
>>>>
>>>
>>

Re: Find similar items

am 01.02.2008 13:08:17 von Tom Moreau

My original code works. Here's a complete repro:

create table Tags
(
ID int primary key
, TagName varchar (12) not null
, PhotoID int not null
)
go
insert Tags values (1, 'cat', 23)
insert Tags values (2, 'animal', 23)
insert Tags values (3, 'pet', 23)
insert Tags values (4, 'animal', 25)
insert Tags values (5, 'dog', 25)
insert Tags values (6, 'car', 26)
insert Tags values (7, 'phone', 26)
insert Tags values (8, 'cat ', 27)
insert Tags values (9, 'animal', 27)
go
select
t1.PhotoID
, count (*)
from
Tags t1
join
Tags t2 on t2.TagName = t1.TagName
where
t2.PhotoId = 27
and
t1.PhotoId <> 27
group by
t1.PhotoID
order by
count (*) desc
go
drop table tags

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Tem" wrote in message
news:OqwS8vIZIHA.3652@TK2MSFTNGP02.phx.gbl...
Please ignore my last post

Count still does not work

ID TagName PhotoID
1 cat 23
2 animal 23
3 pet 23
4 animal 25
5 dog 25
6 car 26
7 phone 26
8 cat 27
9 animal 27

if in the query @PhotoID = 25, it should return
PhotoId Count
23 1


and if in the query @PhotoID = 27, it should return
PhotoId Count
27 2
25 1







"Tem" wrote in message
news:OSrbKbIZIHA.4448@TK2MSFTNGP03.phx.gbl...
> The query did not return anything
>
>
> ID TagName PhotoID
> 1 cat 23
> 2 animal 23
> 3 pet 23
> 4 animal 25
> 5 dog 25
> 6 car 26
> 7 phone 26
>
> if in the query @PhotoID = 25, it should return 23
> if in the query @PhotoID = 26, it should return nothing
>
>
>
>
> "Tom Moreau" wrote in message
> news:uwDG#nFZIHA.4448@TK2MSFTNGP03.phx.gbl...
>> Then this should do it:
>>
>> select
>> t1.PhotoID
>> , count (*)
>> from
>> Tags t1
>> join
>> Tags t2 on t2.TagName = t1.TagName
>> where
>> t2.PhotoId = 23
>> and
>> t1.PhotoId <> 23
>> group by
>> t1.PhotoID
>> order by
>> count (*) desc
>>
>> --
>> Tom
>>
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>> SQL Server MVP
>> Toronto, ON Canada
>> https://mvp.support.microsoft.com/profile/Tom.Moreau
>>
>>
>> "Tem" wrote in message
>> news:OBhmhXFZIHA.4860@TK2MSFTNGP03.phx.gbl...
>> What im trying to say is
>>
>> since 23 has the tags "cat animal pet" it should return other photoIds
>> that
>> contain "cat OR animal OR pet" (in any order)
>>
>> 1 cat 23
>> 2 animal 23
>> 3 pet 23
>> 4 animal 25
>> 5 dog 25
>>
>> If possible put the closest matching ones "cat AND animal AND pet" at the
>> top.
>> Hence getting similar photos
>>
>> "Tom Moreau" wrote in message
>> news:ORBGKsAZIHA.1204@TK2MSFTNGP03.phx.gbl...
>>> So is the requirement that there must be a match on ANY tags of PhotoId
>>> =
>>> 23
>>> (in which case, PhotoID = 25 WILL match) or is it that you want a match
>>> on
>>> ALL tags of PhotoId = 23 (in which case PhotoId WILL NOT match)?
>>>
>>> --
>>> Tom
>>>
>>> ----------------------------------------------------
>>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>>> SQL Server MVP
>>> Toronto, ON Canada
>>> https://mvp.support.microsoft.com/profile/Tom.Moreau
>>>
>>>
>>> "Tem" wrote in message
>>> news:uaC8ta9YIHA.5900@TK2MSFTNGP02.phx.gbl...
>>> Table Tags
>>> ID TagName PhotoId
>>> 1 cat 23
>>> 2 animal 23
>>> 3 pet 23
>>> 4 animal 25
>>>
>>> select PhotoId from Tags where PhotoId = 23 and (other photoId that has
>>> 23's
>>> tags)
>>>
>>> Need some help with this sql statement
>>>
>>> "Tem" wrote in message
>>> news:uUVgy74YIHA.484@TK2MSFTNGP06.phx.gbl...
>>>> I have a single table that contains information of photos
>>>>
>>>> ie.
>>>> ID PhotoName PhotoTags
>>>> 23 my cat cat animal pet
>>>> 24 cell phone electronic communication
>>>> 25 tiger animal zoo
>>>>
>>>> What would be a possible way to write a query that returns similar
>>>> items
>>>> - share similar tags, similar photo name
>>>>
>>>>
>>>> can this be done with a sql query?
>>>>
>>>> Thank you
>>>> Tem
>>>>
>>>
>>