如何将数据分为两列

时间:2018-12-18 11:40:50

标签: sql sql-server tsql sql-server-2008-r2

请考虑以下表格结构和示例数据-

"C:\Program "

现在,我想将列EmpID InputDateTime StatusINOUT ------------------------------------- 1 2018-05-26 08:44 1 1 2018-05-26 08:44 2 2 2018-05-28 08:44 1 2 2018-05-28 12:44 2 1 2018-05-21 08:44 1 1 2018-05-21 10:44 2 2 2018-05-23 08:44 1 2 2018-05-23 08:44 2 分为两列,即InputDateTimeINTIME(1)。后面的逻辑是,OUTTIME(2)为1的日期将为StatusInOut,而InTime为2的日期将为StatusInOut

预期的输出格式如下所示:

OUTTIME(2)

这是我到目前为止尝试过的

Empid   INTIME(1)          OUTIME(2)
--------------------------------------------
1      2018-05-26 08:44    2018-05-26 08:44
2      2018-05-28 08:44    2018-05-28 12:44
1      2018-05-21 08:44    2018-05-21 10:44
2      2018-05-23 08:44    2018-05-23 08:44

4 个答案:

答案 0 :(得分:3)

您需要<RadioButton Name="RadioButtonDhcpConfig" Grid.Row="1" Grid.Column="1" Content="DHCP" VerticalAlignment="Center" Margin="5"> <RadioButton.Style> <Style TargetType="RadioButton"> <Setter Property="IsChecked" Value="True"/> <Style.Triggers> <DataTrigger Binding="{Binding DhcpEnabled}" Value="False"> <Setter Property="IsChecked" Value="False"/> </DataTrigger> </Style.Triggers> </Style> </RadioButton.Style> </RadioButton> <!--Row 2--> <RadioButton Name="RadioButtonManualConfiguration" Grid.Row="2" Grid.Column="1" Content="Manual Configuration:" VerticalAlignment="Center" Margin="5"> <RadioButton.Style> <Style TargetType="RadioButton"> <Setter Property="IsChecked" Value="False"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=RadioButtonDhcpConfig, Path=IsChecked}" Value="False"> <Setter Property="IsChecked" Value="True"/> </DataTrigger> </Style.Triggers> </Style> </RadioButton.Style> </RadioButton> 并使用它们之间的差异进行条件汇总,这也称为row_number()问题:

Gaps and Islands

编辑::如果您想在select empid, max(case when sStatus = 1 then intTime end) as INTIME, max(case when sStatus = 2 then intTime end) as OUTIME from (select t.*, row_number () over ( order by inttime) as seq1, row_number () over (partition by empid order by inttime) as seq2 from #tempStatus t ) t group by empid, (seq1-seq2); 不存在时显示OutTime,则可以使用子查询:

InTime

答案 1 :(得分:1)

这是将结束时间与开始时间匹配的问题:

const wsedit = new vscode.WorkspaceEdit();
const wsPath = vscode.workspace.workspaceFolders[0].uri.fsPath; // gets the path of the first workspace folder
const filePath = vscode.Uri.file(wsPath + '/hello/world.md');
vscode.window.showInformationMessage(filePath.toString());
wsedit.createFile(filePath, { ignoreIfExists: true });
vscode.workspace.applyEdit(wsedit);
vscode.window.showInformationMessage('Created a new file: hello/world.md');

Demo on DB<>Fiddlesome tests

答案 2 :(得分:1)

尝试此PIVOT 样本数据

IF OBJECT_ID('Tempdb..#tempStatus')IS NOT NULL
DROP TABLE #tempStatus

CREATE TABLE #TEMPSTATUS (EMPID INT, INTTIME DATETIME, SSTATUS INT)    
INSERT INTO #TEMPSTATUS    
VALUES(1, '2018-05-26 08:44', 1),    
    (1, '2018-05-26 08:44', 2),    
    (2, '2018-05-28 08:44', 1),    
    (2, '2018-05-28 12:44', 2),        
    (1, '2018-05-21 08:44', 1),    
    (1, '2018-05-21 10:44', 2),        
    (2, '2018-05-23 08:44', 1),    
    (2, '2018-05-23 08:44', 2)    

Sql脚本

SELECT Empid,[INTIME(1)],[OUTIME(2)]
FROM
(
    SELECT EmpId,intTime, CASE WHEN sStatus=1 THEN 'INTIME(1)' 
                       WHEN sStatus=2 THEN 'OUTIME(2)'  END INOutTimes
    FROM #tempStatus
    ) AS SRC
    PIVOT 
    (MAX(intTime) FOR INOutTimes IN ([INTIME(1)],[OUTIME(2)])
    ) AS PVT
UNION ALL
SELECT Empid,[INTIME(1)],[OUTIME(2)]
FROM
(
    SELECT EmpId,intTime, CASE WHEN sStatus=1 THEN 'INTIME(1)' 
                       WHEN sStatus=2 THEN 'OUTIME(2)'  END INOutTimes
    FROM #tempStatus
    ) AS SRC
    PIVOT 
    (MIN(intTime) FOR INOutTimes IN ([INTIME(1)],[OUTIME(2)])
    ) AS PVT

结果

Empid    INTIME(1)                   OUTIME(2)
---------------------------------------------------------------
1       2018-05-26 08:44:00.000     2018-05-26 08:44:00.000
2       2018-05-28 08:44:00.000     2018-05-28 12:44:00.000
1       2018-05-21 08:44:00.000     2018-05-21 10:44:00.000
2       2018-05-23 08:44:00.000     2018-05-23 08:44:00.000

答案 3 :(得分:0)

我尝试过使用内部联接更新后找出解决方案,如下所示。在这里,我跳过了InTime不存在的日期和行。

GridView

这是演示-Spltting Column