I need some help in creating a stored procedure or a dynamic SQL query to query a matrix and have a output in a required format.
Database: SQL Server 2014
States table - screenshot:
Required output
More states will added to the table. So the Script should be able to dynamically get all the rows and columns.
The output is to create a new table.
Thanks in advance.
Try this below links for your reference it may help you for logic
How to pivot table in SQL Server with a stored procedure?
Dynamic Pivot Queries with dynamic dates as column header in SQL Server
Note: The above links for pivoting.better refer it and do the unipovt
Related
So I have a DAX query that I have built in SSAS. But since my application like DAX, I thought pushing the set of DAX results to a database server table would be the ideal solution for my application to read.
How do I output the contents of a DAX query to a SQL Server database table? And if possible truncate the contents of the table before each run?
I'm using SQL Server 2016 if that helps.
I got around this by using a linked server to output the contents of an 'open query' to a table variable in SQL.
I am using SQL Server 2008 and want to convert table data into json format. Can I convert it directly through firing query?
I have created a stored procedure that can take your table and output JSON. You can find it at
GitHub - SQLTableOrViewToJSON
Once you run the stored procedure, you can output your table by making a call like the following:
EXEC SQLTableOrViewToJSON 'MyTable', 'C:\WhereIStowMyJSON\'
Built in support for formatting query results is added in SQL Server 2016 and it will be available in Azure Database.
In older versions you would need to use CLR or some heavy TSQL like:
Producing JSON Documents from SQL Server queries via TSQL
The above solutions seem unnecessarily complicated; starting with 2008, SQL Server supports the following syntax (answer from DBA StackExchange)
SELECT * FROM dbo.x FOR JSON AUTO;
The above query returns a single JSON-formatted column that looks like this (assuming dbo.x contains columns col1~col4of corresponding types):
[{"col1":"val1","col2":"val2","col3":5,"col4":"2019-02-11"}]
More details/options here: https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server
ODBC CONNECT TO database_name;
SQL
EXEC procedure_name arg_1, arg_2
I use above code in Qlikview to retrieve the return tables from SQL Server.
However, there are three tables returned from the procedure.
Qlikview shows only one table and its columns.
How to retrieve more than one tables from a procedure?
Thank you very much!
The query results are returned as a multiple result set. The documentation below may help you navigate a multiple result set from Sql server using qlik.
Using multiple result sets served by SQL Server stored procedures
SO heres what I am trying to do.
I have a table which has 2 columns - QC_Check and Query. For each QC_Check I have a query. There are several records like this.
Is there a way using SQL transformation that, I can fetch the SQL query stored in the Query column to Informatica, run the queries in Teradata and get the results stored somewhere.
Although, I have not tried it myself, this should be possible using SQL transformation in Query mode with Dynamic SQL Queries.
Use the table with Query column as a source. Create a SQL transformation with Query mode. Connect the Query column to the SQL transformation.
Write ~Query_Port~ in the SQL editor in the SQL transformation:
If you want to capture the results from your query, you have to configure output ports for columns you retrieve from the database.
I want to write a SQL statement that uses a pivot in SQL Server 2000. The PIVOT keyword is not available in SQL Server 2000 so I found some examples that use a case statement but that requires that you know the column names beforehand which I won't. How do I do a pivot which dynamically generates the column names from the data it has available to it?
We create SQL commands with the CASE statements from our application and fire them at the database (any database, not specifically SQL server). First we determine the number of pivot columns and their names using one query, from those results we generate the next query.
So the first query to determine the columns looks somewhat like:
SELECT DISTINCT myField FROM myTable
Then we use all the values in this result to construct an SQL command where a CASE statement is generated for each value.
We wanted a databasebase agnostic solution so we do this processing outside the database but i'm sure you could do the same in a stored procedure withing SQL server itself.
I have not tried to replicate PIVOT on SQL Server 2000 but what I have done is use PIVOT when I do not know the column names beforehand. I had used ROW_NUMBER() to determine the column names instead. You can try that.