Hi I have 2 tables. A jobs Table and a Drops Table. In the Jobs table we have a collection post code and delivery post code, in the drops table we have the post codes of any additional drops.I am creating a view which will have a “destination” column and in this column we have all the post codes from the Jobs table and the Drops table separated by a comma.The issue I have is that if the drops table has more than one extra drop instead of putting all the post codes onto one line it is creating a line with the collection post code, delivery post code from the jobs table and the 1st post code out of the drops table on one line then creating a new line and showing the collection post code, delivery postcode and 2nd post code from the drops table.I need a way of looping through the drops table to get each post code and put it on the same line as the collection post code and delivery post code from the jobs table.The foreign key in the drops table is the jobid field and this will appear for for each drop and in the drops table we have a dropid field and drop number field. I assume I would have to count the number of jobid’s in the drop table but I have no idea how to loop through the drops table to retrieve the post codes for each drop. Can anyone help please?This is my current code. The section I am querying I have put in bold. Thanks in advance.[code="sql"]CREATE view [dbo].[CarouselDailyMileages] AS SELECT DATENAME(DW,dbo.Jobs.DELDATEANDTIME) as WEEKDAY, dbo.Jobs.DELDATEANDTIME AS "DATE", [b] (dbo.jobs.colpostcode + ' , ' + dbo.Jobs.DELLPOSTCODE + ', ' + dbo.Drops.POSTCODE) AS "Destination", [/b] DBO.Jobs.ACTUALMILEAGE AS "Mileage", dbo.Jobs.PRICE AS "Sale Price", CASE WHEN (select count (*) from jobs j2 inner join driver on driver.DRIVERPRIMARYID = j2.DRIVERPRIMARYID where j2.DRIVERCOST = 0 AND J2.JOBID = JOBS.JobID AND driver.SUBCONTRACTORINDICATOR = 0) > 0 THEN jobs.PRICE - (jobs.PRICE * .30) ELSE JOBS.DRIVERCOST END -- DBO.JOBS.DRIVERCOST AS "COST PRICE", dbo.Vehicle.VEHICLE AS "Vehicle Type", dbo.Jobs.JOBREFERENCE AS "Reference", dbo.Jobs.requestedby, dbo.customer.customerid, dbo.driver.employeenumber,DBO.JOBS.JOBNUMBER, case when dbo.Jobs.requestedby like '%John Deere%' then 1 -- Green when dbo.Jobs.requestedby like '%FSL%' then 2 -- Blue when dbo.Jobs.requestedby = 'Manroland' then 3 -- Orange -- when dbo.Jobs.requestedby = 'John Deere Harvest 2016' then 4 --Pink when dbo.Jobs.requestedby = 'Clothing' then 5 -- Grey when dbo.Jobs.requestedby = 'Community Playthings' then 6 -- Red when dbo.Jobs.requestedby = 'Siemens' then 7 -- Black when dbo.Jobs.requestedby = 'Carousel Siemens' then 7 --Black when dbo.Jobs.requestedby = 'Siemens - adhoc' then 7 -- Black when dbo.Jobs.requestedby = 'OTRS' then 8 --purple when dbo.Jobs.requestedby = 'AGCO' then 9 -- Navy when dbo.Jobs.requestedby like '%Draeger%' then 10 --Dark Red else 20 end as referenceflagFROM dbo.Jobs INNER JOIN dbo.Drops ON dbo.Jobs.JobID = dbo.Drops.JOBID INNER JOIN dbo.Vehicle ON dbo.Jobs.VEHICLEID = dbo.Vehicle.VEHICLEID inner join dbo.customer on dbo.jobs.CUSTOMERID = dbo.customer.customerid inner join dbo.Driver on dbo.jobs.DRIVERPRIMARYID = dbo.driver.DRIVERPRIMARYID [/code]
↧