0

基于具有 3 条记录的名为 TableCodes 的其他表

Code
1508
1509
1510

我从我的查询 ia temporal table 中得到了这个结果....

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

我需要在同一个表中插入基于值 = 0 的 TableCodes 缺失的其他记录

我要这个

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
4

3 回答 3

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
于 2012-07-04T12:26:38.540 回答
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
于 2012-07-04T13:03:34.673 回答
0

获取已在临时表中的 , 的所有不同组合,将它们与 中的代码交叉连接,然后CapacityDate过滤掉临时表已经与相同日期和代码匹配的行。在 SQL 中,它可能看起来像这样:InstallerCodeWorkAreadIDTableCodes

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
于 2012-07-04T16:01:27.363 回答