In python one can eaisly pivot a table on number of columns without specifying the list of columns(dynamic columns) using pandas. One example can be:
pivotedDf = pd.pivot_table(df,index=['ID','Category','Position','Percentage','Type','Location'],values='Value',columns=['Duration','year'],aggfunc='sum').reset_index()
The above code segment will pivot the dataframe based on two colums(Duration, Year). The distinct list of Duration & Year columns is big and different on each calculation.
How can I achieve the same using SQL in snowflake?
Related
I wrangled my data through python but I am now asked to do the same in SQL. I'm trying to pivot the data in the picture below so the values in 'naam_installation' are the columns, the column 'value' will be the different values and the 'Woning', 'Klant', 'Soort_woning' and 'tijd' will be separate columns. I want it to be like the second screenshot (datetime = tijd).
SQL table to be changed
How I want to data done in python.
I have a table with >50 columns. I have the names of two columns. However, I would like to sum the values of these 2 columns as well as all the columns between them (>50). It seems very impractical to type all the column names in a sum statement. Is there a query for this? I am using SQL Impala by the way.
How to group multiple columns under a single column in data table visualization
Please see attached image
#BULB - You can transform your table by unpivoting and pivoting data to get the desired result.
I first un-pivoted the original data table by applying transformation.
INSERT > Transformations
Then pivoted the unpivot output by applying another transformation.
Note: You can apply both the transformations simultaneously.
Final Output:
PIVOT can only pivot one value into its one column.
Also PIVOT is not helpful if for examle we want to pivot and (group by) intersecting ranges of values.
So we're planning to use MODEL clause to overcome this limits.
We have to pivot into columns:
sum(), count() of data over separate quarters for last 2 years and
present each quarter's result into its own set of column;
sum(), count() of data over separate years for last 2 years and present each years's result into its own set of column;
sum(), count() of data over separate months for last 3 months and present each month's result into its own set of column.
The list of quarters / years / months in the bucketing is fixed and can be hard-coded in the MODEL clause (i.e. it'll be always to look back 2 years).
I have a working prototype doing above using three separate pivots, but it is very inefficient because each pivot has to pass our huge dataset again.
We expect MODEL might require just one pass over the dataset.
Questions:
Is it possible for MODEL clause to create new columns (ie. we group
by month and product category in a subquery, but MODEL should add
columns for the above quarter/year/month buckets)?
Can't think of a best way to define DIMENSION BY and MEASURES so we could
create new columns.. any ideas are highly appreciated.
Edit: posted the same question at https://community.oracle.com/message/13951429
You can PIVOT with multiple pivot columns and multiple result columns
with atc as
(select owner, table_name, column_name, data_type, nullable
from all_tab_columns
where table_name = 'ALL_TAB_COLUMNS')
select *
from atc
pivot
(max(column_name) as max_col_name, min(column_name) as min_col_name
for (data_type, nullable) in
(
('NUMBER','N') as dt_number_n, ('VARCHAR2','N') as dt_vc_n,
('NUMBER','Y') as dt_number_y, ('VARCHAR2','Y') as dt_vc_y
)
)
Looking for a simple way to kind-of transpose a table to be able to further analyze data in software with no DB capabilities.
Table:
Id testdate partid testid
1 2-13-2014 45 58
2 2-23-2014 45 2
I want to extract a table from this that puts testid in the column name and date in its fields, thus looks like this:
partid test-1 test-2 test-3 ... test-58 ...
45 2-23-2014 2-13-2014
There could be a few 100 testid's. I plan to expand the code to multiple columns per testid, eg: test-1-date test-1-result test-1-success.
Prefer common SQL, but if it has to be specific I'd be MS SQL server.
Okay, found it, its "PIVOT" that I needed. When searching PIVOT there are many questions and answers on this site.
One thing that is different in my case compared with most other PIVOT examples is that I try to aggregate strings rather than numbers. (Think of the date field as a string)
Specifically helpful were:
How to create a pivot query in sql server without aggregate function
Get ROWS as COLUMNS (SQL Server dynamic PIVOT query)
Convert Rows to columns using 'Pivot' in mssql when columns are string data type
Pivot table strings grouping under pivot column?