基于其他名为TableCodes的表,该表有3条记录
Code
1508
1509
1510
我从查询中得到了这个结果,我是一个临时表......
CapacityDate InstallerCode WorkAreadID FoxtelCodeID value
2/8/12 BAW 7 1508 3
3/8/12 BAW 7 1508 1
4/8/12 BAW 7 1509 1
我需要在同一个表中插入缺少基于TableCodes且值为0的其他记录
我想要这个
CapacityDate InstallerCode WorkAreadID FoxtelCodeID value
2/8/12 BAW 7 1508 3
2/8/12 BAW 7 1509 0
2/8/12 BAW 7 1510 0
3/8/12 BAW 7 1508 1
3/8/12 BAW 7 1509 0
3/8/12 BAW 7 1510 0
4/8/12 BAW 7 1508 1
4/8/12 BAW 7 1509 0
4/8/12 BAW 7 1510 0
我正在使用以下代码
Insert into #TmpAdjustmentTable (
CapacityDate, InstallerID, InstallerCode, InstallerName,
bThisIsAnAdjustment, sStatusOfCapacity, WorkAreadID, WORKAREA,
CapacityState, FoxtelCodeID, SINTERNALLABELFORCAPACITY,
SLABELFORCAPACITY, CapacityUnits, Capacity, MaxJobs, CommentsForDay
)
select
ta.CapacityDate, ta.InstallerID, ta.InstallerCode, ta.InstallerName,
1, ta.sStatusOfCapacity, ta.WorkAreadID, ta.WORKAREA,
ta.CapacityState, FoxtelCodes.SYSID, FoxtelCodes.SINTERNALSHORTLABELFORCAPACITY,
FoxtelCodes.SLABELFORCAPACITY, 0, 0, 0, ta.CommentsForDay
from #TmpAdjustmentTable ta
inner JOIN FoxtelCodes
On FoxtelCodes.SYSID <> ta.FoxtelCodeID
and FoxtelCodes.BCOLLECTTHISFORCAPACITY = 1
但我得到了......
CapacityDate InstallerCode WorkAreadID FoxtelCodeID value
2/8/12 BAW 7 1508 3
2/8/12 BAW 7 1509 0
2/8/12 BAW 7 1510 0
3/8/12 BAW 7 1508 1
3/8/12 BAW 7 1508 0
3/8/12 BAW 7 1509 0
3/8/12 BAW 7 1509 0
3/8/12 BAW 7 1510 0
3/8/12 BAW 7 1510 0
4/8/12 BAW 7 1508 1
4/8/12 BAW 7 1508 0
4/8/12 BAW 7 1509 0
4/8/12 BAW 7 1509 0
4/8/12 BAW 7 1510 0
4/8/12 BAW 7 1510 0
答案 0 :(得分:0)
嗨检查下面的查询以获得所请求格式的输出。
- 这里我试着在不使用while循环的情况下获取输出但是我无法取得成功。我会尝试找到这个意思,同时你可以检查这个并希望它会有用。
Declare @min int,@max int
SELECT @min=MIN(idn),@max=MAX(idn) from Results
SELECT @min,@max
Declare @tbl_out table (CapacityDate datetime,InstallerCode varchar(100),WorkAreadID int,FoxtelCodeID int,value int)
While(@min<=@max)
begin
;With CTE(CapacityDate,InstallerCode,WorkAreadID,FoxtelCodeID,value) AS
(SELECT CapacityDate,InstallerCode,WorkAreadID,FoxtelCodeID,value from Results where idn = @min)
,CTE1(code,value) AS
(SELECT C.code,isnull(T.Value,0) FROM Codes C LEFT OUTER JOIN CTE T on C.code=T.FoxtelCodeID)
insert into @tbl_out
select r.CapacityDate,r.InstallerCode,r.WorkAreadID,c.code,c.value from CTE1 c,CTE r
SET @min+=1
End
select * from @tbl_out
select * from Results
答案 1 :(得分:0)
嘿这是没有while循环的最新版
Select CapacityDate,InstallerCode,WorkAreadID,code,0 value from Results join Codes
on FoxtelCodeID<>code
union all
Select CapacityDate,InstallerCode,WorkAreadID,FoxtelCodeID,value from Results
order by CapacityDate, value desc
答案 2 :(得分:0)
获取已存在于临时表中的CapacityDate
,InstallerCode
,WorkAreadID
的所有不同组合,将它们与TableCodes
中的代码交叉连接,然后过滤掉临时表已经匹配的行与相同的日期和代码匹配。在SQL中,它可能看起来像这样:
INSERT INTO #Table (
CapacityDate,
InstallerCode,
WorkAreadID,
FoxtelCodeID,
value
)
SELECT
d.CapacityDate,
d.InstallerCode,
d.WorkAreadID,
c.Code,
0
FROM (
SELECT DISTINCT
CapacityDate,
InstallerCode,
WorkAreadID
FROM #Table
) d
CROSS JOIN TableCodes c
LEFT JOIN #Table t ON d.CapacityDate = t.CapacityDate AND c.Code = t.FoxtelCodeID
WHERE t.FoxtelCodeID IS NULL