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.
Related
I have a group of two SQL tables in postgres. A staging table and the main table. Among the variety of reasons for the staging table, the data i am uploading has irregular and different formats for all of the date columns. During the upload process these values go into staging table as varchars to be manipulated into usable formats.
In the main table the column type for the date fields is of type 'date' in the staging table they are of type varchar.
The question is, does postgres support a copy expression similar to
insert into production_t select *,textdate::date from staging_t
I need to change the format of a single field during the copy process. I know i can individually type out all of the column names during the insert and typecast the date columns there, but this table has over 200 columns and is one of 10 tables with similar issues. I want to accomplish this insert+typecast in one line that i can apply to all tables rather than having to type 2000+ lines of sql queries.
You have to write every column of such a query, there is no shorthand.
May I say that a design with 200 columns is questionable.
background
I am trying to create an SSRS report which will run select * on a table passed in as a parameter and display all the data from that table. As I understand it, I can't use a table for this. I want to use a pivot table to achieve this.
select * from #table will return something like this (from the adventureworks DB)
I want to display the date in this format:
Question
How do I achieve this? I looked at using PIVOT/UNPIVOT, but all the examples I've seen use static column names and aggregates.
I won't know the column names at design time (or run time), I'm assuming I will need column headers like 'table name', 'value1', 'value2' etc?
Limitations
I won't have access to create stored procedures. Ideally, the report should be able to be run entirely from SSRS without having to create new tables etc.
Performance is not a concern.
Edit
Editing to add some clarity. The column names in the example above are only an example. The #table parameter could be any table, column names won't be know at design time. The column names could be col1, col2, or name, address... etc.
You can do this with a normal tablix, with a column group on BusinessEntityID
Create a normal tablix.
Remove the row group (group only)
Insert a new parent column group, grouped by BusinessEntityID
Delete the top row (row only)
Add each of the row titles in, as per your suggested output
Add each of the values in the second column
Say I wanted to group all my metrics that I have in my values in my pivot table(including all added calculated fields) into one field to be able to use in the Report Filter.
So I want all those values in a drop down to be able to select a metric and it populates my pivot table below.
Is that possible with VBA Code? Am I explaining clearly?
Problem
We are currently working with some data which is stored as in the screenshot below. We are using this to display information in a C# application. The only way we can get the application to work as needed is to have the information in one single row.
We have tried some methods on Display multiple rows and column values into a single row, multiple column values to no avail.
Desired output
We require each row with the same date and vehicleID to be on the same row to be output from our stored procedure.
Any help would be greatly appreciated. Cheers.
if I understood correctly, you don't know how many unique date/vehicle_id combinations there are in the data. hence you can't just do a pivot over a set list of values.
in this case, you would need to build up a list of the date/vehicle ids dynamically, and then pivot over that list.
please check out the following examples involving dynamic SQL and pivot tables:
PIVOT in sql 2005
SQL Server : dynamic pivot over 5 columns
Pivot Dynamic Columns, no Aggregation
https://www.simple-talk.com/blogs/2007/09/14/pivots-with-dynamic-columns-in-sql-server-2005/
SQL Server dynamic PIVOT query?
Dynamic Pivot Columns in SQL Server
SQL Pivot Query with Dynamic Columns
I have a requirement to display row values as column names in a data grid view. I want to get the store names into columns using sql select statement. (Please refer the attached image). I want user to enter some values under each column. So STORE 1, STORE 2, STORE 3 should displays as columns in datagrid view. Does anyone can help me to get this work?
while googling i found this can be done using PIVOT in SQL. But in this table i don't have any aggregate columns. Any help pls?
the result should be somthing like
You may know that your data only contains a single row for each pivoting column, but SQL Server has to construct a plan that could accommodate multiple rows.
So, use PIVOT and just use an aggregate that, if passed a single value, will return that same value. MIN() and MAX() fit that description (as does SUM if you're working with numeric data)
You may use specific function of dynamic pivot and pass your query with item count column.
You can use below link which provided you function and can easily show you expected output.
http://forums.asp.net/t/1772644.aspx/1
Procedure name:
[dbo].[dynamic_pivot]