Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2007-11-09 : 03:49:27
|
Hi, I have no clue to get pass this.My previous post is my second problem & this my first.Converting Rows to columns: Too many table names in the query. The maximum allowable is 256.----------------------------------------------------------------------select case when v1.ProcessID is null -- 1 row with 4 tables then (Select max(V.ControlValue) from dbo.fcEventHist E left join dbo.fcEventValue V on E.ID = V.EventHistID where E.ProcessID = 367 and V.ControlID = 'FG1010NItemD' group by V.ControlValue) else (Select max(V.ControlValue) from dbo.fcEventArchive E left join dbo.fcEventValueArchive V on E.EventHistID = V.EventHistID where E.ProcessID = 367 and V.ControlID = 'FG1010NItemD' group by V.ControlValue) end NItemD,case when v1.ProcessID is null -- 2nd row then (Select max(V.ControlValue) from dbo.fcEventHist E left join dbo.fcEventValue V on E.ID = V.EventHistID where E.ProcessID = 367 and V.ControlID = 'FG1020NShortD' group by V.ControlValue) else (Select max(V.ControlValue) from dbo.fcEventArchive E left join dbo.fcEventValueArchive V on E.EventHistID = V.EventHistID where E.ProcessID = 367 and V.ControlID = 'FG1020NShortD' group by V.ControlValue) end NShortDfrom(select ProcessID from dbo.fcEventArchive where ProcessID = 367 group by ProcessID)v1 --------------------------------------------------------------------This code works but unfortunately I have 65 rows to be converted & does not allow me, becuase of all the joint tables Can anyone PLEASE see where I can minimixe the amount of tables used?Regards, |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 04:23:32
|
Now I realized your code has several flaws.1) You are grouping by the same column as you are aggregating MAX on2) If ProcessID 367 do not exist in table v1 no records whatsoever will be returnedHave a look at this replacement to see if it gives you what you wantIF EXISTS (SELECT * FROM dbo.fcEventArchive WHERE ProcessID = 367) SELECT MAX(CASE WHEN v.ControlID = 'FG1010NItemD' THEN v.ControlValue ELSE NULL END) AS NItemD MAX(CASE WHEN v.ControlID = 'FG1020NShortD' THEN v.ControlValue ELSE NULL END) AS NShortD FROM dbo.fcEventArchive AS e LEFT JOIN dbo.fcEventValueArchive AS v ON v.EventHistID = e.EventHistID AND v.ControlID IN ('FG1010NItemD', 'FG1020NShortD') WHERE e.ProcessID = 367ELSE SELECT MAX(CASE WHEN v.ControlID = 'FG1010NItemD' THEN v.ControlValue ELSE NULL END) AS NItemD MAX(CASE WHEN v.ControlID = 'FG1020NShortD' THEN v.ControlValue ELSE NULL END) AS NShortD FROM dbo.fcEventHist AS e LEFT JOIN dbo.fcEventValue AS v ON v.EventHistID = e.ID AND v.ControlID IN ('FG1010NItemD', 'FG1020NShortD') WHERE e.ProcessID = 367 E 12°55'05.25"N 56°04'39.16" |
 |
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2007-11-09 : 06:54:04
|
Hi, Wow.. A Big Thank You - totally different & good way. Thank YOUI added the max join as well but I have a problem as the ELSE part does not work it returns for all NULLS - I checked the data in the table.The entire second query without the Else returns Nulls.IF EXISTS (select * from dbo.fcEventArchive where ProcessID = 378)select MAX(Case when v.ControlID = 'FG1010NItemD' then v.ControlValue else NULL END) as NItemD ,MAX(Case when v.ControlID = 'FG1020NShortD' then v.ControlValue else NULL END) as NShortD from dbo.fcEventArchive as e left join dbo.fcEventValueArchive as v on v.EventHistID = e.EventHistID and v.ControlID in ('FG1010NItemD', 'FG1020NShortD') join (select max(v1.EventHistID) as max_id,v1.ControlID from dbo.fcEventArchive E1 left join dbo.fcEventValueArchive V1 on E1.EventHistID = V1.EventHistID where E1.ProcessID = 378 group by v1.controlid) max_qry on max_id = V.EventHistID and max_qry.ControlID = V.ControlID where e.ProcessID = 378Else select MAX(Case when v.ControlID = 'FG1010NItemD' then v.ControlValue else NULL END) as NItemD ,MAX(Case when v.ControlID = 'FG1020NShortD' then v.ControlValue else NULL END) as NShortD from dbo.fcEventHist as e left join dbo.fcEventValue as v on v.EventHistID = e.ID and v.ControlID in ('FG1010NItemD', 'FG1020NShortD') join (select max(v1.EventHistID) as max_id,v1.ControlID from dbo.fcEventArchive E1 left join dbo.fcEventValueArchive V1 on E1.EventHistID = V1.EventHistID where E1.ProcessID = 378 group by v1.controlid) max_qry on max_id = V.EventHistID and max_qry.ControlID = V.ControlID where e.ProcessID = 378It Returns ALL NULLS but there is data & link is fine.] Please have a look! Regards. |
 |
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2007-11-09 : 07:06:03
|
Hi, I got it... I did not change the tables & fields on my max for the second query. Thank You Mr. Larssonfrom dbo.fcEventHist as eleft join dbo.fcEventValue as v on v.EventHistID = e.IDand v.ControlID in ('FG1010NItemD', 'FG1020NShortD')join (select max(v1.EventHistID) as max_id,v1.ControlID from dbo.fcEventHist E1 left join dbo.fcEventValue V1 on E1.ID = V1.EventHistID where E1.ProcessID = 378 group by v1.controlid) max_qry on max_id = V.EventHistID and max_qry.ControlID = V.ControlIDwhere e.ProcessID = 378 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 07:23:59
|
And as a stored procedureCREATE PROCEDURE dbo.uspGetMyEvents( @ProcessID INT)ASSET NOCOUNT ONIF EXISTS (SELECT * FROM dbo.fcEventArchive WHERE ProcessID = @ProcessID) SELECT MAX(CASE WHEN ControlID = 'FG1010NItemD' THEN ControlValue END) AS NItemD MAX(CASE WHEN ControlID = 'FG1020NShortD' THEN ControlValue END) AS NShortD FROM ( SELECT TOP 1 WITH TIES v.ControlID, v.ControlValue FROM dbo.fcEventArchive AS e LEFT JOIN dbo.fcEventValueArchive AS v ON v.EventHistID = e.EventHistID AND v.ControlID IN ('FG1010NItemD', 'FG1020NShortD') WHERE e.ProcessID = @ProcessID ORDER BY e.EventHistID DESC ) AS jELSE SELECT MAX(CASE WHEN ControlID = 'FG1010NItemD' THEN ControlValue END) AS NItemD MAX(CASE WHEN ControlID = 'FG1020NShortD' THEN ControlValue END) AS NShortD FROM ( SELECT TOP 1 WITH TIES v.ControlID, v.ControlValue FROM dbo.fcEventHist AS e LEFT JOIN dbo.fcEventValue AS v ON v.EventHistID = e.ID AND v.ControlID IN ('FG1010NItemD', 'FG1020NShortD') WHERE e.ProcessID = @ProcessID ORDER BY e.ID DESC ) AS j E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|