how do I wrap an sql quesry?

조회 수: 4 (최근 30일)
David malins
David malins 2012년 3월 6일
Hi I have an SQL query that I wish to split over several lines. However, because the quesry needs to go inside quotes I have tried terminating each line with and closing quote and the three dots '...
This is giving a parsing error. Can someone help please
SELECT T1.TIME, MAX(CASE WHEN T2.ID = 1 THEN CAST(T1.VALUE AS FLOAT32) ELSE NULL END) AS VALUE1, MAX(CASE WHEN T2.ID = 2 THEN CAST(T1.VALUE AS FLOAT32) ELSE NULL END) AS VALUE2, MAX(CASE WHEN T2.ID = 3 THEN CAST(T1.VALUE AS FLOAT32) ELSE NULL END) AS VALUE3 FROM PIARCHIVE..PIINTERP2 T1 INNER JOIN (SELECT 1 ID, 'WWL_T1_WindSpeed_10MIN_AVG' TAG UNION SELECT 2 ID, 'WWL_T2_WindSpeed_10MIN_AVG' TAG UNION SELECT 3 ID, 'WWL_T3_WindSpeed_10MIN_AVG' TAG) T2 ON T2.TAG = T1.TAG WHERE T1.TAG IN ('WWL_T1_WindSpeed_10MIN_AVG', 'WWL_T2_WindSpeed_10MIN_AVG', 'WWL_T3_WindSpeed_10MIN_AVG') AND T1.TIME BETWEEN '*-7d' AND '*' AND TIMESTEP = '10m' GROUP BY T1.TIME ORDER BY T1.TIME
cheers dave

채택된 답변

the cyclist
the cyclist 2012년 3월 6일
Here is my technique for wrapping SQL queries:
queryText = '';
queryText = [queryText,'select field from table ']; % Note the space at the end
queryText = [queryText,'where constraint is true '];
queryText = [queryText,'and another_constraint is true '];
queryText = [queryText,'order by field '];
This produces one long string with the whole query, but keep the different parts of the query manageable. It is also convenient for being able to paste the query into other software for checking.
Also, bear in mind that for the single quotes that appear inside the query, you will need to double those for MATLAB to parse it correctly. For example
queryText = ['select * from table where name=''the cyclist'' ']

추가 답변 (1개)

David malins
David malins 2012년 4월 27일
Hi there, thank you for the help with SQL wrapping, works a treat
regards Dave

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by