0

我有这个丑陋的SELECT声明:

        Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements) as Hebdomadaire
        FROM Plaintes join PVehicule on Plaintes.IDEvenements = PVehicule.IDEvenements 
        join EvVehicule on PVehicule.IDVehicule = EvVehicule.IDVehicule
        join Classe_Vh on EvVehicule.IDClasse = Classe_Vh.IDClasse
        join Type_Voy on Classe_Vh.IDTYPE_VOYAGE = Type_Voy.IDTYPE_VOYAGE
        Where Plaintes.Date >= @Sunday AND Plaintes.Date < DATEADD(Day, 7, @SUNDAY) AND Description = 'Urbain'

这给了我这个结果:

在此处输入图像描述

我需要复制这个查询几次并更改一个简单的关键字,比如NolisetoScholar等等。

我知道如果我只是复制它,我会收到不同的请求:

在此处输入图像描述

我想要的是一个包含所有内容的数组,就好像它是一个单独的SELECT而不改变我的整个请求一样。有没有简单的方法可以做到这一点?

在此处输入图像描述

编辑:

假设我想添加另一个必须检查另一个日期条件的列。

Hebdomadaire 这意味着每周一次的英语测试,看看它是否在本周:

         Where Plaintes.Date >= @Sunday AND Plaintes.Date < DATEADD(Day, 7, @SUNDAY)

我会添加一个年度专栏来对此进行测试:

         Where Plaintes.Date >= 2012/01/01

我怎样才能进行查询,以便只对第一个条件进行每周测试?

4

2 回答 2

3

使用GROUP BY Description

SELECT
    Description as Véhicule,
    Count(DISTINCT Plaintes.IDEvenements) as Hebdomadaire
FROM Plaintes
JOIN PVehicule ON Plaintes.IDEvenements = PVehicule.IDEvenements 
JOIN EvVehicule ON PVehicule.IDVehicule = EvVehicule.IDVehicule
JOIN Classe_Vh ON EvVehicule.IDClasse = Classe_Vh.IDClasse
JOIN Type_Voy ON Classe_Vh.IDTYPE_VOYAGE = Type_Voy.IDTYPE_VOYAGE
WHERE Plaintes.Date >= @Sunday AND Plaintes.Date < DATEADD(Day, 7, @SUNDAY)
GROUP BY Description
于 2012-06-22T19:06:55.227 回答
2
Select description as Véhicule
    , Count( DISTINCT Plaintes.IDEvenements) as Hebdomadaire
FROM Plaintes join PVehicule on Plaintes.IDEvenements = PVehicule.IDEvenements 
join EvVehicule on PVehicule.IDVehicule = EvVehicule.IDVehicule
join Classe_Vh on EvVehicule.IDClasse = Classe_Vh.IDClasse
join Type_Voy on Classe_Vh.IDTYPE_VOYAGE = Type_Voy.IDTYPE_VOYAGE
Where Plaintes.Date >= @Sunday 
AND Plaintes.Date < DATEADD(Day, 7, @SUNDAY) 
AND Description IN ('Urbain','Scholar','otherthing')
于 2012-06-22T19:07:08.200 回答