Author |
Topic |
GC72
Starting Member
16 Posts |
Posted - 2013-07-05 : 12:52:19
|
Hi,I have three tables where ENTRY table (EntryID) is for the main records, EntryAttribute table (EntryID, AttributeID)is populated when an AttributeDefinitionID is associated with the Entry and the actual value for the attribute is stored in AttributeString table (AttributeID, AttributeDefinitionID, Value).An EntryID can have multiple AttributeID for the Language attribute where the attributedefinitionid=5. But I'd like to return distinct EntryIDs that have only 1 attributeID for attributedefinition=44Hope this makes sense. This is my query where I am querying for a count of AttributeIDs per distinct EntryID where the AttributeDefinitionID=5 So that I can see all the EntryIDs that have a count of 1. However, the numbers doesnt seem right.Select distinct a.entryID, count(distinct b.attributeID) as Languagefrom entry as ainner join EntryAttribute as b on b.entryID = a.entryIDinner join AttributeString as c on c.AttributeID = c.AttributeIDwhere c.AttributeDefinitionID =5GROUP BY A.ENTRYidHow can I do this by using Having Count, to return total number of distinct ENTRYIDs with 1 AttributeID for the Language attribute (AttributedefinitonID=5)ThanksGC |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-05 : 12:58:58
|
quote: Originally posted by GC72 Hi,I have three tables where ENTRY table (EntryID) is for the main records, EntryAttribute table (EntryID, AttributeID)is populated when an AttributeDefinitionID is associated with the Entry and the actual value for the attribute is stored in AttributeString table (AttributeID, AttributeDefinitionID, Value).An EntryID can have multiple AttributeID for the Language attribute where the attributedefinitionid=5. But I'd like to return distinct EntryIDs that have only 1 attributeID for attributedefinition=44Hope this makes sense. This is my query where I am querying for a count of AttributeIDs per distinct EntryID where the AttributeDefinitionID=5 So that I can see all the EntryIDs that have a count of 1. However, the numbers doesnt seem right.Select distinct a.entryID, count(distinct b.attributeID) as Languagefrom entry as ainner join EntryAttribute as b on b.entryID = a.entryIDinner join AttributeString as c on c.AttributeID = c.AttributeIDwhere c.AttributeDefinitionID =5GROUP BY A.ENTRYidHow can I do this by using Having Count, to return total number of distinct ENTRYIDs with 1 AttributeID for the Language attribute (AttributedefinitonID=5)ThanksGC
I didn't follow the logic you are trying to implement. Nonetheless, don't you need a "attributedefinition=44" somewhere in that query? |
|
|
GC72
Starting Member
16 Posts |
Posted - 2013-07-05 : 13:09:09
|
It should be attributedefiniton = 5 not 44(my mistake). AttributeString tableAttributeID, AttributeDefinitionID, AttributeValue01 |
|
|
GC72
Starting Member
16 Posts |
Posted - 2013-07-05 : 13:09:09
|
It should be attributedefiniton = 5 not 44(my mistake). AttributeString tableAttributeID, AttributeDefinitionID, AttributeValue01 |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-05 : 13:17:59
|
Is EntryAttribute to AttributeString table a 1-1 relationship, or can there be one to many? If it is 1-1, perhaps this?This is the same code that you posted except for adding a having clause at the end.SELECT DISTINCT a.entryID , COUNT(DISTINCT b.attributeID) AS LanguageFROM entry AS a INNER JOIN EntryAttribute AS b ON b.entryID = a.entryID INNER JOIN AttributeString AS c ON c.AttributeID = c.AttributeIDWHERE c.AttributeDefinitionID = 5GROUP BY A.ENTRYid[red]HAVING COUNT(DISTINCT b.attributeID) = 1 [/code] |
|
|
GC72
Starting Member
16 Posts |
Posted - 2013-07-05 : 13:21:52
|
It should be attributedefiniton = 5 not 44(my mistake). AttributeString tableAttributeID, AttributeDefinitionID, AttributeValue01 5 English02 5 French03 5 German04 5 Italian05 10 someothervalue not languageEntryAttribute tableEntryID, AttributeID444 01444 02444 03123 01666 04333 02333 03So I am interested in ENTRYIDs that have only 1 attribute assigned where attribute type is language and is defined by attributedefinitonid = 5for exmaple EntryIDs 123, and ,666 should return as they have 1 attributeIDHope this makes sense |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-05 : 13:33:04
|
[code]SELECT e.EntryIdFROM EntryAttribute e INNER JOIN AttributeString a ON a.AttributeID = e.AttributeID AND a.AttributeDefinitionID = 5GROUP BY e.EntryIdHAVING COUNT(DISTINCT e.AttributeId) = 1[/code] |
|
|
GC72
Starting Member
16 Posts |
Posted - 2013-07-08 : 06:51:42
|
Thanks James. There can be one to many between EntryAttribute and AttributeString, e.g. one EntryID can have many AttributeIDs |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-08 : 10:51:09
|
Can you try the query that I posted earlier? Does it give incorrect results? |
|
|
GC72
Starting Member
16 Posts |
Posted - 2013-07-08 : 12:13:55
|
It seems to give correct results. Many thanks |
|
|
|