Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2008-02-21 : 00:50:48
|
My query is looking for unique booking numbers with their product name, however the database where the data is stored seems to have (in some cases) more than one product Code.How can i select just the first one it comes to for each "booking No"?So that for the first "Booking No" A000398, it will only showA000398 WIEDU02 European Explorerand NOTA000398 WIEDU02 European ExplorerA000398 WIEPU02 European ExplorerTHX ICWSelect distinct "Booking No", "Item ref" as ProdCode, "Prod Name" from tourproducts, BComponents where bcomponents."Item Ref" = tourproducts."Tour Product Code"and "Product Type" = 'T' and "PaxID" = '001' and "Tour Prod Status" = 'CF'order by "Booking No" Sample ResultsBooking ProdCode Prod NameA000398 WIEDU02 European ExplorerA000398 WIEPU02 European ExplorerA000678 WHARU02 Heart of ItalyA000840 WIRIU02 Real IrelandA001062 WROLU02 European JauntA001097 WROLU02 European JauntA001123 WIVAU02 European ValueA001164 WITMU02 Real ItalyA001305 WSOMU02 Sound of MusicA001450 WIPMU02 Portugal Morocco and SpainA001450 WISPU02 Real SpainA001745 EGVOU03 Egyptian VoyagerA001745 PREU8U01Pre Registered Tours Europe Summer 2008A001745 WIEVU02 Egyptian VoyagerA002279 DCRAU03 Canadas RockiesA002419 WIRIU02 Real IrelandA002455 WIRIU02 Real IrelandA002500 WITMU02 Real ItalyA002553 WHARU02 Heart of ItalyA002592 WISPU02 Real SpainA002762 WFESU02 French EscapadeA002842 WIPMU02 Portugal Morocco and SpainA002842 WISPU02 Real SpainA002915 RITMU102Real Italy ReverseA002922 DPNAU03 Northwest Coastal AdventureA003004 AGBOU03 Best of GreeceA003004 WIRGU02 Real GreeceA003026 DWFTU03 Western FrontiersA003067 WISIU02 Simply ItalyA003067 WITAU02 Italy and Sicily |
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-21 : 01:01:16
|
Tryselect * from(Select [Booking No], [Item ref] as ProdCode, [Prod Name],row_number()over (partition by [Booking No] order by [Booking No]) as [Row]from tourproducts, BComponents where bcomponents."Item Ref" = tourproducts."Tour Product Code"and [Product Type] = 'T' and [PaxID] = '001' and [Tour Prod Status] = 'CF' order by [Booking No] )b where [Row]=1One more suggestion, use [] instead of quotes for aliases and specifying column names. |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2008-02-21 : 01:47:45
|
Thanks for the tip about the square brackets. Just so I know for future ref why should i not use quotes.Also the query you offered gives me the following error.Msg 1033, Level 15, State 1, Line 5The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specifiedthanksICW |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-21 : 01:51:33
|
Tryselect * from(Select [Booking No], [Item ref] as ProdCode, [Prod Name],row_number()over (partition by [Booking No] order by [Booking No]) as [Row]from tourproducts, BComponents where bcomponents."Item Ref" = tourproducts."Tour Product Code"and [Product Type] = 'T' and [PaxID] = '001' and [Tour Prod Status] = 'CF' ) b where [Row]=1 order by [Booking No]for quotes, see following thread:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=95347 |
|
|
MaxLu
Starting Member
1 Post |
Posted - 2008-02-21 : 02:13:38
|
Select distinct "Booking No", (select top 1 "Item ref" from BComponents where bcomponents."Item Ref" = tourproducts."Tour Product Code" ) as ProdCode, "Prod Name" from tourproducts where "Product Type" = 'T' and "PaxID" = '001' and "Tour Prod Status" = 'CF'order by "Booking No" |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2008-02-21 : 04:21:07
|
Thanks both for these queries;Sunil yours works perfectly now and gives me 1471 rows.I wanted to compare the two queries to see if they gave the same result, but MAX your query s giving an error, because it looks like the second table is not a part of the query. Also , it may be a red herrig but i want to get the top Booking No not the top Item ref.Thanks again guysmuch appreciated |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-21 : 04:33:09
|
what's MAX you are trying to get? Also, thisselect * from(Select [Booking No], [Item ref] as ProdCode, [Prod Name],row_number()over (partition by [Booking No] order by [Booking No]) as [Row]from tourproducts, BComponents where bcomponents."Item Ref" = tourproducts."Tour Product Code"and [Product Type] = 'T' and [PaxID] = '001' and [Tour Prod Status] = 'CF' ) b where [Row]=1 order by [Booking No]will give you first booking number it finds.for example:A001450 WIPMU02 Portugal Morocco and SpainA001450 WISPU02 Real SpainAbove query will return first record i.e. Portugal Morocco and Spain. Hope it helps. |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2008-02-21 : 04:44:26
|
Sunil,I have adopted your one into my query which I used for a sql viewthanks again:O) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-21 : 06:34:55
|
[code]SELECT {Table alias tp or bc here}.[Booking No], bc.[Item ref] AS ProdCode, MAX({Table alias tp or bc here}.[Prod Name]) AS [Prod Name]FROM TourProducts AS tpINNER JOIN BComponents AS bc ON bc.[Item Ref] = tp.[Tour Product Code]WHERE {Table alias tp or bc here}.[Product Type] = 'T' AND {Table alias tp or bc here}.PaxID = '001' AND {Table alias tp or bc here}.[Tour Prod Status] = 'CF'order by {Table alias tp or bc here}.[Booking No], bc.[Item ref][/code] E 12°55'05.25"N 56°04'39.16" |
|
|
|