Skip to content
Advertisement

How to use PIVOT on SQL with additional criteria like colum count check and grouping?

Present output of the below SQL query is :

2 columns – ensuring the number of output is exactly ten.

How can I transpose it to :

11 columns.

Ensuring that there are all the ten records for the same specific time , else skip to the next minute (next record). I am assuming that it is sorted by time,by default else can I use order by – is it optimum.

Using a standalone vbs file in windows against data in MS Access.


Eg. For understanding : The data table is :

Eg. Apple data is for 3:22 , so skip and move onto next. But ensure for the same time – the count is two (in this case Apple+Amazon) ten in my case. CE1 = Apple , PE1 = Amazon Earlier Output :

New Output required :

Ticker is matched with CE1 , CE2 , PE2 etc.

Thanks

Advertisement

Answer

If data resides in MS Access, consider the crosstab query which you can explicitly specify the 10 columns with PIVOT clause:

Alternatively, run the conditional aggregation, again explicitly specifying the 10 columns with 10 expressions. This approach allows the flexibility of renaming columns such as suffixing _Value.


Should values need to be passed into query, as I recommended in your last question consider ADO parameterization. Alternatively, with DAO (Access’ default API) you can save a prepared statement as an Access query object using PARAMETERS clause and bind values in code using QueryDefs at runtime. Both approaches avoid messy concatenation and punctuation as seen in your mysql variable.

DAO

SQL (save as an Access query; no quotes or concatenation)

VBA

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement