0

这是我之前的问题Concatenate 2 rows in a complex SQL query的后续。

这是我从这个问题中得到的:

PARAMETERS [CurrAxe] TEXT ( 255 ), [CurrOTP] TEXT ( 255 ), [CurrClient] TEXT (
255 ), [StartDate] DATETIME, [EndDate] DATETIME;

SELECT q.Projet, *
FROM   (faitssaillants f
LEFT JOIN employes e
ON f.utilisateur = e.cip)
INNER JOIN (
SELECT s1.otp,
     [s1].[valeur] & "," & [s2].[valeur] AS Projet
FROM   (
  SELECT otp, valeur
  FROM   tb_sommaire
  WHERE  [variable] = 'TitreMandat') AS s1
INNER JOIN (
   SELECT otp, valeur
   FROM   tb_sommaire 
   WHERE  [variable] = 'NomInstallation') AS s2
ON s1.otp = s2.otp) q
ON f.otp = q.otp
WHERE f.otp  = [currotp] 
AND f.client LIKE [currclient] 
AND f.axe LIKE [curraxe] 
AND Datevalue([dateinsertion]) 
    Between [startdate] And [enddate] 
ORDER  BY f.dateinsertion DESC; 

如果我想用 [variable] = 'something else' 而不是 'TitreMandat' 或 'NomInstallation' 添加另一行(我们将其命名为 s3)怎么办?是否可以将所有 tb_sommaire.variable 作为字段并将 tb_sommaire.valeur 作为 tb_sommaire.otp = faitssaillants.otp 的值?它可能也会解决我未来的问题。

4

1 回答 1

0

我会在包括交叉表在内的两个查询中执行您的第一个解决方案。第一个名为 qry1 SQL 的查询:

SELECT 
  f.index, f.project, f.axe, f.client, f.otp, s.variable, s.valeur, 
  f.FaitSaillant, f.dateInsertion, f.Utilisateur, e.empname
FROM 
  (FaitsSaillants AS f INNER JOIN 
  tb_SOMMAIRE AS s ON 
  f.otp = s.otp) LEFT JOIN 
  employes AS e ON 
  f.Utilisateur = e.cip;

您可以将您的WHERE条件添加到此查询中。

第二个查询,交叉表一个:

TRANSFORM First(qry1.valeur) AS FirstOfvaleur
SELECT qry1.index, qry1.project, qry1.axe, qry1.client, qry1.otp
FROM qry1
GROUP BY qry1.index, qry1.project, qry1.axe, qry1.client, qry1.otp
PIVOT qry1.variable;

然后你可以valeur为每个连接 svariable

于 2012-11-15T16:36:10.613 回答