Separating row data out into columns - sql

I have a customers table that houses contacts. Each contact is added as another row to that customer.
I want to create my query so that I can create columns for contact1, contact2, contact3 and so forth. What's the best way to do this?
My rows are dynamic and I have been looking into unpivot and pivot but I'm not sure how I would build that into my query as I have a few joins and other data in the query.
SQL Fiddle example

Dynamic pivot query is what you need. See this:
http://buysql.com/mysql/14-how-to-automate-pivot-tables.html
and this:
SQL Server dynamic PIVOT query?

Related

Dynamic columns in reporting services for an SQL pivot table

I am having trouble with a pivot table that changes the amount of columns depending on the data, so I want to show a "dynamic table" in SSRS according to my SQL table. Any idea of what I can do to show that pivot table in SSRS?

Can we apply a for loop on select sql statement?

I have a SQL table with 150+ columns and I want to apply on them an aggregation function when selecting values but I don't to list all column names by hand. Instead I want to use a for loop through the column names of the table.
I want to do something like this:
SELECT
AGREEMENT_NO,
COUNT(DISTINCT column) FOR column LOOP columns -- Instead of 150+ lines of count
WHERE ...
GROUP BY AGREEMENT_NO
Does anyone know if it's possible to do it in SQL and if yes how?
This can be done by dynamic query.
Take every column of a table by in temporary table by Information_Schema.columns.
Take a while loop for temporary table for all row.
Write dynamic query and concat column name for your requirement.
Execute the query. Eg. Exec("Query" / variable).
This will give you exact result.

Is there a way to query multiple tables at once based on common table name in Oracle through SQL or PL/SQL

I have a requirement to fetch data from all the tables based on common table name pattern every week.Basically my requirement is to merge all the data to form a single lookup table.
Example:
Table Names:
Department_20190101
Department_20190109
Department_20190122
Department_20190129
I have to fetch data from all the tables and have to create a single lookup table. Is there a simple way to do this other than by going through iteration in PL/SQL by getting the table names with the help of ALL_TABLES
Note:The Date part is not consistent.If i can achieve this requirement once then i can easily start inserting the data from the new table to the existing lookup(n+1)
Please share your suggestions.
Regards,
Scott
If you have a very long list of tables, or you requirement is to aggergate the results from all tables e.g. starting with Department_ followed by a certain date or range of dates, then you might need dynamic SQL for this. For the exact example you did show in your question, a CTE with a union query might work:
WITH cte AS (
SELECT * FROM Department_20190101 UNION ALL
SELECT * FROM Department_20190109 UNION ALL
SELECT * FROM Department_20190122 UNION ALL
SELECT * FROM Department_20190129
)
And then use the CTE as:
SELECT *
FROM cte;
This assumes that all tables have identical structures. Also, as a side note, if this be the case, you might want to consider just having a single table with a date column to differentiate between the otherwise common data.
Check here: Execute For Each Table in PLSQL there is nice example to resolve Your problem using pl/sql (pl/sql help You dinamicly grow up sql-query) .

SQL to transpose table

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?

SQL Server : Querying Multiple Rows, Returning In One

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