简体   繁体   中英

How to count values from one table and update another table on same server in SQL Server?

I have two tables in the same server

  1. Attendance table
  2. Payroll table

How to count present/absent from attendance table in (employee database) and update that count info payroll table in (payroll database) on the same server?

Please help me to solve this problem.

Attendance table:

在此处输入图片说明

Payroll table:

在此处输入图片说明

Try this

;WITH X
AS(
    SELECT
         EmpId
        ,COUNT(EntryType) 'P_Count'
    FROM
        dbo.Attendance
    WHERE
        EntryType = 'P'
    GROUP BY
        EmpId
)
UPDATE
    P
SET
    P.Presents  = X.P_Count
FROM
    dbo.PayRoll P
INNER JOIN
    X       A   ON A.EmpId = P.EmpId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM