我会尽力解释,但如果不清楚,请告诉我。英语不是我的第一语言。
我需要一些查询帮助,作为插入记录集的条件。
首先,我有一个db表EmployeeProviders。
在其中一个存储过程中,我根据某些条件重新计算学分。在这种情况下,记录的数量设置为3,但可以更多或更少。
如果在重新计算后,我获得与EmployeeProviders中生效日期相同的数字或信用,我不需要插入这些值。 EmployeeProviders可能包含按生效日期分隔的每个员工的几组记录。
对我来说,困难在于构建一个查询,它将逐个检查记录,但在这种情况下以三个为一组。如果其中一条记录不匹配,我需要插入所有三条记录。如果所有这些都相同,我不会插入任何记录。
declare @StartDate datetime, @employee_id int
select @StartDate = '2013-07-01', @employee_id = 3465
例如,这里是填充了值
的db表 DECLARE @EmployeeProviders TABLE (
ident_id int IDENTITY,
employee_id int,
id int,
plan_id int,
credits decimal(18,5),
effective_date datetime
)
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
VALUES (18753, 23, 0.00000, '2013-06-01')
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
VALUES (3465, 18753, 15, 0.00000, '2013-06-01')
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
VALUES (3465, 18753, 16, 60.00, '2013-06-01')
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
VALUES (3465, 18753, 23, 0.00000, '2013-07-01')
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
VALUES (3465, 18753, 15, 0.00000, '2013-07-01')
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
VALUES (3465, 18753, 16, 81.580, '2013-07-01')
SELECT * FROM @EmployeeProviders WHERE plan_id = 18753 and datediff(dd,effective_date,@StartDate) = 0
这是存储过程中的临时表。它在caclulation过程中得到更新
DECLARE @Providers TABLE (
id int,
plan_id int,
credits decimal(18,5)
)
INSERT INTO @Providers (plan_id, id, credits)
VALUES (18753, 23, 0.00000)
INSERT INTO @Providers (plan_id, id, credits)
VALUES (18753, 15, 0.00000)
INSERT INTO @Providers (plan_id, id, credits)
VALUES (18753, 16, 81.580)
SELECT * FROM @Providers
此临时表中的所有更新金额与db表EmployeeProviders中的金额相同,因此,我不需要插入新的记录集 我怎么能做一个可以像这样的条件的查询 如果不是EXISTS() 或者只是做 INSERT EmployeeProviders()...... SELECT ... FROM @Providers ,,, - 如果值与EmployeeProviders中的值不同,将返回3条记录的查询
另一种情况,@ Providers.credits = 65所以,因为与EmployeeProviders.credits相比,对于id = 16,金额被更改了。我将向EmployeeProvider表添加新的3条记录
DECLARE @Providers TABLE (
id int,
plan_id int,
credits decimal(18,5)
)
INSERT INTO @Providers (plan_id, id, credits)
VALUES (18753, 23, 0.00000)
INSERT INTO @Providers (plan_id, id, credits)
VALUES (18753, 15, 0.00000)
INSERT INTO @Providers (plan_id, id, credits)
VALUES (18753, 16, 65.00)
SELECT * FROM @Providers
提前谢谢你,
麦
答案 0 :(得分:1)
如果我正确理解您的问题,您的问题可以通过以Except
if exists(
select ID, plan_id , credits from @Providers
except
SELECT id, plan_id, credits FROM @EmployeeProviders WHERE plan_id = 18753 and datediff(dd,effective_date,@StartDate) = 0
)
答案 1 :(得分:0)
我会捅它。在存储过程中尝试这个:
if (EXISTS(select 3465,pr.plan_id,pr.id,epr.credits,effective_date from
providers pr left join @EmployeeProviders epr on
pr.id = epr.id and pr.plan_id = epr.plan_id and pr.credits = epr.credits and effective_date = '2013-07-01'
where epr.credits is NULL ))
INSERT INTO @EmployeeProviders (employee_id, plan_id, id, credits, effective_date)
select 3463,plan_id,id,credits,'2013-07-01' from @Providers
中查看
答案 2 :(得分:0)
使用类似以下查询的内容。我把它概括为你。核实 。
WITH TEMP
AS
(
SELECT A.employee_id, A.plan_id, A.id, A.credits, A.effective_date,CASE WHEN A.CREDITS = B.CREDITS THEN 1 ELSE 0 END AS SAME
FROM @EmployeeProviders as A INNER JOIN @Providers as B ON A.ID = B.ID AND
A.PLAN_ID = B.PLAN_ID AND A.effective_date = '2013-07-01'
)
SELECT *
FROM TEMP AS A INNER JOIN
(SELECT EMPLOYEE_ID , PLAN_ID , SUM(SAME) AS TOTAL , COUNT(*) AS CNT FROM TEMP GROUP BY EMPLOYEE_ID , PLAN_ID ) AS B
ON A.EMPLOYEE_ID = B.EMPLOYEE_ID AND A.PLAN_ID = B.PLAN_ID
WHERE B.TOTAL != B.CNT