c# - ORDER BY, SELECT with UNION 排序难度

标签 c# sql asp.net sql-server

我在 SQL 请求中使用关键字 UNION 有困难, 我需要按 SELECT 未显示的列对结果进行排序,

    DECLARE @search0 varchar(30);

SET @search0 = 'joll'


SELECT c.CdCnd AS n, FORMAT(c.ModifieLe, 'd', 'fr-FR') AS Modifié, c.Nom+' '+c.prenom AS 'Nom Complet', c.TelMobile AS 'Tel. Mob', c.SuiviPar AS 'Suivi par', a.LibAction AS 'à faire', c.Remuneration AS Rémunération, p.LibPrio AS Priorité, c.disponibilite AS Disponibilité, c.MotCleTech AS MCTech, c.MotCleFct AS MCFonc, c.MotCleEnt AS MCEnt, c.Details AS 'Détails', c.DateDispo AS DTDispo, c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a 
    ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p 
    ON c.CdPrio = p.CdPrio  
WHERE  (  ( Nom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR prenom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR MotCleTech LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR MotCleFct LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR MotCleEnt LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  )  ) 

    UNION 

SELECT c.CdCnd AS n, FORMAT(c.ModifieLe, 'd', 'fr-FR') AS Modifié, c.Nom+' '+c.prenom AS 'Nom Complet', c.TelMobile AS 'Tel. Mob', c.SuiviPar AS 'Suivi par', a.LibAction AS 'à faire', c.Remuneration AS Rémunération, p.LibPrio AS Priorité, c.disponibilite AS Disponibilité, c.MotCleTech AS MCTech, c.MotCleFct AS MCFonc, c.MotCleEnt AS MCEnt, c.Details AS 'Détails', c.DateDispo AS DTDispo, c.Mobilite AS Mobilité
FROM Candidat c
LEFT JOIN TypAction a 
    ON c.CdAction = a.CdAction
LEFT JOIN TypPriorite p 
    ON c.CdPrio = p.CdPrio  
WHERE  ( 1 = 2  OR  c.CdCnd = '3' OR  c.CdCnd = '48' OR  c.CdCnd = '16' )

ORDER BY Modifié DESC

在这里,我需要使用列 c.ModifieLe 按 DateTime 对我的所有结果进行排序, 但是,由于 UNION 限制,我可以在不显示它的情况下使用此列,并且我需要在“fr-FR”中格式化我的日期,

因此,我使用“Modifié”对结果进行排序,但这种排序类似于 varchar 类型...

我需要在法国文化中格式化我的 DateTime 并使其保持日期类型。

感谢所有帮助。

最佳答案

首先应用 UNION,然后对它们进行排序:

;WITH T AS
(
    SELECT 
        c.CdCnd AS n, 
        c.ModifieLe, 
        c.Nom+' '+c.prenom AS 'Nom Complet', 
        c.TelMobile AS 'Tel. Mob', 
        c.SuiviPar AS 'Suivi par', 
        a.LibAction AS 'à faire', 
        c.Remuneration AS Rémunération, 
        p.LibPrio AS Priorité, 
        c.disponibilite AS Disponibilité, 
        c.MotCleTech AS MCTech, 
        c.MotCleFct AS MCFonc, 
        c.MotCleEnt AS MCEnt, 
        c.Details AS 'Détails', 
        c.DateDispo AS DTDispo, 
        c.Mobilite AS Mobilité
    FROM Candidat c
    LEFT JOIN TypAction a 
        ON c.CdAction = a.CdAction
    LEFT JOIN TypPriorite p 
        ON c.CdPrio = p.CdPrio  
    WHERE  (  ( Nom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR prenom LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR MotCleTech LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR MotCleFct LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  OR MotCleEnt LIKE '%' + @search0 + '%' COLLATE SQL_Latin1_General_Cp437_CI_AI  )  ) 

    UNION 

    SELECT 
        c.CdCnd AS n, 
        c.ModifieLe, 
        c.Nom+' '+c.prenom AS 'Nom Complet', 
        c.TelMobile AS 'Tel. Mob', 
        c.SuiviPar AS 'Suivi par', 
        a.LibAction AS 'à faire', 
        c.Remuneration AS Rémunération, 
        p.LibPrio AS Priorité, 
        c.disponibilite AS Disponibilité, 
        c.MotCleTech AS MCTech, 
        c.MotCleFct AS MCFonc, 
        c.MotCleEnt AS MCEnt, 
        c.Details AS 'Détails', 
        c.DateDispo AS DTDispo, 
        c.Mobilite AS Mobilité
    FROM Candidat c
    LEFT JOIN TypAction a 
        ON c.CdAction = a.CdAction
    LEFT JOIN TypPriorite p 
        ON c.CdPrio = p.CdPrio  
    WHERE  ( 1 = 2  OR  c.CdCnd = '3' OR  c.CdCnd = '48' OR  c.CdCnd = '16' )
)
SELECT
    [n],    
    FORMAT([ModifieLe], 'd', 'fr-FR') AS Modifié,  
    [Nom Complet], 
    [Tel. Mob], 
    [Suivi par], 
    [à faire], 
    [Rémunération], 
    [Priorité], 
    [Disponibilité], 
    [MCTech], 
    [MCFonc], 
    [MCEnt], 
    [Détails], 
    [DTDispo], 
    [Mobilité]
FROM T ORDER BY CAST(ModifieLe AS DATETIME) DESC

关于c# - ORDER BY, SELECT with UNION 排序难度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40040893/

相关文章:

c# - 使用 linq c# 在值列表中查找第二个最大值

sql - H2 db 中数组文字的语法是什么?

c# - C#如何检查是否已引发异常

javascript window.print() 无法打印完整的网格

asp.net - 如何在代码隐藏文件中的 asp.net 表中插入 <th> 标记?

c# - 从 web api 2 获取值时出现 Json 解析错误

c# - 如何向工厂模式添加新的派生类型?

c# - 更改正在验证的控件的边框或背景颜色?

mysql - 统计不同表数据库的行数

mysql - 插入新行,除非其中一个值与另一行匹配?