尝试使用yes获取新列,没有值

时间:2012-01-25 11:42:28

标签: sql sql-server

我有两张桌子:

  1. 列有列的文件夹(folderid,foldername,userid)
  2. 列入列表的用户(folderid,shortlisteduserid) 它们与folderid链接
  3. 我正在使用以下查询来获取由userid 50创建的文件夹列表,并计算50已将每个文件夹中的用户数列入候选

    select a.folderid,a.foldername,count(b.ShortlistedUserId) as [count]
    from tblshortlistfolders a left outer join tblshortlistedusers b on a.folderid=b.folderid 
    where a.userid = 50
    group by a.folderid,a.foldername
    

    现在我想在此结果中再添加一列,其中每行的值将如下所示

    1. 如果folderid在tblshortlistedusers userid中包含49 值='是'
    2. 如果folderid在tblshortlistedusers userid中不包含49 那么值='没有'
    3. 但是当我以某种方式应用此条件时,列计数不会显示正确的值,并且结果不会按文件夹分组。多个行显示在同一文件夹的结果中。

      如何获取所需的表格结果(folderid,foldername,totalcount,' newcolumn')

2 个答案:

答案 0 :(得分:1)

如果您展示了您尝试过的查询,会有所帮助。我想这可能会做你想要的:(来自记忆)

select a.folderid,a.foldername,count(b.ShortlistedUserId) as [count]
,case when count(case when b.ShortlistedUserId=49 then 1 else null end)>0 then 'Yes' else 'No' end as [NewColumn]
from tblshortlistfolders a left outer join tblshortlistedusers b on a.folderid=b.folderid 
where a.userid = 50
group by a.folderid,a.foldername

修改 也许我误解了你的数据。以下是一些包含一些示例数据的完整示例:

use tinker -- or whatever test db you have.

CREATE TABLE tblshortlistfolders (
 folderid     INT NOT NULL primary key
,foldername   NVARCHAR(255)
,userid       INT NOT NULL
);

CREATE TABLE tblshortlistedusers (
 folderid            INT NOT NULL
,shortlisteduserid   INT NOT null
,constraint fk_tblshortlistedusers_folderid foreign key (folderid) references [dbo].[tblshortlistfolders]
);

INSERT INTO tblshortlistfolders VALUES ( 100 , 'one'   ,  1); -- created by user 1
INSERT INTO tblshortlistfolders VALUES ( 200 , 'two'   , 50); -- created by user 50
INSERT INTO tblshortlistfolders VALUES ( 300 , 'three' , 50); -- created by user 50
INSERT INTO tblshortlistfolders VALUES ( 400 , 'four'  , 49); -- created by user 49

INSERT INTO tblshortlistedusers VALUES ( 100,  1); -- shortlisted by 50
INSERT INTO tblshortlistedusers VALUES ( 100, 50); -- shortlisted by 01 too
INSERT INTO tblshortlistedusers VALUES ( 200, 50); -- shortlisted by 50
INSERT INTO tblshortlistedusers VALUES ( 200, 49); -- shortlisted by 49 too
INSERT INTO tblshortlistedusers VALUES ( 300, 50); -- shortlisted by 50
INSERT INTO tblshortlistedusers VALUES ( 300, 50); -- shortlisted by  1 too

SELECT a.folderid,a.foldername,COUNT(b.ShortlistedUserId) AS [COUNT]
,CASE WHEN COUNT(CASE WHEN b.ShortlistedUserId=49 THEN 1 ELSE NULL END)>0 THEN 'Yes' ELSE 'No' END AS [NewColumn]
FROM tblshortlistfolders a LEFT OUTER JOIN tblshortlistedusers b ON a.folderid=b.folderid 
WHERE a.userid = 50
GROUP BY a.folderid,a.foldername

这就是我理解你的问题。也许你可以给出你想要的例子吗?

答案 1 :(得分:0)

select 
    a.folderid,
    a.foldername,
    count(b.ShortlistedUserId) as [count],
    case when b.userid = 49 then 'yes' else 'no' end as [newcolumn]
from 
    tblshortlistfolders a 
    left outer join tblshortlistedusers b on a.folderid=b.folderid 
where a.userid = 50
group by a.folderid,a.foldername