Dear Experts,I'm fairly new to the world of Pivots and Group By clauses and am having a challenging time calculating TEUS of containers in our shipment system.Basically I have relational tables for Shipments and Containers which have been linked in a many-to-many relationship using an intermediate table as follows:ShipmentsShipmentIDSH_SOB*ContainersContainerIDCO_Type*Relate_Shipments_ContainersRSC_ShipmentLinkRSC_ContainerLinkNext, I have a table called ContainerTypes linked to the Containers table via the ContainerType field in a single-many relationship.ContainerTypesContainerTypeIDCT_DescriptionThe ContainerTypes listed are "20ft" and "40ft"The SH_SOB field in the Shipments table is a date of the shipment and each shipment may contain any number of linked containers each of a different single ContainerType.What I have so far achieved is a count(*) of the number of "20ft" containers falling within each month of the year 2009 (Based on the SH_SOB Date) using a Pivot Table.Here is my T-SQL that does this:SELECT [1] as Jan, [2] as Feb, [3] as Mar, [4] as Apr, [5] as May, [6] as Jun, [7] as Jul,
as Aug, [9] as Sep, [10] as Oct, [11] as Nov, [12] as [Dec] FROM ( SELECT Containers.ContainerID, MONTH(Shipments.SH_SOB) AS SHMonth FROM ContainerTypes INNER JOIN Containers INNER JOIN Shipments INNER JOIN Relate_Shipments_Containers ON Shipments.ShipmentID = Relate_Shipments_Containers.RSC_ShipmentLink ON Containers.ContainerID = Relate_Shipments_Containers.RSC_ContainerLink ON ContainerTypes.ContainerTypeID = Containers.CO_Type_Link WHERE (YEAR(Shipments.SH_SOB) = '2009' AND ContainerTypes.CT_Description like '%20%') GROUP BY Containers.ContainerID, Shipments.SH_SOB) pvt PIVOT (Count(ContainerID) FOR SHMonth In ([1],[2],[3],[4],[5],[6],[7],
,[9],[10],[11],[12])) as child
Now, the same is logical for 40ft.However!To get the TEUS of each container type, I need a multiple of 1 for each 20ft container count (Which is basically the count(*) as above) but for the 40ft, I need a multiple of 2 (I.e. Double the count(*))E.G. (2 x 20ft = 2*1 = 2 TEUS)+(2 x 40ft = 2*2 = 4 TEUS) = 6 TEUS in Total for that month.How, on this earth, do I get the 20ft count added to the 40ft double-count within the same format as my original pivot table over months???E.G.Type Jan Feb Mar etc...20ft 2 1 740ft 4 6 8 (These are double counts of 2, 3 and 4)-------------------------Total 6 7 15 (I just need this row at best!!!)I hope this makes sense...All help will be appreciated, code even more...xR