Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql中带有连接的计数查询_Sql_Sql Server_Count - Fatal编程技术网

sql中带有连接的计数查询

sql中带有连接的计数查询,sql,sql-server,count,Sql,Sql Server,Count,我尝试在SQL中创建计数查询,并尝试在任何用户在asp.net中上载新文档时向主管、经理和主管显示通知 质疑 ALTER procedure [dbo].[countdocuments] @DepID int as SELECT COUNT(*), Designation.DesigID FROM DocumentInfo INNER JOIN dbo.Userss ON dbo.Userss.DesigID = dbo.Designa

我尝试在SQL中创建计数查询,并尝试在任何用户在asp.net中上载新文档时向主管、经理和主管显示通知

质疑

ALTER procedure [dbo].[countdocuments]
   @DepID int
as
    SELECT 
       COUNT(*),
       Designation.DesigID
    FROM DocumentInfo
    INNER JOIN dbo.Userss ON dbo.Userss.DesigID = dbo.Designation.DesigID
    WHERE 
       Userss.DesigID = 'Finance'
但当我执行它时,会出现以下错误

Msg 4104,16级,状态1,程序文件,第7行 无法绑定多部分标识符dbo.Designation.DesigID。 Msg 4104,16级,状态1,程序文件,第5行 无法绑定多部分标识符Designation.DesigID

好的,这是我的指定表

这是documentinfo表

这是userss表

当我尝试这个查询时

ALTER procedure [dbo].[countdocuments]
    @DepID int
as
BEGIN
    SET NOCOUNT ON;

    SELECT 
        COUNT(*) AS Cnt, Designation.DesigID
    FROM 
        Designation 
    INNER JOIN 
        dbo.Userss ON dbo.Userss.DesigID = dbo.Designation.DesigID
    WHERE 
        Userss.DesigID = @DepID
    GROUP BY 
        Designation.DesigID
END
当我试图用@depid=4执行类似的操作时,这会让我看到这样的结果,我不明白4是从哪里来的

Cnt  DesigID
 4     4

在youjoin子句和ON子句中有不同的表名。假设您在DocumentInfo表中有DesigID列,我已尝试修复它。

查看您的注释,请尝试以下操作

ALTER procedure [dbo].[countdocuments]
@DepID int
as
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) AS Cnt,Designation.DesigID
FROM Designation left join dbo.Userss 
on dbo.Userss.DesigID = dbo.Designation.DesigID
left Join DocumentInfo on Userss.UserId= DocumentInfo.UserId and Designation.DepId = DocumentInfo.DepId
WHERE Userss.DesigID = @DepID
GROUP BY Designation.DesigID

END
或者试试下面

Looking at your comment try following

ALTER procedure [dbo].[countdocuments]
@DepID int
as
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) AS Cnt,Designation.DesigID
FROM DocumentInfo  left Join Userss on DocumentInfo.UserId = Userss.UserId
left Join Designation on DocumentInfo.DepId = Designation.DeptId
and Designation.DesigID = Userss.DesigId
WHERE Userss.DesigID = @DepID
GROUP BY Designation.DesigID

END

+5:哦,没有人回答我……:/这是因为它无法在表指定中找到列名称DesigId。指定表格在哪里?或者,您是想将别名作为指定表放在DocumentInfo中吗?能否提供架构?在documnet信息表中,没有名为desigid的coulmn。desigid仅在Users表中,而在desig表中有两列,一列是desigid,另一列是desigtypein DocumentInfo表中没有一列desigid。那么,您为什么要添加DocumentInfo桌子连接到设计表的位置在哪里?@user3134694用户和设计表之间的关系是什么?当所有用户注册时,他/她的设计将保存在表中..并确定哪个用户属于哪个用户designations@user3134694我在问你可以在用户和指定表之间链接哪一列?我只在用户和指定表中有desigid coulmnTable您和我的答案将不起作用,因为他已经说过他在documentInfo中没有DesigIdtable@user3134694DocumentInfo中是否有任何列引用了名称中的任何列???@user3134694能否更新您的问题,并提供有关所有三个表及其表中列的信息,和表中的外键。
Cnt  DesigID
 4     4
ALTER procedure [dbo].[countdocuments]
@DepID int
as
BEGIN
  SET NOCOUNT ON;
    SELECT COUNT(*) AS Cnt,Designation.DesigID
    FROM DocumentInfo inner join dbo.Userss 
    on dbo.Userss.DesigID = dbo.DocumentInfo.DesigID
    WHERE Userss.DesigID = @DepID
    GROUP BY DocumentInfo.DesigID

END
ALTER procedure [dbo].[countdocuments]
@DepID int
as
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) AS Cnt,Designation.DesigID
FROM Designation left join dbo.Userss 
on dbo.Userss.DesigID = dbo.Designation.DesigID
left Join DocumentInfo on Userss.UserId= DocumentInfo.UserId and Designation.DepId = DocumentInfo.DepId
WHERE Userss.DesigID = @DepID
GROUP BY Designation.DesigID

END
Looking at your comment try following

ALTER procedure [dbo].[countdocuments]
@DepID int
as
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) AS Cnt,Designation.DesigID
FROM DocumentInfo  left Join Userss on DocumentInfo.UserId = Userss.UserId
left Join Designation on DocumentInfo.DepId = Designation.DeptId
and Designation.DesigID = Userss.DesigId
WHERE Userss.DesigID = @DepID
GROUP BY Designation.DesigID

END