4 Answers
4

Yes, well, half your problems are due to not having a resolved Data Model, and that will cause problems in SQL everywhere; if you fix that, the SQL will be much easier. The other half is not being able to use SQL effectively.

You have not posted info re the other tables, so I can't give you a reasonably accurate DM, I am going just from what you have provided, and of course made huge assumptions about the other related tables, which are most probably incorrect. If you provide the info, I can provide an accurate DM.

Is a Category Independent or "owned" by only one Member ?

Modelled: "No."

On what basis is a Category "shared" ?

Modelled: sharing is the same as ownership; a shared Category is simply one that is owned by more than one Member

Then, what is the difference between an "owned" Category and a "shared" Category ?

We do not know what the Categories are for (Subjects ?). In which case why are Categories, not Subjects, owned/shared by Members ?

Your Category, if it is owned, really should not be called Category, rather something like CategoryOwned.

I cannot see how CategoryViewed shows all the Views; it allows only one (the last ?) view per Member. Where is the ViewDate ?

Modelled: As a log of Views. If you want only one (the last) view per Member, remove ViewDate from the PK.

It is not a good idea to perform Unions, etc, unless you absolutely have to. Right now the data is small; when the database is large, you will certainly feel the effects of the work tables being created; filled; destroyed. So far from your requirement, there is no need for Unions, Distincts, worktables, etc. But the DM does present problems.

SQL. I cannot see how your SQL (or any of the other answers) retrieves your requirement of "the last time that category was viewed [by any Member]". Additionally, you should always try to get your SQL correct (returning the correct set of data) for each set, before Unioning, etc: you have not done that; that is why you have duplicate rows. Using DISTINCT is a stupid way to fix the problem; you are correct in seeking understanding instead.

I won't attempt to provide the SQL for your model. Here is the SQL for the Interim Data Model provided. Obviously, this is much, much cheaper than Unions:

DISTINCT made this query return the data I wanted but a couple questions remain, such as 1) why were there identical rows being returned and 2) does that query take more overhead than the UNION statement in one of my answers below..
–
David RyderNov 24 '10 at 18:54

Wouldn't it be cheaper and faster for David to simply learn SQL. If he doesn't, he's going to still have the same problems with the new DBMS.
–
PerformanceDBANov 25 '10 at 5:09

Would've added an answer earlier but wasn't entirely sure of your data model. I've got two points. Firstly you should be aware that UNION is like UNION ALL with a DISTINCT. If you can get away with using UNION ALL that would be better. Also, the original query was only joining to last_viewed using idcategory and not idmember.
–
Mike MeyersNov 24 '10 at 22:51

Ok. @Appreciate the input: That is Ugly as sin, and a performance headache that will not show up until the db gets reasonably populated, and then you have a nightmare removing all those Unions and worktables that you've coded everywhere. Fix the data model first, then the SQL is straight-forward
–
PerformanceDBANov 26 '10 at 8:39