Hi Chiu I Keanu Leung ,
Welcome to Microsoft Q&A platform and thanks for posting your query here.
It seems like you've created multiple inputs in Azure Stream Analytics (ASA) for different consumer groups, and you're using a SQL query to filter and route data from an IoT hub into different outputs and looking for suggestion on improvement.
You can optimize the query for readability . Instead of creating multiple CTEs (Common Table Expressions) for each 'Project,' you can simplify your query by using a single query with conditional filtering. Here's an example:
SELECT
CAST(project AS nvarchar(max)) AS Project,
CAST(device AS nvarchar(max)) AS Device,
CAST(time AS datetime) AS Time
INTO [sql-pj001-J0000]
FROM [iothub-pj001-grpA]
WHERE Project IN ('J0000', 'J0001', 'J0002', 'J0003', 'J0004');
SELECT
CAST(project AS nvarchar(max)) AS Project,
CAST(device AS nvarchar(max)) AS Device,
CAST(time AS datetime) AS Time
INTO [sql-pj001-J0005]
FROM [iothub-pj001-grpB]
WHERE Project = 'J0005';
Hope it helps. Thankyou