开发者

SQL Query (pl/sql) for a complicated logic

I have been stuck with this problem for a while. First I will explain the table structure

There are four tables in my system

Domain

Columns: 
      DomainID (primary key)
      DomainName

 Data:
        DomainID  DomainName
      1000        Google.com
      2000        mySql.com

WebPage

 Columns:
      WebpageID   (primary key)
      WebpageName
      DomainID   (FK from domain table)
 Data:
    5001   SearchPage.html   1000
    5002   Welcome.html      1000
    5003   ContactUs.htm     1000
    5004   AboutUs.html      1000

PluginType (PluginsType that can be added to a web page) This table lists the plugin types available for each Domain

  Columns: 
         PluginType     (primary key)
         DomainID       (primary key)
         PluginTypeName  

   Data
        PluginTypeID        DomainID        PluginTypeName
         8000                 1000             searchButton
         8001                 1000             DropDownMenu
         8002                 1000             InteractiveForm
         8003                 1000             loginForm
         8004                 1000             LogoutForm

Plugin: Each webpage in a domain can use any number of plugins.

      Columns:
             PluginID     (primary key)
             WebpageID    (FK from webpage table)
   开发者_C百科          pluginTypeID (FK from plugintype table)

      Data:
         pluginID        WebpageID(Name)         PluginTypeID
          10001             5001(SearchPage.html)   8000(SearchButton)
          10002             5001(SearchPage.html)   8001(DropDownMenu)
          10003             5002 (Welcome.html)     8000 (SearchButton)
          10004             5002 (Welcome.html)     8001 (DropDownMenu)
          10005             5002 (Welcome.html)     8004 (lotoutform)  
          10006             5003 (ContactUs.htm)    8003 (loginForm)
          10007             5004 (AboutUs.htm)      8002 (loginForm)

Now what I want is, given a domain ID, I want the list of all webpages-plugins available, in such a way that the plugin is not repeated in more than one webpage. To put in another way, webpage-plugin combination for each plugin, in such a way that the plugin is not repeated in more than one web page.

So for the domain 1000(google.com)

The result I want is

          5002 (Welcome.html)     8000 (SearchButton)
          5002 (Welcome.html)     8001 (DropDownMenu)
          5002 (Welcome.html)     8004 (lotoutform)
          5003 (ContactUs.htm)    8003 (loginForm)

I have chosen only 5002 and 5003 webpages, because they include all the plugins for the domain 1000(google.com). There is one more thing, it is preferred to select a webpage with just one plugin. But I am interested in finding out the solution without this preference, later maybe I can improve on the solution.


You can look at it from the other perspective, since you are returning one row per plugin-type, you need all the plugin-typess on a domain, along with a sample page where that plugin-type is, well, pluged-in, something along the lines of:

Select PluginTypeName, 
  (select top 1 WebpageName 
    from WebPage w
      inner join Plugin p on p.WebpageID = w.WebpageID
    where p.pluginTypeID = pt.pluginTypeID) as SampleWebPage
From PluginTypes pt

this will return something like:

searchButton    SearchPage.html
DropDownMenu    SearchPage.html
InteractiveForm null
loginForm       ContactUs.html
lotoutform      Welcome.html             


SELECT  PluginTypeId,
        (
        SELECT  webpageID
        FROM    plugin pi
        WHERE   pi.pluginTypeId = pd.pluginTypeId
                AND webPageID IN
                (
                SELECT  WebPageID
                FROM    WebPage
                WHERE   DomainId = 1000
                )
        ORDER BY
                (
                SELECT  COUNT(*)
                FROM    plugin pc
                WHERE   pc.webpageId = pi.webpageId
                )
        LIMIT 1
        ) AS WebPageId
FROM    Plugin pd
WHERE   WebPageId IS NOT NULL


Above result (submitted by SWeko) should work, I was trying it out, may give repeated results. Also, it groups results per domain... Here is a modification:

Select DISTINCT DomainID, pt.PluginTypeName, 
  (select top 1 WebpageName 
    from WebPage w
      inner join Plugin p on p.WebpageID = w.WebpageID
    where p.pluginTypeID = pt.pluginTypeID) as SampleWebPage
From PluginTypes pt
GROUP BY DomainID, pt.PluginTypeName
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜