0

This is a follow up to my previous question Concatenate 2 rows in a complex SQL query.

Here is what I got from this question:

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; 

What if I would like to add another row (let's name it s3) with [variable] = 'something else' instead of 'TitreMandat' or 'NomInstallation'? Would it be possible to get all the tb_sommaire.variable as fields and tb_sommaire.valeur as values where tb_sommaire.otp = faitssaillants.otp? It would probably fix my future problems too.

1 Answer 1

0

I would do your first solution in two queries including a crosstab. First query named 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;

You can add your WHERE criteria to this query.

2nd query, the crosstab one:

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;

then you can concatenate the valeurs for each variable

Sign up to request clarification or add additional context in comments.

2 Comments

I have to read a little bit more about TRANSFORM and PIVOT. I can't test it yet, but thank you.
I always use the crosstab wizard and then modify it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.