Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the query i am trying to achieve a count particularly for each date but how do i Achieve it?

What I have tried:

select [dbo].[GetCheckInCheckOut](11867 , format (Cast(StartDateTime as Date),'yyyy-MM-dd'))Presence ,	UserID ,Cast(StartDateTime as Date)as StartDateTime,(select VendorId,count(*) from tblVisits where CheckInDateTime >= '2022-02-01' and CheckOutDateTime <= '2022-02-25'  and CreatedBy=11867 group by VendorID)VisitCount from tblAttendance as a   
left join tblVisits as v on a.UserID =v.CreatedBy
where 
StartDateTime between '2022-02-01' and  '2022-02-25' 

and
UserID =11867 Group by UserID ,Cast(StartDateTime as Date)
Posted
Updated 24-Feb-22 20:06pm

It would help if you reformatted your SQL so it's readable, and capitalize SQL keywords:
SQL
SELECT [dbo].[GetCheckInCheckOut](11867    <--- syntax error
    , FORMAT(CAST(StartDateTime AS Date), 'yyyy-MM-dd'))Presence  <--- syntax error
    , UserID
    , CAST(StartDateTime AS Date) AS StartDateTime
    , (
        SELECT VendorId
             , COUNT(*) 
        FROM tblVisits 
        WHERE CheckInDateTime >= '2022-02-01' 
          AND CheckOutDateTime <= '2022-02-25'
          AND CreatedBy = 11867
          GROUP BY VendorID
       )VisitCount FROM tblAttendance AS a         <--- and yet another syntax error
LEFT JOIN tblVisits AS v 
ON a.UserID = v.CreatedBy
WHERE StartDateTime BETWEEN '2022-02-01' AND '2022-02-25' 
  AND UserID = 11867 
GROUP BY UserID, CAST(StartDateTime AS Date)

I have no idea what you're trying to do, but you've got some cleaning up to do just to make your SQL readable, and therefore debuggable.
 
Share this answer
 
v2
Comments
Maciej Los 25-Feb-22 1:43am    
Wow! You have found 3 syntax errors in such of ugly formatted SQL code. 5ed!
As Dave already mentioned, the format of your SQL code is not readible and thus contains several errors.

The error message is quite obvious. Your subquery returns more then one column. See:
How to Fix “Only one expression can be specified in the select list…” in SQL Server[^]

Now, take a look at your sql code:
SQL
select [dbo].[GetCheckInCheckOut](11867 , format (Cast(StartDateTime as Date),'yyyy-MM-dd'))Presence ,	
    UserID ,
    Cast(StartDateTime as Date) as StartDateTime,
    (
      select VendorId,count(*)
      from tblVisits
      where CheckInDateTime >= '2022-02-01' and CheckOutDateTime <= '2022-02-25'  and 
          CreatedBy=11867
      group by VendorID
   ) VisitCount  -- <-- this subquery returns more than 1 column: VendorId And the result of COUNT(*)
from tblAttendance as a   
left join tblVisits as v on a.UserID =v.CreatedBy
where StartDateTime between '2022-02-01' and  '2022-02-25' and
UserID =11867 Group by UserID, Cast(StartDateTime as Date
)


On the other hand, you should start learning about variables[^]. In your query the value of 11867 is entered 3 times: at the beginning, in the middle and at the end. Replace it with variable!

I also strongly recommend to rethink your query. Start with smaller pieces. When you get them working, try to connect them each other.

You can share example data at: db<>fiddle[^], then copy the link and paste it into your question. Then, we'll be able to inspect your sql code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900