sql 遊標

 --C7:檢測數據
  if @strType = 'C7'
  begin
    --begin
    --alter table dq_項目信息 add 底盤檢查檢驗員 varchar(30)
    --end
---更新DQ項目信息
    begin
    DECLARE @id INT;
    DECLARE @temp VARCHAR(30);
    
    --創建遊標tempCursor,並定義遊標所指向的集合
    DECLARE tempCursor CURSOR 
    FOR (select FID
    from DQ_項目信息 where FID in (select  Max(FID) as FID from DQ_檢測信息 where len(RTrim(底盤檢查檢驗員)) > 1  and 檢測日期 >= @strDateBegin and 檢測日期 <= @strDateEnd  group by 車牌號碼+號牌種類,檢測類別)   );   
    
    OPEN tempCursor;                              --打開遊標
    FETCH NEXT FROM tempCursor INTO @id;          --遊標讀取下一個數據

    WHILE @@fetch_status=0                        --遊標讀取下一個數據的狀態,0表示讀取成功
        BEGIN
            SELECT @temp=底盤檢查檢驗員 FROM DQ_檢測信息 WHERE FID=@id;   --得到本次循環的id,並查詢id對應的name,賦值到@temp變量中
            update DQ_項目信息 set 底盤檢查檢驗員= @temp where FID = @id   --更新DQ_項目信息表 底盤檢查檢驗員 數據
            FETCH NEXT FROM tempCursor INTO @id;                    --繼續用遊標讀取下一個數據
        END
    close tempCursor  --關閉遊標
    deallocate tempCursor   --釋放遊標
    end
------結束更新DQ項目信息
     --01 查找數據   
    select top 100 percent 底盤檢查檢驗員 AS DJY,
    SUM(1) as Quantity,
    SUM(case when (底盤檢查_CS-底盤檢查不合格次數) = '1'then 1 else 0 end) as Qualified, 
    SUM(case when 底盤檢查_CS = '1' and 底盤檢查不合格次數 = '0' then 1 else 0 end) as Qualified1,
    SUM(case when 底盤檢查_CS = '1' then 1 else 0 end) as Quantity1,
    SUM(case when 底盤檢查_CS = '2' and 底盤檢查不合格次數 = '1'then 1 else 0 end) as Qualified2,
    SUM(case when 底盤檢查_CS = '2'  then 1 else 0 end) as Quantity2,
    SUM(case when 底盤檢查_CS = '3' and 底盤檢查不合格次數 = '2' then 1 else 0 end) as Qualified3,
    SUM(case when 底盤檢查_CS = '3' then 1 else 0 end) as Quantity3,
    SUM(case when Convert(int,底盤檢查_CS) > 3 then 1 else 0 end) as Quantity4 
    into #tempReportStandard_C67
    from DQ_項目信息 where FID in (select  Max(FID) as FID from DQ_檢測信息 where len(RTrim(底盤檢查檢驗員)) > 1  and 檢測日期 >= @strDateBegin and 檢測日期 <= @strDateEnd  group by 車牌號碼+號牌種類,檢測類別) 
    group by 底盤檢查檢驗員
    
    --02 合併數據
    select DJY AS DJY,
    Quantity,Qualified,Qualified1,
    Quantity1,
    case when Quantity1 = 0 then '-' else convert(varchar(10),Qualified1*10000/Quantity1 *0.01) end as RateQualified1,
    Quantity2 as QuantityRe1,
    case when Quantity2 = 0 then '-' else convert(varchar(10),Qualified2*10000/Quantity2 *0.01) end as RateQualified2,
    Quantity3 as  QuantityRe2,
    case when Quantity3 = 0 then '-' else convert(varchar(10),Qualified3*10000/Quantity3 *0.01) end as RateQualified3,
    Quantity4 as QuantityRe3,
    case when Quantity4 = 0 then '-' else convert(varchar(10),(Qualified-Qualified1-Qualified2-Qualified3)*10000/Quantity4*0.01) end as RateQualified4
    from #tempReportStandard_C67 
    
  end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章