Skip to content

Tag: pivot

Postgres: Count multiple events for distinct dates

People of Stack Overflow! Thanks for taking the time to read this question. What I am trying to accomplish is to pivot some data all from just one table. The original table has multiple datetime entries of specific events (e.g. when the customer was added add_time and when the customer was lost lost_time). Th…

dynamic pivot SQL Query in DB2

I have table like below in DB2 where all these 3 columns are VARCHAR data type I need a dynamic PIVOT query which should select values for ABC_COL1 and ABC_COL2 in row format when FILE_NM filter is used as ABC.TXT The same query should select the values for XYZ_COL1, XYZ_COL2 and XYZ_COL3 in row format when F…

PL SQL Pivot: use different aggregates

I want to pivot sum(sales) per country as below. But the price column should be an average per shop. So, I would love to have this result: Desired Result: shop avg_price US CN 2 6 100 10 3 5 60 10 Current Result: shop price US CN 2 5 null 5 2 7 100 5 3 5 60 10 I

SQL – Adding columns

I am new to SQL and was wondering if someone can help me with this small request. I am trying to have my output like down below: But I am currently getting this output: Here is my SQL code: How can I get the columns to reflect MF, CH_A, CH_B, and CH_C with its respective value? Answer @Med2020 Your query

Flatten the Data in BigQuery

I have dimensions.key_value of RECORD type i run the following query with following output. To unnest the data i have created the following view: My Output: There are two thing i need to verify is my query correct ? How i can make the query generic i.e if i don’t know all the key, there some other keys …