Aggregating Rows into a Single row - sql
I am having trouble aggregating a table down into a single row for each customer, the table contains bookings but has duplicate records for different parts of the booking like flight, accommodation, tax, transfer that sort of thing.
I know what columns I have to group by but I just cant seem to get it right
This is what I have so far:
BkgAgentLut
this is a lookup table that contains a single record for each filename and sheet name as filename could have two different sheet names, it brings back a system name which I need to use to define what files im using for that aggregation.
AdvBookings
This is the raw data table that holds the bookings this has to be split into different aggregations I.E( those files that use Micro as its system, those that use Vantage as its system) this is because the two different systems require diofferent group by clauses due to the raw data.
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
Tables:
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]
Sample Data for 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 )
script to change the data types:
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
Apologies as this is too large for a comment and might not be considered an answer.
Compare these 2 queries: The first of these will not produce any rows whatsoever unless 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'
;
The second of those queries will list all records from AdvBookings even if A.[Res System] = 'Vantage' is not true.
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'
The difference is how the condition is applied. In the first query you have an "implied inner join" because every row HAS TO comply with the where clause. In the second you have a LEFT JOIN with conditions and this does not prohibit rows from being listed from AdvBookings.
For the rest of your question you need to provide sample data that will illustrate your problem. I could not find any BUT I was using SQLFiddle and this limits DDL to 8000 characters so I could only get a few rows inserted. Also the datetime information you have provided seems to be only contain time.
see: http://sqlfiddle.com/#!3/646fc/5
For the few records I did get inserted I had no specific problem with your query.
Related
SQL Server Fix Correlated Sub-Queries for Better Performance?
SQL Server 2014 I am trying to learn better SQL code practices to improve performance. I am inheriting some old code, and wanted to get an idea of how someone who actually knows what they're doing would improve it. I have tried to shorten the code to just the main part I'm working on right now. It seems like there is a better way to get the Reason Codes (PersonalTraining, Tennis, etc.) than running multiple Correlated Subqueries? But, so far I'm having trouble getting the results to return on one row per customer. The final results must stay the same for a 3rd party. A customer can have zero to multiple Reason Codes that are stored in the table "asajoinmbr" Table asajoinmbr: CREATE TABLE [dbo].[ASAJoinmbr]( [cust_code] [char](10) NOT NULL, [mbr_code] [char](10) NOT NULL, [reason_code] [char](3) NULL, [associate_code] [char](10) NULL, [join_note] [ntext] NULL, [wants_contact] [char](1) NULL, [club] [smallint] NULL, [region] [char](4) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO Table asamembr: CREATE TABLE [dbo].[ASAMembr]( [cust_code] [nvarchar](10) NULL, [mbr_code] [nvarchar](10) NULL, [lname] [nvarchar](20) NULL, [fname] [nvarchar](20) NULL, [status] [nvarchar](6) NULL, [bdate] [datetime] NULL, [email] [nvarchar](80) NULL, [Club] [nvarchar](3) NULL, ) ON [PRIMARY] Table strcustr: CREATE TABLE [dbo].[StrCustr]( [cust_code] [nvarchar](10) NULL, [bridge_code] [nvarchar](10) NULL, [bus_name] [nvarchar](30) NULL, [status] [nvarchar](1) NULL, [phone] [nvarchar](20) NULL, [address1] [nvarchar](30) NULL, [address2] [nvarchar](30) NULL, [city] [nvarchar](20) NULL, [state] [nvarchar](10) NULL, [post_code] [nvarchar](10) NULL, [cntry_abbrev] [nvarchar](3) NULL, [cust_type] [nvarchar](10) NULL, [obtained_date] [date] NULL, [geo_code] [nvarchar](9) NULL, [email] [nvarchar](80) NULL, [club] [smallint] NULL, ) ON [PRIMARY] GO Any thoughts on improving any part of this code? select SC.Club as [Location], (LTRIM(RTRIM(SC.Cust_Code))+Mem.Mbr_code) as [Customer Number], Replace(LTRIM(RTRIM(mem.fname)),'"','-') as [Customer First Name], LTRIM(RTRIM(mem.lname)) as [Customer Last Name], mem.email as [Customer Email Address], (select 'Personal Training' from ASAJoinmbr ajm where sc.cust_code = ajm.cust_code and reason_code = 'PT' group by cust_code) as PersonalTraining, (select 'Group Fitness' from ASAJoinmbr ajm where sc.cust_code = ajm.cust_code and (reason_code = 'GE' or reason_code = 'GF') group by cust_code) as GroupFitness, (select 'Cardio/Weight'from ASAJoinmbr ajm where sc.cust_code = ajm.cust_code and (reason_code = 'CT' or reason_code = 'CV' or reason_code = 'WT') group by cust_code) as CardioWeight, (select 'Tennis'from ASAJoinmbr ajm where sc.cust_code = ajm.cust_code and (reason_code = 'TE' or reason_code = 'TN') group by cust_code) as Tennis, (select 'Aquatics' from ASAJoinmbr ajm where sc.cust_code = ajm.cust_code and reason_code = 'AQ' group by cust_code) as Aquatics from strcustr SC INNER JOIN ASAMEMBR MEM on SC.cust_code=MEM.Cust_code and sc.club=mem.Club Where DATEDIFF(year,(mem.bdate),GETDATE() ) >19 and (DATEDIFF(day, convert(date,sc.obtained_date,101),GETDATE() )= 14 or (DATEDIFF(day, convert(date,sc.obtained_date,101),GETDATE() )=93 and sc.status='A')) and mem.status = 'A' and mem.email is not null and mem.email <>'' and (sc.bridge_code <> '94811' or sc.geo_code <> 'Goldsmbr') and sc.cust_type<>'E' group by sc.club,sc.cust_code,mem.mbr_code,mem.fname,mem.lname,mem.email Results:
t-sql end of month insert
I have created two database tables named TSALESFACT and DimDate. TSALESFACT table contains the monthly sales target values. And DimTime contains the date values which begins from 01/01/2005. CREATE TABLE [dbo].[TSalesFact]( [DateID] [smalldatetime] NULL, [YearID] [int] NOT NULL, [MonthID] [int] NOT NULL, [SeasonID] [char](10) NOT NULL, [DepartmentID] [char](10) NOT NULL, [SalesAmount] [int] NOT NULL ) CREATE TABLE [dbo].[dim_Date]( [ID] [int] NOT NULL, [Date] [datetime] NOT NULL, [Day] [char](2) NOT NULL, [DaySuffix] [varchar](4) NOT NULL, [DayOfWeek] [varchar](9) NOT NULL, [DOWInMonth] [tinyint] NOT NULL, [DayOfYear] [int] NOT NULL, [WeekOfYear] [tinyint] NOT NULL, [WeekOfMonth] [tinyint] NOT NULL, [Month] [char](2) NOT NULL, [MonthName] [varchar](9) NOT NULL, [Quarter] [tinyint] NOT NULL, [QuarterName] [varchar](6) NOT NULL, [Year] [char](4) NOT NULL, [StandardDate] [varchar](10) NULL, [HolidayText] [varchar](50) NULL) I would like to INSERT these sales target values into a new table named DailySalesTargets. But, the main purpose of this operation is writing the target sales to the each of end of month date. All of the date values for the relevant month will be zero. My desired resultset for this operation is like below: How can I achieve this? Any idea?
I think this is what you are trying to do. insert into [DailySalesTargets]([DateID],[YearID],[MonthID], [SeasonID],[DepartmentID],[SalesAmount]) select d.[Date],d.[Year],d.[Month] ,dptSeason.[SeasonID],dptSeason.[DepartmentID] ,case when EOMONTH(d.[Date])=s.[DateID] then s.[SalesAmount] else 0 end as [SalesAmount] from [dbo].[dim_Date] d cross join (select distinct [DepartmentID],[SeasonID] from [dbo].[TSalesFact]) dptSeason left join [dbo].[TSalesFact] s on d.[Date] = s.[DateID] and s.[DepartmentID]=dptSeason.[DepartmentID] and s.[SeasonID]=dptSeason.[SeasonID]
Trouble migrating data from old tables to a new one
I have two tables that I want to replace with a new one. The old tables: CREATE TABLE [dbo].[OldContacts]( [OldContactID] [int] IDENTITY(1,1) NOT NULL, [ClientID] [int] NULL, [BusinessID] [int] NULL, [Title] [varchar](20) NULL, [FirstName] [varchar](100) NULL, [Surname] [varchar](100) NULL, [JobTitle] [varchar](100) NULL, [EmailAddress] [varchar](100) NULL, [Telephone] [varchar](20) NULL, [Mobile] [varchar](20) NULL, [DecisionMaker] [int] NULL, [PrimaryContact] [int] NULL, [ContactDisabled] [int] NULL, [ZoomInfoPersonId] [int] NULL, [CreationDate] [datetime] NULL, [BusinessListRecordID] [int] NULL) CREATE TABLE [dbo].[Transaction]( [TransactionId] [int] IDENTITY(1,1) NOT NULL, [CreditTransactionId] [int] NOT NULL, [PersonId] [int] NOT NULL, [CompanyId] [int] NOT NULL, [CompanyName] [nvarchar](150) NOT NULL, [ContactName] [nvarchar](150) NOT NULL, [ContactEmail] [nvarchar](150) NULL, [ContactPhone] [nvarchar](50) NULL, [ContactJobTitle] [nvarchar](150) NULL, [EmailBought] [bit] NULL, [AddressLine1] [varchar](100) NULL, [AddressLine2] [varchar](100) NULL, [AddressLine3] [varchar](100) NULL, [County] [varchar](100) NULL, [Town] [varchar](100) NULL, [Country] [varchar](100) NULL, [Telephone] [varchar](100) NULL, [Website] [varchar](500) NULL, [Industry] [varchar](100) NULL, [BusinessID] [int] NULL) And the new one: CREATE TABLE [dbo].[ClientContacts]( [ClientContactID] [int] IDENTITY(1,1) NOT NULL, [ClientUserID] [int] NULL, [PersonID] [int] NULL, [BusinessID] [int] NULL, [CompanyID] [int] NULL, [CompanyName] [varchar](50) NULL, [ContactName] [varchar](100) NULL, [ContactFirstName] [varchar](50) NULL, [ContactSurname] [varchar](50) NULL, [ContactEmail] [varchar](50) NULL, [ContactPhone] [varchar](50) NULL, [ContactMobile] [varchar](50) NULL, [ContactJobTitle] [varchar](50) NULL, [EmailBought] [bit] NULL, [AddressLine1] [varchar](50) NULL, [AddressLine2] [varchar](50) NULL, [AddressLine3] [varchar](50) NULL, [County] [varchar](50) NULL, [Town] [varchar](50) NULL, [Country] [varchar](50) NULL, [CompanyTelephone] [varchar](50) NULL, [CompanyWebsite] [varchar](50) NULL, [Industry] [varchar](50) NULL, [DecisionMaker] [bit] NULL, [PrimaryContact] [bit] NULL, [ContactDisabled] [bit] NULL, [CreationDate] [datetime] NULL, [BusinessListRecordID] [int] NULL, [IsPurchased] [bit] NULL ) The old logic was, you created a record on Transaction, and it would then add a record to OldContacts, creating the redundancy we are now trying to get rid of. However, you could have a record on OldContacts that didn't come from a Transaction. Now, I need to migrate the data to the new table, getting all the Transaction records plus the OldContacts that aren't duplicates of the Transaction ones. And here is what I've got so far: INSERT INTO dbo.ClientContacts ( ClientUserID, PersonID, BusinessID, CompanyID, CompanyName, ContactName, ContactFirstName, ContactSurname, ContactEmail, ContactPhone, ContactMobile, ContactJobTitle, EmailBought, AddressLine1, AddressLine2, AddressLine3, County, Town, Country, CompanyTelephone, CompanyWebsite, Industry, DecisionMaker, PrimaryContact, ContactDisabled, CreationDate, BusinessListRecordID, IsPurchased ) SELECT CT.ClientUserId, LC.ZIPersonId, LC.BusinessID, T.CompanyId, T.CompanyName, CONCAT(LC.FirstName, ' ', LC.Surname), LC.FirstName, LC.Surname, LC.EmailAddress, LC.Telephone AS ContactPhone, LC.Mobile AS ContactMobile, LC.JobTitle, T.EmailBought, T.AddressLine1, T.AddressLine2, T.AddressLine3, T.County, T.Town, T.Country, T.Telephone, T.Website, T.Industry, LC.DecisionMaker, LC.PrimaryContact, LC.ContactDisabled, LC.CreationDate, LC.BusinessListRecordID, CASE LC.ZIPersonId WHEN 0 THEN 0 ELSE 1 END FROM OldContacts LC LEFT OUTER JOIN dbo.[Transaction] T ON LC.ZIPersonId = T.PersonId LEFT OUTER JOIN dbo.CreditTransaction CT ON T.CreditTransactionId = CT.CreditTransactionId However, this only gets me one record, where the ZIPersonId matches. How can I get the records from both tables without getting duplicates? Here are some test records: INSERT INTO dbo.OldContacts (ClientID, BusinessID, Title, FirstName, Surname, JobTitle, EmailAddress, Telephone, Mobile, DecisionMaker, PrimaryContact, ContactDisabled, ZIPPersonId, CreationDate, BusinessListRecordID) VALUES (13832, 10, 'Mr', 'John55', 'Smith55', 'Admin', 'john55#smith.com', '123456', '123456', 0, 0, 0, 55, GETDATE(), NULL) INSERT INTO dbo.OldContacts (ClientID, BusinessID, Title, FirstName, Surname, JobTitle, EmailAddress, Telephone, Mobile, DecisionMaker, PrimaryContact, ContactDisabled, ZIPPersonId, CreationDate, BusinessListRecordID) VALUES (13832, 10, 'Mr', 'John66', 'Smith66', 'Admin', 'john66#smith.com', '123456', '123456', 0, 0, 0, 66, GETDATE(), NULL) INSERT INTO dbo.OldContacts (ClientID, BusinessID, Title, FirstName, Surname, JobTitle, EmailAddress, Telephone, Mobile, DecisionMaker, PrimaryContact, ContactDisabled, ZIPPersonId, CreationDate, BusinessListRecordID) VALUES (13832, 10, 'Mr', 'John77', 'Smith77', 'Admin', 'john77#smith.com', '123456', '123456', 0, 0, 0, 77, GETDATE(), NULL) -- ****************************************************************************** INSERT INTO dbo.[Transaction] (CreditTransactionId, PersonId, CompanyId, CompanyName, ContactName, ContactEmail, ContactJobTitle, EmailBought, AddressLine1, AddressLine2, AddressLine3, County, Town, Country, Telephone, Website, Industry, BusinessID) VALUES (7965, 77, 123, 'TestCompany', 'John77 Smith77', 'john77#smith77.com', 'Admin', 1, 'AL1','AL2', 'AL3', 'testcounty', 'testtown', 'testcountry', '321456', 'www.test.com', 'someIndustry', 10) EDIT I replaced both joins with LEFT OUTER JOINS and I now have all the records. However, I am not getting some columns: no ClientUserId, CompanyId, CompanyName for contacts that come from the OldContacts table and the isPurchased field is always one, when it shouldn't, it should be 1 if the ZIPersonId is not 0.
It seems you have to replace the INNER JOIN with a LEFT JOIN: INSERT INTO dbo.ClientContacts ( ClientUserID, PersonID, BusinessID, CompanyID, CompanyName, ContactName, ContactFirstName, ContactSurname, ContactEmail, ContactPhone, ContactMobile, ContactJobTitle, EmailBought, AddressLine1, AddressLine2, AddressLine3, County, Town, Country, CompanyTelephone, CompanyWebsite, Industry, DecisionMaker, PrimaryContact, ContactDisabled, CreationDate, BusinessListRecordID, IsPurchased ) SELECT CT.ClientUserId, LC.ZIPersonId, LC.BusinessID, T.CompanyId, T.CompanyName, CONCAT(LC.FirstName, ' ', LC.Surname), LC.FirstName, LC.Surname, LC.EmailAddress, LC.Telephone AS ContactPhone, LC.Mobile AS ContactMobile, LC.JobTitle, T.EmailBought, T.AddressLine1, T.AddressLine2, T.AddressLine3, T.County, T.Town, T.Country, T.Telephone, T.Website, T.Industry, LC.DecisionMaker, LC.PrimaryContact, LC.ContactDisabled, LC.CreationDate, LC.BusinessListRecordID, CASE LC.ZIPersonId WHEN 0 THEN 0 ELSE 1 END FROM OldContacts LC LEFT JOIN dbo.[Transaction] T ON LC.ZIPersonId = T.PersonId LEFT JOIN dbo.CreditTransaction CT ON T.CreditTransactionId = CT.CreditTransactionId This will select the OldContacts even when there are no Transactions or CreditTransaction. To answer your new question: CASE LC.ZIPersonId WHEN 0 THEN 0 ELSE 1 END Should be CASE T.PersonId WHEN 0 THEN 0 ELSE 1 END
try an union first and then deselect all duplicates in the UNION of OLDCONTACTS and TRANSACTION. Its hard to replicate your case, I would try something like this: select the-cols-you-need from ( select a.the-cols-you-need, a.distinct(foreign-key) from ( select * from oldcontacts union all select * from transaction ) as a ) inner join CreditTransaction CT on a.transactionId = CT.transactionId
Mssql old table to new table different data types and empties
I have a new table I created for a membership application for a local club, anyway there existing software stored info in a DBF file I can import this into mssql as a table that's no trouble I am just stuck on merging the old data into the new table. This is the old Table Structure (From DBF imported into mssql) [NUMBER] [int] NULL, [LASTNAME] [char](30) NULL, [FIRSTNAME] [char](20) NULL, [TITLE] [char](5) NULL, [SALUTATION] [char](25) NULL, [ADD1] [char](30) NULL, [ADD2] [char](30) NULL, [ADD3] [char](30) NULL, [TOWN] [char](30) NULL, [COUNTY] [char](30) NULL, [POSTCODE] [char](8) NULL, [TELEPHONE] [char](30) NULL, [TYPE] [char](1) NULL, [PAID] [char](1) NULL, [COMMITTEE] [char](30) NULL, [WARNINGS] [int] NULL, [MONTHDRAW] [int] NULL, [WOMANS] [char](1) NULL, [SENIORCIT] [char](1) NULL, [DOB] [datetime] NULL, [AMOUNTCLUB] [decimal](6, 2) NULL, [DATEPAY] [datetime] NULL, [AMOUNTBRAN] [decimal](6, 2) NULL, [AMOUNTDUE] [decimal](6, 2) NULL This is the new Table: [MemberID] [nvarchar](10) NOT NULL, [Title] [nvarchar](10) NOT NULL, [FirstName] [nvarchar](50) NOT NULL, [Surname] [nvarchar](50) NOT NULL, [HouseNumber] [nvarchar](25) NULL, [Street] [nvarchar](25) NULL, [City] [nvarchar](25) NULL, [County] [nvarchar](25) NULL, [Postcode] [nvarchar](10) NULL, [TelephoneNumber] [nvarchar](15) NULL, [MobileNumber] [nvarchar](15) NULL, [EmailAddress] [nvarchar](50) NULL, [DOB] [date] NULL, [Age] [nvarchar](10) NULL, [SeniorCitizen] [nvarchar](5) NULL, [Warnings] [nvarchar](50) NULL, [MembershipCategory] [nvarchar](20) NULL, [ServiceBranch] [nvarchar](20) NULL, [Paid] [nvarchar](10) NULL, [ToPay] [float] NULL, [DatePaidIn] [date] NULL, [Entry] [tinyint] NULL, [FOB] [nvarchar](10) NULL, [JoinDate] [date] NULL, [SubscriptionYear] [date] NULL, [TotalPaid] [float] NULL, I am writing the insert like; INSERT INTO [dbo].[MemberDetails] ([MemberID],[Title],[FirstName],[Surname],[HouseNumber],[Street],[City],[County],[Postcode],[TelephoneNumber],[MobileNumber],[EmailAddress],[DOB],[Age],[SeniorCitizen],[Warnings],[MembershipCategory],[ServiceBranch],[Paid],[ToPay],[DatePaidIn],[Entry],[FOB],[JoinDate],[SubscriptionYear],[TotalPaid]) SELECT [NUMBER], [TITLE], [FIRSTNAME], [LASTNAME], [ADD1], [ADD2], [TOWN], [COUNTY], [POSTCODE], [TELEPHONE] FROM [dbo].[MEMBERS] GO I got as far as that column then realized "how do I do empty values as no one in the old table had a mobile record?" also do I need to do Select AS "new column name" for the old table values?
Suppose you go from OldTable to NewTable, adding an answer column to the original question column: insert NewTable (question, answer) select question , '42' -- Specify a literal or default value from OldTable You can also omit the new column from the insert list: insert NewTable (question) select question from OldTable This will copy the question column and put the default value null in the answer column. You do not have to use aliases in the select list. It's strictly order based. This would work fine: insert NewTable (question) select question as CeciNEstPasUnePipe from OldTable
Joining multiple columns in one table to a single column in another table
I am looking to create a view that pulls data from two tables "Schedule" and "Reference". Schedule has 50+ columns (it's almost completely denormalized -- not my design), most of which contain a value that could be joined to a column in the Reference table. How do I write the SQL statement to correctly join each column in Schedules to the single column in Reference? The Schedule table is defined as: CREATE TABLE [dbo].[Schedule]( [ID] [int] NOT NULL, [SCHEDULEWEEK] [datetime] NOT NULL, [EMPNO] [numeric](10, 0) NOT NULL, [EMPLNAME] [varchar](32) NULL, [EMPFNAME] [varchar](32) NULL, [EMPSENDATE] [datetime] NULL, [EMPHIREDATE] [datetime] NULL, [EMPTYPE] [char](1) NULL, [EMPSTATUS] [char](1) NULL, [SNREFUSALS] [tinyint] NULL, [QUALSTRING] [varchar](128) NULL, [JOBOVERSHIFTTYPE] [bit] NULL, [SHORTNOTICE] [bit] NULL, [SHORTNOTICEWAP] [bit] NULL, [SHORTNOTICEPHONE] [varchar](32) NULL, [LEADHAND] [bit] NULL, [DUALCURRENCY] [bit] NULL, [MIN100WINDOW] [bit] NULL, [STATHOLIDAY] [bit] NULL, [AREAOVERHOURS] [bit] NULL, [DOUBLEINTERZONES] [bit] NULL, [MAXDAYSPERWEEK] [tinyint] NULL, [MAXHOURSPERWEEK] [numeric](10, 2) NULL, [MAXHOURSPERSHIFT] [numeric](10, 2) NULL, [MAXDOUBLESPERWEEK] [tinyint] NULL, [ASSIGNEDDAYS] [tinyint] NULL, [ASSIGNEDHOURS] [numeric](10, 2) NULL, [ASSIGNEDDOUBLES] [tinyint] NULL, [ASSIGNEDLOAHOURS] [numeric](10, 2) NULL, [SHIFTNO1] [int] NULL, [TEXT1_1] [varchar](64) NULL, [TEXT2_1] [varchar](64) NULL, [DAYFLAG1] [bit] NULL, [COMMENT1] [text] NULL, [SHIFTNO2] [int] NULL, [TEXT1_2] [varchar](64) NULL, [TEXT2_2] [varchar](64) NULL, [DAYFLAG2] [bit] NULL, [COMMENT2] [text] NULL, [SHIFTNO3] [int] NULL, [TEXT1_3] [varchar](64) NULL, [TEXT2_3] [varchar](64) NULL, [DAYFLAG3] [bit] NULL, [COMMENT3] [text] NULL, [SHIFTNO4] [int] NULL, [TEXT1_4] [varchar](64) NULL, [TEXT2_4] [varchar](64) NULL, [DAYFLAG4] [bit] NULL, [COMMENT4] [text] NULL, [SHIFTNO5] [int] NULL, [TEXT1_5] [varchar](64) NULL, [TEXT2_5] [varchar](64) NULL, [DAYFLAG5] [bit] NULL, [COMMENT5] [text] NULL, [SHIFTNO6] [int] NULL, [TEXT1_6] [varchar](64) NULL, [TEXT2_6] [varchar](64) NULL, [DAYFLAG6] [bit] NULL, [COMMENT6] [text] NULL -- Snip ) ON [PRIMARY] And the Reference table is defined as: CREATE TABLE [dbo].[Reference]( [ID] [int] NOT NULL, [CODE] [varchar](21) NOT NULL, [LOCATIONCODE] [varchar](4) NOT NULL, [SCHAREACODE] [varchar](16) NOT NULL, [LOCATIONNAME] [varchar](32) NOT NULL, [FLTAREACODE] [varchar](16) NOT NULL ) ON [PRIMARY] I am trying to join each [TEXT1_]/[TEXT2_] column in Schedule to the [SCHAREACODE] column in reference. All the reference table contains is a list of areas where the employee could work.
I think he means to join on the Reference table multiple times: SELECT * FROM Schedule AS S INNER JOIN Reference AS R1 ON R1.ID = S.FirstID INNER JOIN Reference AS R2 ON R2.ID = S.SecondID INNER JOIN Reference AS R3 ON R3.ID = S.ThirdID INNER JOIN Reference AS R4 ON R4.ID = S.ForthID
Your description is a bit lacking, so I'm going to assume that Schedule has 50+ columns (it's almost completely denormalized -- not my design), most of which contain a value that could be joined to a column in the Reference table. means that 1 of the 50+ columns in Schedule is a ReferenceId. So, given a table design like: Schedule ( MaybeReferenceId1, MaybeReferenceId2, MaybeReferenceId3, ... ) Reference ( ReferenceId ) Something like: SELECT * FROM Schedule JOIN Reference ON Schedule.MaybeReferenceId1 = Reference.ReferenceId OR Schedule.MaybeReferenceId2 = Reference.ReferenceId OR Schedule.MaybeReferenceId3 = Reference.ReferenceId OR Schedule.MaybeReferenceId4 = Reference.ReferenceId ... would work. You could simplify it by using IN if your RDBMS supports it: SELECT * FROM Schedule JOIN Reference ON Reference.ReferenceId IN ( Schedule.MaybeReferenceId1, Schedule.MaybeReferenceId2, Schedule.MaybeReferenceId3, Schedule.MaybeReferenceId4, ... )
From updated question Perhaps something like this? It will be messy no matter what you do. SELECT S.ID S.TEXT1_1, TEXT1_1_RID = COALESCE((SELECT MAX(R.ID) FROM Reference R WHERE R.SCHAREACODE = S.TEXT1_1), 0), S.TEXT1_2, TEXT1_2_RID = COALESCE((SELECT MAX(R.ID) FROM Reference R WHERE R.SCHAREACODE = S.TEXT1_2), 0), ... FROM Schedule S
Agree with TheSoftwareJedi, but can I just suggest using LEFT JOINs so that failures-to-match don't cause your Schedule row to disappear? Of course, doing 28 JOINs is going to be a bit cumbersome whatever the details. I'm not sure I'd call this "denormalized", more "abnormalized" ... :-)
Try a query like this: select s.*, r.schareacode from schedule s, where s.text1_1 = s.schareacode or s.text2_1 = s.schareacode or s.textx_x = s.schareacode .. You should be able to get the same results with traditional joins so I recommend you experiment with that as well.