Sql 查询仅根据条件从 1 列中获取少量记录,并 show/display 使用 Select 查询单独获取

Sql query to fetch only few records from 1 column based on Condition and show/display it separately using Select query

已编辑:

在我的数据库 table 中,我有 4 列 ID ,Action, NameDateTime 具有以下记录:

Action         Name      DateTime
-------------------------------------
Submit         AA        'date time'
Update         BB        'date time'
Save           CC        'date time'
Match          DD        'date time'
Submit         EE        'date time'
Submit         FF        'date time'
Update         GG        'date time'
Approve        HH        'date time'
Approve        II        'date time'
Update         JJ        'date time'
Match          KK        'date time'
Save           LL        'date time'
Match          MM        'date time'
etc                      'date time'
etc                      'date time'

这些记录可以重复多次。

我想 select 上面 Table 的名称和日期时间具有 Action = Submit 和 Action = Update;使用 select 查询分为两个单独的列(就像我们使用 select 查询获取数据一样)。

示例查询:

SELECT 
    Name as SubmitterName, DateTime AS SubmitterDateTime
FROM 
    [tbl_WorkflowHistory]
WHERE 
    Action = 'Submit' 

SELECT 
    Name as UpdateName, DateTime AS  UpdateDateTime
FROM 
    [tbl_WorkflowHistory]
WHERE 
    Action = 'Update'

唯一的问题是我想同时显示两个查询的记录,但在 4 个单独的列中,如下格式:

需要输出:

SubmitName    SubmitDateTime    UpdateName    UpdateDateTime
-------------------------------------------------------------
    AA        'datetime'          BB          'datetime' 
    EE        'datetime'          GG          'datetime'
    FF        'datetime'          JJ          'datetime'
    etc                           etc

在 "SubmitName" 列下,所有具有 Action=Submit 的名称都应与 SubmitDateTime 列中相应的 DateTime 一起出现。同样,在 "UpdateName" 列下,所有名称都应具有 Action=Update 以及 UpdateDateTime 列中相应的 DateTime。我不知道如何编写此查询,它只是解释我的要求的示例。

并且在 select 像上面的输出记录后,使用 sql 查询,我必须在 RadGrid 中显示它,其中 "SubmitDateTime" 列中的数据必须单独显示(日期分开,时间分开)在 RadGrid 的 2 个不同列中,即 SubmitDate、SubmitTime。对于 "UpdateDateTime" 也类似,即 UpdateDate、UpdateTime;如下所述:

SubmitAction    SubmitDate    SubmitTime    UpdateAction    UpdateDate    UpdateTime
------------------------------------------------------------------------------------ 
Submit          'date'        'time'        Update          'date'        'time'
Submit          'date'        'time'        Update          'date'        'time'
Submit          'date'        'time'        Update          'date'        'time'
etc                                      

我希望我把我的要求说清楚了。请让我知道该怎么做。我正在使用 SQL 服务器数据库。请回复。提前致谢

如果 DateTime 列中的数据是 space 分隔的,那么您可以这样做:

INSERT INTO TABLE_2(SubmitAction,SubmitDate,SubmitTime)
SELECT Action,left(DateTime,locate(' ',DateTime)-1),substr(DateTime,locate(' ',DateTime)+1) FROM TABLE_1;  

如果我正确理解了你的问题,它应该可以工作。如果你需要其他东西,请详细说明你的问题。谢谢

根据我的要求,以下查询工作正常:

 Select * FROM
 ( 
 SELECT RequestID, Action, UserName as SubmitterName , CreatedDate AS  SubmitterDateTime
 FROM [tbl_WorkflowHistory]
 Where Action = 'Submit') as A

 Join

 (SELECT RequestID, Action, UserName as UpdateName , CreatedDate AS  UpdateDateTime
 FROM [tbl_WorkflowHistory]
 Where Action = 'Update') as B 

 ON A.RequestID=B.RequestID

谢谢大家的回复和帮助