oracle - Complex SQL query (suggestions needed) -


could throw me ideas how resolve following issue:

i have pictures, videos , tags in database. tags can associated pictures , videos. have query database tag ids , count modified before 7 days or earlier , sort them count of associations have picture , video assets.

so idea @ end can output tags associated pictures or videos during last 7 days. wrote down db structure fields have

video     id  picture     id  picture_attrmapping     cs_ownerid  (id of picture)     cs_attrid  ( store id of tag attribute (picture_tag or video_tag))     assetvalue ( store id of associated tag)  video_attrmapping     cs_ownerid  (id of video)     cs_attrid  ( store id of tag attribute (picture_tag or video_tag))     assetvalue ( store id of associated tag)  tag     id     updatedate  attributes (picture_tag attribute id stored here)     id     name 

so can see tags, videos, picture , attributes stored in separate tables. can tell tag referenced video or picture (in other words, video/picture has tag association) if video/picture_attrmapping table has record video or picture tag attribute id in cs_attrid column , id of tag in assetvalue column.

i assuming query sub-queries, started break task in sub-tasks , figure out how obtain information that's needed.

i have id of tag attributes video , picture objects:

select id attributes name = 'picture_tag' or name = 'video_tag' 

also example of how can query tags n days old:

select id tag updateddate between to_date('2013-08-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss') , current_date 

this isn't complex seems, however, not sure how whole query should , start. throw idea or sample?

i using oracle, familiar mysql too, samples of dbmss great. let me know if made myself clear enough.

the tags updated in last 7 days can written as:

select id tags updatedate >= sysdate - 7 

from you're saying attributes table has 2 values in it; can ignore it. if has more tables picture_attrmapping , video_attrmapping ensure attributes enforced when join them doesn't matter either.

if want tag associated picture or video need enforce exists in 1 of *_arrtmapping tables:

select *   tags t   left outer join picture_attrmappings pa     on t.id = pa.assetvalue   left outer join video_attrmappings va     on t.id = va.assetvalue  t.updatedate >= sysdate - 7    , ( pa.assetvalue not null          or va.assetvalue not null          ) 

you want tag ids have been modified most; need order count:

select t.id   tags t   left outer join picture_attrmappings pa     on t.id = pa.assetvalue   left outer join video_attrmappings va     on t.id = va.assetvalue  t.updatedate >= sysdate - 7    , ( pa.assetvalue not null          or va.assetvalue not null          )  group t.id  order count(*) desc 

on different note quite strange schema (assuming you've left nothing out). expect picture_attrmappings junction table between tags , picture. tags table should store unique list of tags doesn't seem does. updatedate should in either picture_arrtmappings can know when each tag last updated or in picture know when tags last updated (or both).

i suspect you're missing of schema see no method, you've provided, of using either picture or video.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -