Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
Delphi 7 Enterprise
|
Re: SQL Abfrage, Zählen von n Datensätzen aus 2. Tabelle
7. Aug 2008, 12:41
SQL-Code:
SELECT IssueID, Name,
CASE WHEN (SELECT COUNT(*)
FROM TB_IssueAttachetFiles
WHERE FK_IssueID = i.IssueID) > 0
THEN 'Yes'
ELSE NULL
END Attachment
FROM tb_issues i
da du die genaue Anzahl ja nicht benötigst...
SQL-Code:
SELECT IssueID, Name,
CASE WHEN EXISTS (SELECT *
FROM TB_IssueAttachetFiles
WHERE FK_IssueID = i.IssueID)
THEN 'Yes'
ELSE NULL
END Attachment
FROM tb_issues i
oder auch...
SQL-Code:
SELECT i.IssueID, i.Name, COALESCE('Yes (' + a.anzahl + ')', '') AS Attachment
FROM tb_issues i
LEFT JOIN (SELECT FK_IssueID, COUNT(*) AS anzahl
FROM TB_IssueAttachetFiles
GROUP BY FK_IssueID) a
ON i.IssueID = a.FK_IssueID
|
|
Zitat
|