Skip to content

Tag: sql

SQL pivot and group by month

I’m trying to get the sum of production per month and pivot the month row to columns. Could you please tell me what is wrong with this code?’ Answer There were several items wrong in your query. Columns need aliases and your max() within the PIVOT was referencing an invalid column name

Do i need to cast integer param to string for varchar index to be used?

When i query a varchar column (indexed) against an integer number, it runs extremely slow. I thought mysql was able to infer this and cast the parameter to string, but when i use an integer for filtering it avoids the index. Is this ok? Is it a collation problem? Should i always cast manually integers to stri…

Simplifying SELECT statement

so I have a statement I believe should work… However it feels pretty suboptimal and I can’t for the life of me figure out how to optimise it. I have the following tables: Transactions [Id] is PRIMARY KEY IDENTITY [Hash] has a UNIQUE constraint [BlockNumber] has an Index Transfers [Id] is PRIMARY K…

Returning non-overlapping records within a date range

I have the following data. I have looked over a lot of threads about overlapping and non-overlapping dates but none seemed to help me. From this table with a SQL query I want to return the first record out of overlapping dates or basically I have been struggling a lot with this query and was wondering if this…

BigQuery – Solving a basic grouping/ aggregation error

I wish to output a sum of revenue per year per product, but also include a column with the year of introduction of each product (based on date first ordered). I can not figure out what I am doing wrong here as I can do each calculation separately but not it seems within the same query, I am sure this