开发者

Merge of two select queries

I want to merge these two Select queries into single query. How can i do this?

 SELECT link_id, COUNT(*)  FROM Loc  GROUP BY link_id 

     SELECT Slink.[xlink:Show],Loc.[xlink:show],
     Slink.[xlink:actuate],Loc.[xlink:actuate] ,
     href, Sem.SemRoleDescrip
     FRO开发者_如何学JAVAM Sem  JOIN Loc  ON 
     Sem.SemRoleId = Loc.SemRoleId
  JOIN Slink ON Slink.link_id = Loc.link_id


One solution would be to

  • add the COUNT statement as a subquery
  • (LEFT) JOIN this subselect with the SLink table
  • Add the LinkCount to the list of selected values.

SQL Statement

SELECT  Slink.[xlink:Show]
        , Loc.[xlink:show]
        , Slink.[xlink:actuate]
        , Loc.[xlink:actuate] 
        , href
        , Sem.SemRoleDescrip
        , SLinkCount.LinkCount
FROM    Sem  
        JOIN Loc ON Sem.SemRoleId = Loc.SemRoleId
        JOIN Slink ON Slink.link_id = Loc.link_id
        LEFT JOIN (
          SELECT  link_id, COUNT(*) AS LinkCount 
          FROM    Loc  
          GROUP BY 
                  link_id 
        ) SLinkCount ON SLinkCount.link_id = Slink.link_id                  

You might want to read up on Subqueries in the reference manual

12.2.9.8. Subqueries in the FROM Clause

Subqueries are legal in a SELECT statement's FROM clause. The actual syntax is:

SELECT ... FROM (subquery) [AS] name ...

The [AS] name clause is mandatory, because every table in a FROM clause must have a name. Any columns in the subquery select list must have unique names.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜