将行聚合为单行

时间:2014-09-24 04:28:38

标签: sql sql-server aggregate

我无法将每个客户的表格汇总到一行,该表格包含预订但是包含预订的不同部分的重复记录,例如航班,住宿,税收,转移等等。

我知道我要分组的列,但我似乎无法正确理解 这就是我到目前为止所做的:

BkgAgentLut 这是一个查找表,其中包含每个文件名和工作表名称的单个记录,因为filename可以有两个不同的工作表名称,它会带回一个系统名称,我需要用它来定义我用于该聚合的文件。

AdvBookings 这是保存预订的原始数据表,必须将其拆分为不同的聚合IE(使用Micro作为其系统的文件,使用Vantage作为其系统的那些文件)这是因为两个不同的系统需要不同的group by子句到期原始数据。

SELECT
Min(URN), 
MAX(CAST(bookingdate AS integer)),
MAX(CAST(departuredate AS integer)),
MAX([IdentityValue]) AS [IdentityValue],
MAX(B.[FileName]) AS [FileName],
MAX(B.[SheetName]) AS [SheetName],
MAX([LineNum]) AS [LineNum],
MAX([Title]) AS [Title],
MAX([FirstName]) AS [FirstName],
MAX([Initial]) AS [Initial],
MAX([Surname]) AS [Surname],
MAX([NameLine]) AS [NameLine],
MAX([Addr1]) AS [Addr1],
MAX([Addr2]) AS [Addr2],
MAX([Addr3]) AS [Addr3],
MAX([Addr4]) AS [Addr4],
MAX([Addr5]) AS [Addr5],
MAX([HouseNo]) AS [HouseNo],
MAX([City]) AS [City],
MAX([County]) AS [County],
MAX([Postcode]) AS [Postcode],
MAX([TelNo1]) AS [TelNo1],
MAX([TelNo2]) AS [TelNo2],
SUM(CAST(TotalCost AS MONEY)) AS  TotalCost,
NettCost = NULL,
Paid = NULL,
Balance = NULL,
Discount = NULL,
Commission = NULL,
MAX(Adults) AS Adults,
MAX(Children) AS Children,
MAX(Infants) AS Infants,
MAX(Duration) AS Duration,
MAX(PAX) AS PAX,
SUM(CAST(Duration AS INT)) AS Duration,
MAX([DPA1]) AS [DPA1],
MAX([DPA2]) AS [DPA2],
MAX([PrimaryCode]) AS [PrimaryCode],
MAX([SEG]) AS [SEG],
MAX([Month]) AS [Month],
MAX([DeparturePoint]) AS [DeparturePoint],
MAX([ArrivalPoint]) AS [ArrivalPoint],
MAX([HolidayType]) AS [HolidayType],
MAX([TertiaryCode]) AS [TertiaryCode],
MAX([SubTertiaryCode]) AS [SubTertiaryCode],
MAX([Season]) AS [Season],
MAX([HQABTA]) AS [HQABTA],
MAX([BranchABTA]) AS [BranchABTA],
MAX([Company]) AS [Company],
MAX([Clerk]) AS [Clerk],
MAX([FirstRef]) AS [FirstRef],
MAX([SecondRef]) AS [SecondRef],
MAX([Board]) AS [Board],
MAX([EmailAddress]) AS [EmailAddress],
MAX([Direct]) AS [Direct],
MAX([Division]) AS [Division],
MAX([LeadPaxAge]) AS [LeadPaxAge],
MAX([DueDate]) AS [DueDate],
MAX([PreferredMailing]) AS [PreferredMailing],
MAX([Confidential]) AS [Confidential],
MAX([InsuranceNote]) AS [InsuranceNote],
MAX([DOB]) AS [DOB],
MAX([LastDestination]) AS [LastDestination],
MAX([Pax_DOBS]) AS [Pax_DOBS],
MAX([PaymentMethods]) AS [PaymentMethods],
MAX([AirportName]) AS [AirportName],
MAX([Resort]) AS [Resort],
MAX([Hotel]) AS [Hotel],
MAX([HotelLine2]) AS [HotelLine2],
MAX([RoomType]) AS [RoomType],
MAX([PreviousBookings]) AS [PreviousBookings],
MAX([PreviousTravels]) AS [PreviousTravels],
MAX([DestinationList]) AS [DestinationList],
MAX([Tour]) AS [Tour],
MAX([Description]) AS [Description],
MAX([Gender]) AS [Gender],
MAX([TransactionNo]) AS [TransactionNo],
MAX([TransactionType]) AS [TransactionType],
MAX([BusAcctRef]) AS [BusAcctRef],
MAX([BookingStatus]) AS [BookingStatus],
MAX([BranchName]) AS [BranchName],
MAX([OperName]) AS [OperName]
FROM AdvBookings B
LEFT JOIN BkgAgentLut A 
ON B.FileName = A.FileName 
AND B.SheetName = A.SheetName 
WHERE A.[Res System] = 'Vantage'
GROUP BY    title, 
    firstname, 
    Surname, 
    addr1, 
    Addr2, 
    Addr3, 
    Addr4, 
    Postcode,
    BookingDate,  -- THIS NEEDS TO BE TURNED INTO AN INTEGER SO i CAN USE IT AS A GROUP BY
    Departuredate -- THIS NEEDS TO BE TURNED INTO AN INTEGER SO i CAN USE IT AS A GROUP BY
ORDER BY FileName 

表格

    CREATE TABLE [dbo].[AdvBookings](
        [IdentityValue] [int] NOT NULL,
        [FileName] [varchar](55) NOT NULL,
        [SheetName] [varchar](49) NOT NULL,
        [LineNum] [int] NOT NULL,
        [BookingDate] [datetime] NOT NULL,
        [URN] [varchar](8) NULL,
        [Title] [varchar](25) NULL,
        [FirstName] [varchar](30) NULL,
        [Initial] [varchar](5) NULL,
        [Surname] [varchar](38) NULL,
        [NameLine] [varchar](41) NULL,
        [Addr1] [varchar](58) NULL,
        [Addr2] [varchar](50) NULL,
        [Addr3] [varchar](50) NULL,
        [Addr4] [varchar](45) NULL,
        [Addr5] [varchar](48) NULL,
        [HouseNo] [varchar](5) NULL,
        [City] [varchar](25) NULL,
        [County] [varchar](31) NULL,
        [Postcode] [varchar](19) NULL,
        [TelNo1] [varchar](40) NULL,
        [TelNo2] [varchar](37) NULL,
        [DPA1] [varchar](12) NULL,
        [DPA2] [varchar](4) NULL,
        [TotalCost] [varchar](19) NULL,
        [NettCost] [varchar](20) NULL,
        [Paid] [varchar](8) NULL,
        [Balance] [varchar](8) NULL,
        [Discount] [varchar](8) NULL,
        [Commission] [varchar](10) NULL,
        [PrimaryCode] [varchar](7) NULL,
        [Adults] [varchar](10) NULL,
        [Children] [varchar](7) NULL,
        [Infants] [varchar](2) NULL,
        [Pax] [varchar](7) NULL,
        [SEG] [varchar](3) NULL,
        [DepartureDate] [datetime] NULL,
        [Month] [varchar](19) NULL,
        [DeparturePoint] [varchar](22) NULL,
        [ArrivalPoint] [varchar](25) NULL,
        [HolidayType] [varchar](27) NULL,
        [TertiaryCode] [varchar](7) NULL,
        [SubTertiaryCode] [varchar](7) NULL,
        [Season] [varchar](3) NULL,
        [HQABTA] [varchar](7) NULL,
        [BranchABTA] [varchar](14) NULL,
        [Duration] [varchar](47) NULL,
        [Company] [varchar](64) NULL,
        [Clerk] [varchar](8) NULL,
        [FirstRef] [varchar](20) NULL,
        [SecondRef] [varchar](20) NULL,
        [Board] [varchar](51) NULL,
        [EmailAddress] [varchar](150) NULL,
        [Direct] [varchar](66) NULL,
        [Division] [varchar](7) NULL,
        [LeadPaxAge] [varchar](4) NULL,
        [DueDate] [varchar](19) NULL,
        [PreferredMailing] [varchar](10) NULL,
        [Confidential] [varchar](1) NULL,
        [InsuranceNote] [varchar](21) NULL,
        [DOB] [datetime] NULL,
        [LastDestination] [varchar](42) NULL,
        [Pax_DOBS] [varchar](255) NULL,
        [PaymentMethods] [varchar](255) NULL,
        [AirportName] [varchar](15) NULL,
        [Resort] [varchar](52) NULL,
        [Hotel] [varchar](180) NULL,
        [HotelLine2] [varchar](49) NULL,
        [RoomType] [varchar](255) NULL,
        [PreviousBookings] [varchar](255) NULL,
        [PreviousTravels] [varchar](255) NULL,
        [DestinationList] [varchar](255) NULL,
        [Tour] [varchar](36) NULL,
        [Description] [varchar](169) NULL,
        [Gender] [varchar](6) NULL,
        [TransactionNo] [varchar](4) NULL,
        [TransactionType] [varchar](35) NULL,
        [BusAcctRef] [varchar](6) NULL,
        [BookingStatus] [varchar](10) NULL,
        [BranchName] [varchar](62) NULL,
        [OperName] [varchar](50) NULL
    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF
    GO                      


        CREATE TABLE [dbo].[BkgAgentLut](
            [FileName] [nvarchar](255) NULL,
            [SheetName] [nvarchar](255) NULL,
            [Agent] [nvarchar](255) NULL,
            [ABTA] [nvarchar](5) NULL,
            [FileName_Rule] [nvarchar](255) NULL,
            [Agent_Identification_Rule] [nvarchar](255) NULL,
            [ShortName] [nvarchar](255) NULL,
            [Res System] [nvarchar](255) NULL,
            [ResID] [int] NULL
        ) ON [PRIMARY]
AdvBookings的

示例数据

IdentityValue,FileName,SheetName,LineNum,BookingDate,URN,Title,FirstName,Initial,Surname,NameLine,Addr,Addr2,Addr3,Addr4,Addr5,HouseNo,City,County,PosNcode,TelNo1,TelNo8,DPA1,DPA2,TotalCost,NettCost,Paid,Balance,Discount,Commission,PrimaryCode,Adults,Children,Infants,Pax,SEG,DepartureDate,Month,DeparturePoint,ArrivalPoint,HolidayType,TertiaryCode,SubTertiaryCode,Season,HQABTA,BranchABTA,Duration,Company,Clerk,FirstRef,SecondRef,Board,EmailAddress,Direct,Division,LeadPaxAge,DueDate,PreferredMailing,Confidential,InsuranceNote,DOB,LastDestination,Pax_DOBS,PaymentMethods,AirportName,Resort,Hotel,HotelLine2,RoomType,PreviousBookings,PreviousTravels,DestinationList,Tour,Description,Gender,TransactionNo,TransactionType,BusAcctRef,BookingStatus,BranchName,OperName
145990,ATE_L389X_20140902044749.CSV,ATE_L389X_20140902044749.CSV,1,00:00.0,385221,Mr,W,W,Sousa,NULL, David Moon Hosue,Devonshire Place,St Helier,NULL,NULL,NULL,NULL,NULL,KE2 3DP,864886,07797 748 173 Daught,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,1,0,0,1,NULL,00:00.0,NULL,JER,FNC,Summer Sun,NULL,NULL,NULL,NULL,L389X,14,ESTR,NULL,NULL,NULL,<No BoardBasis Defined>,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Funchal,Residencial Parque,NULL,NULL,NULL,NULL,Portugal,NULL,NULL,Male,2,Package Holiday,NULL,D,Bellingham Travel St Helier,Estrela Travel
1410709,ATE_Howard_20140901162839.CSV,ATE_Howard_20140901162839.CSV,1,00:00.0,8866,Mr,W,W,Coward,NULL, Southwood Road,Trowbridge,Wiltshire,NULL,NULL,NULL,NULL,NULL,BA647BZ,01885 666638,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,2,2,0,4,NULL,00:00.0,NULL,NULL,NULL,Cruise,NULL,NULL,NULL,NULL,92943,NULL,MCRU,NULL,NULL,NULL,NULL,carol.coward@blueyonder.co.uk,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,4,Cruise,NULL,D,Howard Travel,Misc Cruise
470590,AdvantageData 31 August.xlsx,'CBT Travel$',1,49:59.0,5035,Mr.,q,q,Hudson,NULL, Harlech Close,Haslingden,Rossendale,Lancashire,NULL,NULL,NULL,NULL,BB4 6NL,NULL,NULL,NULL,NULL,1729,1653,NULL,NULL,76,NULL,NULL,2,0,0,NULL,NULL,00:00.0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,11,Royal Caribbean Cruise Line,NULL,NULL,NULL,Full Board,NULL,NULL,NULL,NULL,NULL,NULL,N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Eastern Caribbean,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
496438,ALTHAMS_20140907.csv,ALTHAMS_20140907.csv,1,00:00.0,2004019,Mr,D,D,Cooper,NULL, Ullswater Close,Rishton,Blackburn,NULL,NULL,NULL,NULL,NULL,BB64EP,1854888480,NULL,Y,N,2206.24,NULL,2206.24,0,NULL,265.74,CNT,2,0,NULL,2,TP,00:00.0,Sep,MANCHESTER,NULL,NULL,REG,NULL,S10,11626,NULL,14,THOMSON,CT1,2771894,NULL,HB,NULL,NULL,ACC,65,06/07/2010,P,N,INSURANCE ISSUED,NULL,NULL,NULL,NULL,NULL,SANTORINI,VENUS BEACH,NULL,1 DOUBL3  SH WC BL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
1455951,ATE_C5649_20140903155313.CSV,ATE_C5649_20140903155313.CSV,1,00:00.0,64512,Mr,W,W,O Donnell,NULL, Derrycarib Road,NULL,Portadown,Co Armagh,NULL,NULL,NULL,NULL,BN62 6UY,NULL,7716080365,NULL,NULL,12,NULL,NULL,NULL,NULL,NULL,NULL,1,0,0,1,NULL,00:00.0,NULL,NULL,NULL,Summer Sun,NULL,NULL,NULL,NULL,C5649,NULL,MISC,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,2,Misc Item,NULL,D,Terra - C5649,Misc Account - Ukl Bookings
329035,HOLIDAYTVL_20140907.csv,HOLIDAYTVL_20140907.csv,1,00:00.0,1065539,Mr,PATRICK,P,Moody,NULL, Tennyson Avenue,Bridlington,East Yorkshire,NULL,NULL,NULL,NULL,NULL,YO65 2EX,6866661564,NULL,N,N,523,NULL,523,0,NULL,62.76,BCH,1,0,NULL,1,ST,00:00.0,Jun,TON,NULL,NULL,NULL,NULL,S10,35404,35404,7,SHEARINGS,RLH,M13291,NULL,HALF BOARD,NULL,NULL,NULL,NULL,27/03/2010,P,N,INS. DECLINED,NULL,NULL,NULL,NULL,NULL,"OBAN,MULL & IONA",GREAT WESTERN,NULL,SINGLE ROOM WITH A SEA VIEW,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
396525,REGAL_20140907.csv,REGAL_20140907.csv,1,00:00.0,1070979,Mrs,JANET,W,Hoskins,NULL, St David'S Close,Wild Mill,Bridgend,NULL,NULL,NULL,NULL,NULL,CF36 6RR,01656 658865,NULL,N,N,1106.55,NULL,1078,0,NULL,97.73,SSU,2,0,NULL,2,TP,00:00.0,Jul,"CARDIFF,UK",NULL,NULL,BUS,NULL,S10,71538,71538,10,THOMAS COOK,CLA,T122485N,       BUS,HALF BOARD,NULL,NULL,NULL,46,02/05/2010,P,N,JTI10A-38338146,00:00.0,NULL,NULL,NULL,NULL,MONASTIR,ROYAL KENZ,NULL,1 TWIN3 B S WC BAL/TER : PORT EL KANTAOUI,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Regal Travel - 71538,NULL
417902,TicketsAnywhere_20140831.tab,TicketsAnywhere_20140831.tab,1,00:00.0,100,Mrs,Tanya,NULL,Farrer,NULL, Merton Road,NULL,NULL,NULL,NULL,NULL,Princes Risborough,Bucks,PP27 0DR,0660 943 8015,NULL,NULL,NULL,18.5,18.5,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,00:00.0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,8932X,0,Ultrasun Sun cream,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Ultrasun sun cream,NULL,NULL,Ultrasun Sun cream,NULL
437608,TRAVELTIMEWORLD_20140907.csv,TRAVELTIMEWORLD_20140907.csv,1,00:00.0,3012302,Miss,LYNN,L,Guthrie,NULL,Nb Crystal,White Lion Wharf,Star Tops End Marsworth,NULL,NULL,NULL,NULL,NULL,PP23 4LK,1448,7756854687,N,N,541.9,NULL,541.9,0,NULL,27.6,LHV,1,0,NULL,1,ST,00:00.0,Feb,LH4,NULL,NULL,RPT,NULL,L,F2256,F2256,70,JETSET,ASH,3268399,NULL,NULL,NULL,NULL,NULL,NULL,02/01/2010,P,N,CLIENT DECLINED,NULL,NULL,NULL,NULL,NULL,ACCRA,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
447594,AdvantageData 31 August.xlsx,'Apex Rhuddlan$',1,56:23.0,6618,Mrs.,C,C,Morris,NULL, Heol Hendre,Rhuddlan,Denbighshire,NULL,NULL,NULL,NULL,NULL,LL68 5PG,NULL,NULL,NULL,NULL,418,418,NULL,NULL,0,NULL,NULL,2,0,0,NULL,NULL,00:00.0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4,Alfa Travel Ltd,NULL,NULL,NULL,Half Board,NULL,NULL,NULL,NULL,NULL,NULL,N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Vardiff And The Valleys,NULL,NULL,NULL,NULL,NULL,Alfa Travel - Free Insurance Offer,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
457205,AdvantageData 31 August.xlsx,'Baldwins Tenterden $',1,39:56.0,56,Mr.,P,P,Sabin,NULL,Woodcote,Woodchurch Road,Tenterden,NULL,NULL,NULL,NULL,NULL,NN30 7AD,NULL,NULL,NULL,NULL,3412.98,3362.98,NULL,NULL,50,NULL,NULL,2,0,0,NULL,NULL,00:00.0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,16,Celebrity Cruises,NULL,NULL,NULL,Full Board,NULL,NULL,NULL,NULL,NULL,NULL,N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Exotic Southern Caribbean,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
457821,AdvantageData 31 August.xlsx,'Baldwins Tonbridge$',1,04:03.0,14179,Mr.,K,K,Hill,NULL,Lodge Farm Oast,Bramble Reed Lane,Matfield,Kent,NULL,NULL,NULL,NULL,NN62 7EN,NULL,NULL,NULL,NULL,5239,5239,NULL,NULL,0,NULL,NULL,2,2,0,NULL,NULL,30:00.0,NULL,LGW,PVK,NULL,NULL,NULL,NULL,NULL,NULL,14,Travelux Ltd,NULL,NULL,NULL,Self Catering,NULL,NULL,NULL,NULL,NULL,NULL,N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Villa Votsalo,NULL,NULL,NULL,NULL,NULL,Greece / Lefkada,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
285347,CH Advantage_Report upto 31aug14.tab,CH Advantage_Report upto 31aug14.tab,1,00:00.0,7785,Mr,Mark,NULL,Saunders,NULL, Whitehall Road,NULL,NULL,NULL,NULL,NULL,Toronto,Ontario,M4W 2C5,14166886488,NULL,NULL,NULL,60,60,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4,NULL,00:00.0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,K4373,0,Out`N`About,NULL,NULL,NULL,NULL,linda.saunders@rogers.com,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Airport transfers,NULL,NULL,Out`N`About,NULL
81365,ATE_56665_20140902040159.CSV,ATE_56665_20140902040159.CSV,1,00:00.0,1295,Mrs,W,W,Green,NULL,C/O Mr & Mrs P Wanstall,2 Wellfield,Hartley,Kent,NULL,NULL,NULL,NULL,DA3 7EQ,01464 606 915,7957830380,NULL,NULL,2304,NULL,NULL,NULL,NULL,NULL,NULL,2,0,0,2,NULL,00:00.0,NULL,LGW,HOG,Longhaul,NULL,NULL,NULL,NULL,56665,14,TCH,NULL,NULL,NULL,<No BoardBasis Defined>,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Playa Pesquero,Hotel Playa Pesquero,NULL,NULL,NULL,NULL,Cuba,NULL,NULL,Female,1,Bonded Package,NULL,D,Sunways Travel,Thomas Cook Holidays
1056744,ATE_L3885_20140902045317.CSV,ATE_L3885_20140902045317.CSV,1,00:00.0,388377,Ms,Ann,A,Shanahan,NULL, Landscape Grove,St Helier,Jersey,Channel Islands,NULL,NULL,NULL,NULL,KE2 3KU,644168,077977 44186,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,4,0,0,4,NULL,00:00.0,NULL,NULL,NULL,<No Type Defined>,NULL,NULL,NULL,NULL,L3885,3,SBK,NULL,NULL,NULL,<No BoardBasis Defined>,ann.shanahan@bedellgroup.com        ann_shanahan@hotmail.com,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,To Be Advised,NULL,2 X Twins,NULL,NULL,NULL,NULL,NULL,Male,2,Hotels Foreign,NULL,D,Bellingham Travel St Brelade,Superbreaks
1420142,ATE_G1255_20140903152628.CSV,ATE_G1255_20140903152628.CSV,1,00:00.0,13922,Mr,C,C,Bushby,NULL,The Well House,Strait Lane,"Huby, Leeds",West Yorkshire,NULL,NULL,NULL,NULL,LS67 0EA,633361,L 07050811894,NULL,NULL,5770,NULL,NULL,NULL,NULL,NULL,NULL,3,0,0,3,NULL,00:00.0,NULL,LHR,MRU,Longhaul,NULL,NULL,NULL,NULL,G1255,10,IFON,NULL,NULL,NULL,<No BoardBasis Defined>,bushby@bigfastweb.net,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Mauritius,Le Touessrok,NULL,NULL,NULL,NULL,Mauritius,NULL,NULL,Male,1,Std Package Hol,NULL,D,Number One Travel,If Only Holidays Ltd
468921,AdvantageData 31 August.xlsx,'Baldwins Uckfield$',1,14:41.0,1,Mr.,W,W,Taylor,NULL, Hart Close,Uckfield,East Sussex,NULL,NULL,NULL,NULL,NULL,NN22 2DA,NULL,NULL,NULL,NULL,3058.2,3058.2,NULL,NULL,0,NULL,NULL,2,0,0,NULL,NULL,00:00.0,NULL,LHR,MRU,NULL,NULL,NULL,NULL,NULL,NULL,7,Enchanting Holidays,NULL,NULL,NULL,All Inclusive,NULL,NULL,NULL,NULL,NULL,NULL,N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Le Meridien Ile Maurice,NULL,NULL,NULL,NULL,NULL,Mauritius,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
760382,ATE_33466_20140902043502.CSV,ATE_33466_20140902043502.CSV,1,00:00.0,3,Mr,I,I,Test,NULL,Test,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,1,0,0,1,NULL,00:00.0,NULL,NULL,NULL,<No Type Defined>,NULL,NULL,NULL,NULL,33466,NULL,RAIE,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,1,Motorail,NULL,D,Travelwise Handforth,French Motorail
769599,ATE_2585X_20140903154534.CSV,ATE_2585X_20140903154534.CSV,1,00:00.0,1,Mr,D,D,Keenan,NULL, Redcar Drive,Eastham,Wirral,NULL,NULL,NULL,NULL,NULL,CP62 8PE,01515130466-Daniel,NULL,NULL,NULL,1900,NULL,NULL,NULL,NULL,NULL,NULL,2,0,0,2,NULL,00:00.0,NULL,NULL,NULL,Cruise,NULL,NULL,NULL,NULL,2585X,NULL,PAGE,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,1,Cruise,NULL,D,Dalton Travel Dunmow,Page & Moy Ltd
1535988,ATE_81633_20140902043127.CSV,ATE_81633_20140902043127.CSV,1,00:00.0,823,Mrs,W,W,Fairhurst,NULL,Chapel House,Arkholme,CARNFORTH,Lancashire,NULL,NULL,NULL,NULL,LA6  6AX,015848 81454,NULL,NULL,NULL,71.9,NULL,NULL,NULL,NULL,NULL,NULL,1,0,0,1,NULL,00:00.0,NULL,NULL,NULL,<No Type Defined>,NULL,NULL,NULL,NULL,81633,NULL,RAIL,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Female,1,Rail Ticket,NULL,D,Gates Travel Kendal,ATOC
1910689,ATE_68605_20140903155212.CSV,ATE_68605_20140903155212.CSV,1,00:00.0,46357,Mr,Jonathan,W,Brady,NULL, Lynden Gate Park,Portadown,NULL,Co Armagh,NULL,NULL,NULL,NULL,BN63 5YP,6610804168,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,2,0,0,2,NULL,00:00.0,NULL,BFS,<No Dest D,Summer Sun,NULL,NULL,NULL,NULL,68605,2,SOLU,NULL,NULL,NULL,<No BoardBasis Defined>,NULL,NULL,3,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Alton Towers,Best Western Moathouse,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,2,Package/Ft Only,NULL,D,Terra Travel Lurgan,Travel Solutions
1943852,ATE_61659_20140903154707.CSV,ATE_61659_20140903154707.CSV,1,00:00.0,1,Mr,N,N,Express,NULL,.,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,1,0,0,1,NULL,00:00.0,NULL,NULL,NULL,National Xpress,NULL,NULL,NULL,NULL,61659,NULL,NEX,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,6,Miscellaneous,NULL,D,AirViceroy,National Express
206859,ATE_75543_20140807124842.CSV,ATE_75543_20140807124842.CSV,1,00:00.0,168,Mrs,K,K,Scott,NULL, Twining Avenue,Twickenham,Middlesex,NULL,NULL,NULL,NULL,NULL,NW2 5LP,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,3,0,0,3,NULL,00:00.0,NULL,LGW,IST,Summer Sun,NULL,NULL,NULL,NULL,75543,7,PLAN,NULL,NULL,NULL,<No Board Basis Defined>,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Limassol,Blue Crane Apts,NULL,NULL,NULL,NULL,Turkey,NULL,NULL,Female,1,Std Package Hol,NULL,D,Thames Travel,Planet  Holidays
207037,ATE_P5549_20140903153751.CSV,ATE_P5549_20140903153751.CSV,1,00:00.0,41813,Mr,S,S,Rawson,NULL, Wike Ridge Mount,Leeds,NULL,NULL,NULL,NULL,NULL,NULL,LS67 9NP,6984916306,NULL,NULL,NULL,1040.6,NULL,NULL,NULL,NULL,NULL,NULL,1,0,0,1,NULL,00:00.0,NULL,MAN,MEL,<No Type Defined>,NULL,NULL,NULL,NULL,P5549,NULL,JSET,NULL,NULL,NULL,.,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Australia,NULL,NULL,Male,1,Consolidation,NULL,D,Top Choice Holidays,Jetset Tours
226820,ATE_P6140_20140902041603.CSV,ATE_P6140_20140902041603.CSV,1,00:00.0,1548,Mrs,K,K,Kells,NULL,Granville House,Barrack Lane,Lilleshall,Nr Newport Shropshire,NULL,NULL,NULL,NULL,NF60 9ER,1958666661,NULL,TRUE,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,2,0,0,2,NULL,00:00.0,NULL,NULL,NULL,Summer Sun,NULL,NULL,NULL,NULL,P6140,7,THOS,NULL,NULL,NULL,<No BoardBasis Defined>,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Breadsall,Priory,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Female,2,Package Holiday,NULL,D,Roma Travel Keyworth,Thomas Cook Group Ltd
237560,ATE_P6603_20140903154136.CSV,ATE_P6603_20140903154136.CSV,1,00:00.0,59664,Mrs,Jayne,W,Yeomans,NULL,Oswestry,NULL,NULL,Shropshire,NULL,NULL,NULL,NULL,SY66 2LW,1691658111,7763979418,NULL,NULL,1612,NULL,NULL,NULL,NULL,NULL,NULL,2,0,0,2,NULL,00:00.0,NULL,MAN,PFO,Summer Sun,NULL,NULL,NULL,NULL,P6603,7,THOM,NULL,NULL,NULL,<No BoardBasis Defined>,jayne4467yeomans@aol.com,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Paphos,Louis Phaethon Beach Club,NULL,NULL,NULL,NULL,Cyprus,NULL,NULL,Female,1,Package Holiday,NULL,D,PolkaDot Travel,Thomson Holidays Ltd
249315,ATE_K5624_20140902043254.CSV,ATE_K5624_20140902043254.CSV,1,00:00.0,216,Mr,EW,EW,Murray,NULL,a Linton Close,Newark,NULL,Notts,NULL,NULL,NULL,NULL,NG244NQ,666146,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,2,2,0,4,NULL,00:00.0,NULL,NULL,NULL,Coach Tours Uk,NULL,NULL,NULL,NULL,K5624,4,TRWH,NULL,NULL,NULL,<No BoardBasis Defined>,NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Skegness Contra,De Vere Mottram Hall Hotel,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Male,2,Std Package Hol,NULL,D,Roma - Newark,Travel Wright Ltd
263146,ATE_P6621_20140903153607.CSV,ATE_P6621_20140903153607.CSV,1,00:00.0,34393,Mrs,A,A,White,NULL, Glen Rise,Belfast,NULL,NULL,NULL,NULL,NULL,NULL,BN5 7LF,8890940015,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,2,0,0,2,NULL,00:00.0,NULL,BFS,SYD,<No Type Defined>,NULL,NULL,NULL,NULL,P6621,NULL,BSP,NULL,NULL,NULL,<No Board Basis Defined>,markana@virginmedia.com,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,Australia,NULL,NULL,Female,1,Air Tickets,NULL,E,Breakaway Travel Shankill Road Belfast,B S P ( U K )
用于更改数据类型的

脚本:

         
SELECT CAST([IdentityValue] as bigint)[IdentityValue]
      ,[FileName]
      ,[SheetName]
      ,CAST([LineNum] AS INT) [LineNum]
      ,CAST([BookingDate] AS INT) [BookingDate]
      ,CAST([URN] AS INT) [URN]
      ,[Title]
      ,[FirstName]
      ,[Initial]
      ,[Surname]
      ,[NameLine]
      ,[Addr1]
      ,[Addr2]
      ,[Addr3]
      ,[Addr4]
      ,[Addr5]
      ,[HouseNo]
      ,[City]
      ,[County]
      ,[Postcode]
      ,[TelNo1]
      ,[TelNo2]
      ,[DPA1]
      ,[DPA2]
      ,CAST([TotalCost] AS MONEY) [TotalCost]
      ,CAST([NettCost] AS MONEY) [NettCost]
      ,[Paid]
      ,[Balance]
      ,CAST([Discount] AS MONEY) [Discount]
      ,[Commission]
      ,[PrimaryCode]
      ,CAST([Adults] AS INT) [Adults]
      ,CAST([Children] AS INT) [Children]
      ,CAST([Infants] AS INT) [Infants]
      ,[Pax]
      ,[SEG]
      ,CAST([DepartureDate]
      ,[Month]
      ,[DeparturePoint]
      ,[ArrivalPoint]
      ,[HolidayType]
      ,[TertiaryCode]
      ,[SubTertiaryCode]
      ,[Season]
      ,[HQABTA]
      ,[BranchABTA]
      ,CAST([Duration] AS INT) [Duration]
      ,[Company]
      ,[Clerk]
      ,[FirstRef]
      ,[SecondRef]
      ,[Board]
      ,[EmailAddress]
      ,[Direct]
      ,[Division]
      ,[LeadPaxAge]
      ,[DueDate]
      ,[PreferredMailing]
      ,[Confidential]
      ,[InsuranceNote]
      ,[DOB]
      ,[LastDestination]
      ,[Pax_DOBS]
      ,[PaymentMethods]
      ,[AirportName]
      ,[Resort]
      ,[Hotel]
      ,[HotelLine2]
      ,[RoomType]
      ,[PreviousBookings]
      ,[PreviousTravels]
      ,[DestinationList]
      ,[Tour]
      ,[Description]
      ,[Gender]
      ,[TransactionNo]
      ,[TransactionType]
      ,[BusAcctRef]
      ,[BookingStatus]
      ,[BranchName]
      ,[OperName]
      INTO CustStg
  FROM AdvBookings

1 个答案:

答案 0 :(得分:0)

道歉,因为这对于评论来说太大了,可能不会被视为答案。

比较这两个查询:其中第一个不会产生任何行,除非A. [Res System] ='Vantage'

select 'use where condition', b.*
FROM AdvBookings B
LEFT JOIN BkgAgentLut A 
      ON B.FileName = A.FileName 
        AND B.SheetName = A.SheetName 
WHERE A.[Res System] = 'Vantage'
;

其中第二个查询将列出来自AdvBookings的所有记录,即使A. [Res System] ='Vantage'不是真的。

select 'use join condition', b.*
FROM AdvBookings B
LEFT JOIN BkgAgentLut A 
      ON B.FileName = A.FileName 
        AND B.SheetName = A.SheetName 
        AND A.[Res System] = 'Vantage'

区别在于如何应用条件。在第一个查询中,您有一个“隐含的内部联接”,因为每一行都符合where子句。在第二个中你有一个带有条件的LEFT JOIN,这并不禁止从AdvBookings中列出行。


对于您的其余问题,您需要提供可说明问题的示例数据。我找不到任何但是我正在使用SQLFiddle,这将DDL限制为8000个字符,所以我只能插入几行。您提供的日期时间信息似乎也只包含时间。

请参阅:http://sqlfiddle.com/#!3/646fc/5

对于我插入的少数记录,我的查询没有特别的问题。