I am looking for the best table structure to create a dynamic SQL statement like this below (which is not dynamic yet). I have to choose between
- joined tables
- a single row with all columns with the content comma-delimted which I then will parse
- one large table with multiple rows per Cost Centre Activity Code
- or anything else
In this example the key which all link to is: 'NSEA8102' which is a Cost Centre Activity code
SELECT
@pDate,
@pDate,
'NSEA8102', --Cost Centre Activity Code
ccg.tCCGroup,
SUM(logs.tTripHours) AS tTriphours,
'Actual EMV Hours Worked - ' + DATENAME(MONTH,@pDATE) + ' ' + CAST(YEAR(@pDate) AS CHAR(4))
FROM dbo.tblEMV_Logsheet AS logs INNER JOIN
dbo.tblLookup_EMVEquipment AS ccg ON logs.tEquipmentKey = ccg.tEquipmentKey
WHERE tDate BETWEEN @BMonth and @EMonth
AND (logs.tAreaCode in ('MINEE', 'SERVICE'))
AND (logs.tEventCode LIKE 'RASSTEEPS')
AND logs.tSourceLocationCode = 'STEEPS'
AND logs.tDestinationLocationCode = 'ERASSTSP'
AND (ccg.tCCGroup IN ('FADT', 'FPC800', 'FWA800'))
AND ccg.tValid = 1
GROUP BY ccg.tCCGroup
Any suggestion would be welcome. Thanks