Ältesten und jüngesten Eintrag

GP83

Grünschnabel
Hallo zusammen,

Wir arbreiten mit dem SQL Server 2005 . Ich benötige aus einer Tabelle jeweils den ältesten und jüngsten Eintrag. Wenn es sich um einen "stornierten Posten" handelt den ältesten (kleinste Entry No), sonst den jüngsten (größte Entry No)..

Meine Tabelle hat in etwas folgendes Aussehen

Entry No Customer Fraktion System Storniert
123 A 08 12 ja
124 A 08 12 ja
138 A 08 12 nein
142 A 10 11 ja
153 A 10 11 nein

Ich bräuchte für diese Beispiel Tabelle als Ergebnis die Einträge mit den [Entry No_]
123 ,138, 142 , 153

Ich erhalte das geüwnschte Ergebnis durch eine komplizierte StoredPRocedure

Code:
ALTER PROCEDURE [dbo].[foreign_period_storno]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

-- Buffer Variables
declare @fraction as char (20)
declare @customer as char (20)
declare @system as char(30)
-- Border Variables
declare @fractionmax as int
declare @customermax as int
declare @systemmax as int
-- Count Variables
declare @fractioncnt as int
declare @customercnt as int
declare @systemcnt as int

-- Initialize Variables
set @fractionmax =(SELECT count (*) FROM [DimFraction])
set @fractioncnt = 1

set @customermax =(SELECT count (*) FROM [DimCustomer])
set @customercnt = 1

set @systemmax =(SELECT count (*) FROM [DimSystem])
set @systemcnt = 1

set @system = (SELECT [ Code ] FROM [DimSystem] WHERE [SK_System] = @systemcnt)
	

WHILE (@fractioncnt <= @fractionmax)
BEGIN
set @fraction = (SELECT [Fraction] FROM [DimFraction] WHERE [SK_Fraction] = @fractioncnt)

	While (@systemcnt <= @systemmax)
	BEGIN
	set @system = (SELECT [ Code ] FROM [DimSystem] WHERE [SK_System] = @systemcnt)
	
		WHILE (@customercnt <= @customermax)
		BEGIN
		set @customer = (SELECT [No_] FROM [DimCustomer] WHERE [SK_Customer] = @customercnt)
		
			-- Insert into the tmp Table 
			INSERT INTO [tmpJAMPF_2008] ([Source Table], [Entry No], [Customer], [System],[Fraction], [Year],[Month],
			[Notification Type], [Rückstellungsart],[Amount],[Quantity],[Source Type],[Notification Cycle],[Customer name],
			[Line Discount Amount],[Posting Date],[Correction])
	
			Select * 
			FROM [jampf_2008]
			Where [Entry No] IN (
				SELECT Top 1 [Entry No]
				FROM [jampf_2008]
				WHERE [Source Table] = 'Fakturaposten' and [correction]= 1 and customer = @customer  and [System]= @system and fraction = @fraction
				UNION
				SELECT Top 1 [Entry No]
				FROM [jampf_2008]
				WHERE [Source Table] = 'Fakturaposten' and [correction]= 0 and customer = @customer  and [System]= @system and fraction = @fraction
				ORDER BY [Entry No] desc)

		--	Order BY [Customer], [System], [Fraction], [Posting Date], [Notification Type],[Source Type],[Entry No]
		set @customercnt = @customercnt + 1
		END--Customer	
		
	set @customercnt = 1
	set @systemcnt = @systemcnt + 1		
	End-- System
	set @systemcnt = 1

set @fractioncnt = @fractioncnt + 1
END -- Fraktion
		

END


Aufgrund der Verschachtelung erhlate ich eine extrem schlechte Performance.

Wer kann mir helfen, damit ich die Tabelle nach Kunden, System, Fraktion und Entry No gruppieren und sortieren und dann jeweils die größte bzw kleinste [Entry No] jeder Gruppe in eine neue Tabelle einzufügen?

Viele Dank für Eure Hifle!!
Steffen
 
Hallo Zusammen,
mein Kollege hat letzte NAcht die geniale Lösung gefunden. Man baut Sichten auf, die in etwa folgendes Statement enthalten:

PHP:
select * from a as t1
JOIN
  (select Min([Entry No]) as EntryNo
      from a
      group by [System],[Fraktion],[Kunde]) t2 on t1.[Entry No] = t2.EntryNo


Gruß
GP83
 
Zurück