Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How To Write This Query
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01396430
Message ID:
01396442
Vues:
53
Here's what I have, but the result set doesn't look right. I think I need some more assistance:
SELECT p.*, ph.* 
	FROM Parts p
	JOIN PartHistory ph ON ph.PartId = p.PartId
	WHERE p.PartId IN
		(SELECT PartId
			FROM PartHistory mt1
			WHERE NOT EXISTS 
				(SELECT 1 
					FROM PartHistory mt2 
					WHERE mt1.PartId = mt2.PartId AND 
						  mt1.SerialNo = mt2.SerialNo AND 
						  mt1.Sequence < mt2.Sequence))
>First, replace 'mytable' with 'PartHistory' and 'PartNo' with 'PartId'. After that you can use result set as derived table and join back to Parts on PartId.
>
>>Ok, I get it. But there's a catch
>>
>>PartNo is in Parts. The remain data is in PartHistory:
>>
>>
>>CREATE TABLE Parts
>>	(PartId		INT IDENTITY PRIMARY KEY,
>>	 PartNo		VARCHAR(30)	NOT NULL,
>>	 Description	TEXT		NOT NULL)
>>
>>CREATE TABLE PartHistory
>>	(HistoryId	INT IDENTITY PRIMARY KEY,
>>	 PartId		INT		NOT NULL,
>>	 UserId		INT		NOT NULL,
>>	 LocationId	INT		NULL,
>>	 SerialNo	VARCHAR(30)	NULL,
>>	 Quantity	INT		NOT NULL,
>>	 DropShip	TINYINT		NULL,
>>	 Printed	TINYINT		NULL,
>>	 ActionDate	DATETIME	NULL DEFAULT GETDATE(),	
>>	 Sequence	INT		NOT NULL,
>>	 Comments	TEXT		NULL)
>>
>>
>>
>>So, given this, how do I join these tables in this query you gave me?
>>
>>
>>>Try
>>>-- or 
>>>SELECT * FROM mytable mt1
>>>  WHERE NOT EXISTS (SELECT 1 FROM mytable mt2 
>>>                  WHERE mt1.PartNo = mt2.PartNo AND mt1.SerialNo = mt2.SerialNo
>>>                    AND mt1.Sequence < mt2.Sequence)
>>>- or
>>>SELECT * FROM mytable mt1
>>>  WHERE 1 =  (SELECT COUNT(*)  FROM mytable mt2 
>>>                  WHERE mt1.PartNo = mt2.PartNo AND mt1.SerialNo = mt2.SerialNo)
>>>
>>>- or (may not work because of NULL date)
>>>SELECT * FROM mytable mt1
>>>  WHERE NOT EXISTS (SELECT 1 FROM mytable mt2 
>>>                  WHERE mt1.PartNo = mt2.PartNo AND mt1.SerialNo = mt2.SerialNo
>>>                    AND mt1.Date <> mt2.Date)
>>>
>>
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform