2017-01-24 39 views
1

我無法獲取不同的記錄。這是我的查詢。獲取不同記錄的問題(分組方式)

SELECT DISTINCT TOP (100) PERCENT 
dbo.New_BookList.author, dbo.issued.issued, dbo.Ath_Crs_New.SUBJECT, dbo.New_BookList.isbn, dbo.New_BookList.title 
FROM dbo.New_BookList 
LEFT OUTER JOIN dbo.Ath_Crs_New 
ON dbo.New_BookList.catalog_Number = dbo.Ath_Crs_New.CATALOG_NUMBER 
AND dbo.New_BookList.emplid = dbo.Ath_Crs_New.EMPLID 
LEFT OUTER JOIN dbo.issued ON dbo.New_BookList.emplid = dbo.issued.emplid 
AND dbo.New_BookList.isbn = dbo.issued.itemnum 
AND dbo.New_BookList.term = dbo.issued.term 
WHERE (dbo.New_BookList.book_status <> 'cancelled') 
AND (dbo.New_BookList.emplid = 227698467) 
AND (dbo.New_BookList.term = 1172) 
ORDER BY dbo.New_BookList.isbn 

這是我得到的輸出:

+------------+--------+---------+---------------+--------------------------------+ 
| Author | Issued | Subject |  ISBN  |    Title    | 
+------------+--------+---------+---------------+--------------------------------+ 
| ALPERT  | False | REL  | 9780231165716 | RELIGION & SPORTS    | 
| TOP HAT | False | EAR  | 9780986615108 | MONOCLE CAT SEMESTER   | 
| TOP HAT | False | IST  | 9780986615108 | MONOCLE CAT SEMESTER   | 
| KING  | False | CFS  | 9781269910842 | HUMAN SEXUALITY TODAY PACKAGE | 
| PEARSON ED | False | CFS  | 9781269952460 | MYPSYCHLAB FOR KING HUMAN SEXU | 
| KING  | False | CFS  | 9781269954280 | HUMAN SEXUALITY TODAY CUSTOM F | 
| DUARTE  | False | IST  | 9781422187104 | HBR GUIDE TO PERSUASIVE PRESEN | 
| MILLER  | False | IST  | 9781541800861 | PROGRAMMING IN PYTHON 3 ACCESS | 
| ARNOLD  | False | REL  | 9781935551218 | GIFT OF SPORTS - REVISED (LOOS | 
+------------+--------+---------+---------------+--------------------------------+ 

我的問題是與Monacle CAT學期的書。由於兩個副本上的ISBN編號相同,因此如果它已經列出,我只想顯示一次。所以我需要以某種方式將它按照ISBN進行分組,無論這個主題是什麼。

任何想法,我會如何做到這一點?

+0

「主題」列是查詢中的必填字段嗎? – bbrumm

+0

是的。但只要ISBN是相同的,哪個主題就顯示出來並不重要。 – spacerobot

回答

3

您可以對結果進行分組,並按字母順序選擇MAX或MIN主題。

SELECT DISTINCT TOP (100) PERCENT 
    dbo.New_BookList.author 
    , dbo.issued.issued 
    , MAX(dbo.Ath_Crs_New.SUBJECT) AS [SUBJECT] 
    , dbo.New_BookList.isbn 
    , dbo.New_BookList.title 
FROM   
    dbo.New_BookList LEFT OUTER JOIN 
        dbo.Ath_Crs_New ON dbo.New_BookList.catalog_Number = dbo.Ath_Crs_New.CATALOG_NUMBER AND 
        dbo.New_BookList.emplid = dbo.Ath_Crs_New.EMPLID LEFT OUTER JOIN 
        dbo.issued ON dbo.New_BookList.emplid = dbo.issued.emplid AND dbo.New_BookList.isbn = dbo.issued.itemnum AND 
        dbo.New_BookList.term = dbo.issued.term 
WHERE  (dbo.New_BookList.book_status <> 'cancelled') AND (dbo.New_BookList.emplid = 227698467) AND (dbo.New_BookList.term = 1172) 
GROUP BY 
    dbo.New_BookList.author 
    , dbo.issued.issued 
    , dbo.New_BookList.isbn 
    , dbo.New_BookList.title 
ORDER BY dbo.New_BookList.isbn 
+0

謝謝。完美的作品! – spacerobot