0

I have a temporary table like this. enter image description here

Then I'm pivoting that table using following query and output attached.

 SELECT *
 FROM (
  SELECT * from  @tmpTable2
 ) AS SourceTable
 PIVOT
(
max(timec)
FOR eventb
IN (
[Veh Reg Time],[Fitness ok time],[RFID Issue time],[MG-IN],[WB IN],
[MG-OUT],      [WB OUT]
)
) AS PivotTableAlias

enter image description here

In here issue is for a one 'RegNo' there is multiple records. But I want only one row for a one 'RegNo'. How can I achieve this?

Expected out put should be like this. enter image description here

4
  • 1
    i think DISTINCT will help you OR GROUP BY Commented Oct 20, 2015 at 8:47
  • How can I use distinct for this query? Commented Oct 20, 2015 at 8:51
  • ok post your expected output result here Commented Oct 20, 2015 at 8:53
  • expected result attached. Commented Oct 20, 2015 at 8:59

2 Answers 2

1
SELECT *
 FROM (
  SELECT regno,eventb,timec,rfid from  @tmpTable2
 ) AS SourceTable
 PIVOT
(
max(timec)
FOR eventb
IN (
[Veh Reg Time],[Fitness ok time],[RFID Issue time],[MG-IN],[WB IN],
[MG-OUT],      [WB OUT]
)
) AS PivotTableAlias
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT pt.regno, pt.rfid, MAX(vrt), MAX(fot), MAX(rit), MAX(MI), MAX(wi), MAX(Mo), MAX(wo) FROM (
SELECT *
 FROM (
  SELECT * from @temtable2 t
 ) SourceTable
 PIVOT
(
max(timec)
FOR eventb
IN('Veh Reg Time' AS vrt,'Ftness Ok time' AS fot, 'RFID Issue time' AS rit, 'MG-IN' AS mi, 'WB IN' AS wi, 'MG-OUT' AS mo, 'WB OUT' AS wo)
) PivotTableAlias) pt
GROUP BY  pt.regno, pt.rfid

Comments

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.