使用Joins对表进行SQL查询并返回不同的记录

时间:2013-12-07 05:37:33

标签: sql

我正在执行SQL Query来过滤一些结果,构建了Query并产生了结果但是所有重复的记录也都来了。所以任何人都可以帮助我解决这个问题。我需要不同的记录并在3个不同的表格上查询。

我的查询:

 SELECT TOP 10 leafname                                        AS 'File Name', 
              MIN(Docs.dirname)                               AS 'Web Directory' 
              , 
              extension                                       AS 
              'File Type', 
              a.tp_title                                      AS 'Created By', 
              a.tp_login                                      AS 'Login Name', 
              CONVERT(VARCHAR(11), userdata.tp_created, 106)  AS 'Created Date', 
              CONVERT(VARCHAR(11), userdata.tp_modified, 106) AS 'Modified Date' 
              , 
              ( CAST(( CAST(CAST(size AS NUMERIC(18, 2)) / 1024 
                            AS NUMERIC(18, 2)) / 1024 ) AS 
                       NUMERIC(18, 2)) )                      AS 'Size in MB' 
--,b.tp_Title as Editor,b.tp_Login as EditorLogin 
FROM   Docs WITH(nolock) 
       INNER JOIN UserData WITH(nolock) 
               ON Docs.siteid = userdata.tp_siteid 
       INNER JOIN Webs WITH(nolock) 
               ON Docs.webid = Webs.id 
       INNER JOIN Sites WITH(nolock) 
               ON Webs.siteid = SItes.id 
       INNER JOIN UserInfo AS a WITH(nolock) 
               ON a.tp_id = UserData.tp_author 
                  AND a.tp_siteid = SItes.id 
WHERE  ( leafname LIKE 'Default.aspx' 
          OR extension LIKE 'aspx' ) 
       AND dirname NOT LIKE '%personal%' 
       AND dirname NOT LIKE '%_catalogs%' 
       AND Docs.dirname != '' 
--order by dirname 
GROUP  BY leafname, 
          Docs.dirname, 
          extension, 
          a.tp_title, 
          a.tp_login, 
          userdata.tp_created, 
          userdata.tp_modified, 
          Docs.size 

任何帮助.. ???

2 个答案:

答案 0 :(得分:0)

可能会对你有用。尝试这种方法。

select distinct tmp.* from (
SELECT TOP 10 
leafname AS 'File Name', 
MIN(Docs.dirname) AS 'Web Directory',
extension AS 'File Type', 
a.tp_title AS 'Created By', 
a.tp_login AS 'Login Name', 
CONVERT(VARCHAR(11), userdata.tp_created, 106)  AS 'Created Date', 
CONVERT(VARCHAR(11), userdata.tp_modified, 106) AS 'Modified Date', 
( CAST(( CAST(CAST(size AS NUMERIC(18, 2)) / 1024 AS 
NUMERIC(18, 2)) / 1024 ) AS NUMERIC(18, 2)) ) AS 'Size in MB' 
--,b.tp_Title as Editor,b.tp_Login as EditorLogin 
FROM   Docs WITH(nolock) 
       INNER JOIN UserData WITH(nolock) 
               ON Docs.siteid = userdata.tp_siteid 
       INNER JOIN Webs WITH(nolock) 
               ON Docs.webid = Webs.id 
       INNER JOIN Sites WITH(nolock) 
               ON Webs.siteid = SItes.id 
       INNER JOIN UserInfo AS a WITH(nolock) 
               ON a.tp_id = UserData.tp_author 
                  AND a.tp_siteid = SItes.id 
WHERE  ( leafname LIKE 'Default.aspx' 
          OR extension LIKE 'aspx' ) 
       AND dirname NOT LIKE '%personal%' 
       AND dirname NOT LIKE '%_catalogs%' 
       AND Docs.dirname != '' 
--order by dirname 
GROUP  BY leafname, 
          Docs.dirname, 
          extension, 
          a.tp_title, 
          a.tp_login, 
          userdata.tp_created, 
          userdata.tp_modified, 
          Docs.size 
) as tmp

答案 1 :(得分:0)

首先,请使用重复数据编写结果集的子集。 使用MIN()函数的目的是什么? 尝试在需要的地方使用DISTINCT。

最佳做法: 1)尝试在每个列名之前使用表别名(如a.tp_id)以避免ambiguos字段。 2)使用AS [别名]而不是AS'别名'