我有这个丑陋的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'
这给了我这个结果:
我需要复制此查询几次,并将Nolise
之类的简单关键字更改为Scholar
等等。
我知道如果我只是复制它,我会得到不同的请求:
我想要的是一个包含所有内容的数组,就好像它是一个SELECT
而不改变我的整个请求。有没有简单的方法来达到这个目标?
编辑:
假设我想添加另一个列,这个列必须检查另一个日期条件。
Hebdomadaire,这意味着每周在英语测试,看看它是否在本周:
Where Plaintes.Date >= @Sunday AND Plaintes.Date < DATEADD(Day, 7, @SUNDAY)
我会添加一个测试此列的anual列:
Where Plaintes.Date >= 2012/01/01
如何进行查询,以便每周仅对第一个条件进行测试?
答案 0 :(得分: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
答案 1 :(得分: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')