Quantcast
Channel: sql-server – Tech ABC to XYZ
Viewing all articles
Browse latest Browse all 30

count query with joins in sql

$
0
0

count query with joins in sql

Asked By: user3134694
Originally Asked On: 2014-01-05 18:38:16
Asked Via: stackoverflow

I try to create a count query in SQL, and I try to show notifications to supervisor, manager and director when any users uploaded new documents in asp.net

Query

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'

but when I execute it, the following error occurs

Msg 4104, Level 16, State 1, Procedure countdocuments, Line 7
The multi-part identifier “dbo.Designation.DesigID” could not be bound.
Msg 4104, Level 16, State 1, Procedure countdocuments, Line 5
The multi-part identifier “Designation.DesigID” could not be bound.

OK this is my designation table

DesigID   DesigType
  1       SuperVisor
  2       Manager
  3       Director
  4       BasicUsers

and this is documentinfo table

DocID  DocDescrit  DocName UploadedDate Uploadfile DocTypeID DepID ApproveID UploadedBy UserID
32  asp.net codescomputer 2013-12-30 22:30:00.623    details.docx   1   2   1amna   24

and this is userss table

UserID UserName Password UserTypeID DepID CreateDate Email PhoeNumber DesigID
21  john    abc 2   NULL    NULL    2013-12-02 22:01:03.903 NULL    abc@hotmail.com 12313   4

and when I try this query

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

when I try to execute like with @depid = 4 this show me like this and I don’t understand where is 4 coming from..

Cnt  DesigID
 4     4

He received 2 answers
without selecting any answers.

If the selected answer did not help you out, the other answers might!

All Answers For: count query with joins in sql

M.Ali’s answer to

count query with joins in sql

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

You have different Table Names in you JOIN clause and ON clause. I have tried to fix it assuming you have DesigID column in your DocumentInfo table.

DevelopmentIsMyPassion’s answer to

count query with joins in sql

Looking at your comment try following

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

Or try below

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

Of course, you should really check out the original question.

The post count query with joins in sql appeared first on Tech ABC to XYZ.


Viewing all articles
Browse latest Browse all 30

Trending Articles