Pivot sql query - sql

I've SQL Server 2005 table
COMPETITOR (Id, Title, cDate, Price)
I want to format the query so that its view in Gridview will be like this:
Please help me writing the sql query.

SQL 2005 supports PIVOT - the Books Online doc is http://msdn.microsoft.com/en-us/library/ms177410(SQL.90).aspx
The main shortcoming I've found with the PIVOT stuff is that you must specify the column names, although you can use a query beforehand and inject the values into a varchar variable and then execute that.

Related

Calling Search Parameters in SQL for multiple SQL Scripts.

Is it possible to create a variable like call to be used holding the search parameters for a SQL "Select" call?. I have multiple SELECT SQL queries that use the same parameters for EXCLUDING certain data. That would be in the "NOTLIKE" portion of the code.
Can you have one variable in the SQL code that can be called in multiple SQL scripts is my question.
This will be for MS SQL Server Management Studio v17.8.1 and MS SQL Server 2008.
SELECT distinct DB.[ReportNumber]
,DB.[StatusText]
,DB.[ProjActStageName]
,DB.[ProjectOwnerTypeName]
,DB.[ProjTypeName]
,DB.[ProjWorkTypeName]
FROM [Database]
WHERE DB.ReportNumber = DP.ReportNumber
AND DB.ProjTypeName IN ('Custom House', 'Spec Houses')
AND DB.ProjectName NOT LIKE '%Repair%'
AND DB.ProjectName NOT LIKE '%Replace%'
AND DB.ProjectName NOT LIKE '%Abatement%'
ORDER BY DB.ReportNumber
TO -->
SELECT distinct DB.[ReportNumber]
,DB.[StatusText]
,DB.[ProjActStageName]
,DB.[ProjectOwnerTypeName]
,DB.[ProjTypeName]
,DB.[ProjWorkTypeName]
FROM [Database]
WHERE DB.ReportNumber = DP.ReportNumber
AND DB.ProjTypeName IN ('Custom House', 'Spec Houses')
(Variable excluding certain data)
ORDER BY DB.ReportNumber"
You might want to read this thread for some options on how to approach your problem: SQL Query with NOT LIKE IN

Convert DB2 SQL query to SQL Server

I have to convert a DB2 query to SQL Server, but don't understand what exactly below query does:
SELECT
t.MyColumnA NAME(MyColumnA-01),
t.MyColumnA COLHDG("COA" "VALUE")
FROM
MyTable t
It looks like the NAME and COLHDG functions are just UI functions specific to HelpSystems. The actual query would be
SELECT t.MyColumnA AS "COA VALUE"
FROM MyTable t

How can I use a SQL Server table in an openquery to an Oracle database?

I have a database on SQL Server and would like to use a column in one of my tables in a linked server openquery I'm running to an Oracle database in order to match values between the two and insert the result into columns in my table in SQL Server .
Essentially I want it to be like this:
SELECT col1, col2, col3, col4
FROM OPENQUERY(link, 'SELECT * FROM Oracle_Table
WHERE ID = MSSQL.dbo.table.ID`)
So I'd like to be able to use my internal table column values to query an external database. They are related tables but different systems.
Would it be possible to get a big list of the values in the SQL Server table column and use it as a variable in the Oracle query? I've searched extensively online but haven't been able to find this one.
You can't pass parameters like I wanted to, but I ended up creating a bunch of queries in Powershell using a for loop and variables within the string to create my large query, then put a UNION ALL after each SELECT FROM OPENQUERY()

SQL-92 (Filemaker): How can I UPDATE a list of sequential numbers?

I need to re-assign all SortID's, starting from 1 until MAX (SortID) from a subset of records of table Beleg, using SQL-92, after one of the SortID's has changed (for example from 444 to 444.1). I have tried several ways (for example SET #a:=0; UPDATE table SET field=#a:=#a+1 WHERE whatever='whatever' ORDER BY field2), but it didn't work, as these solutions all need a special kind of SQL, like SQLServer or Oracle, etc.
The SQL that I use is SQL-92, implemented in FileMaker (INSERT and UPDATE are available, though, but nothing fancy).
Thanks for any hint!
Gary
From what I know, SQL-92 is a standard and not a language. So you can say you are using T-SQL, which is mostly SQL-92 compliant, but you can't say I program SQL Server in SQL-92. The same applies to FileMaker.
I suppose you are trying to update your table through ODBC? The Update statement looks OK, but there are no variables if FileMaker SQL (and I am not sure using a variable inside query will give you result you expect, I think you will set SortId in every row to 1). You are thinking about doing something like Window functions with row() in TSQL, but I do not think this functionality is available.
The easiest solution is to use FileMaker, resetting the numbering for a column is really a trivial task which takes seconds. Do you need help with this?
Edit:
I was referring to TSQL functions rank() and row_number(), there is no row() function in TSQL
I finally got the answer from Ziggy Crueltyfree Zeitgeister on the Database Administrators copy of my question.
He suggested to break this down into multiple steps using a temporary table to store the results:
CREATE TABLE sorting (sid numeric(10,10), rn int);
INSERT INTO sorting (sid, rn)
SELECT SortID, RecordNumber FROM Beleg
WHERE Year ( Valuta ) = 2016
AND Ursprungskonto = 1210
ORDER BY SortID;
UPDATE Beleg SET SortID = (SELECT rn FROM sorting WHERE sid=Beleg.SortID)
WHERE Year ( Valuta ) = 2016
AND Ursprungskonto = 1210;
DROP TABLE sorting;
Of course! I just keep the table definition in Filemaker (let the type coercion be done by Filemaker this way), and filling and deleting from it with my function: RenumberSortID ().

Replacing values with wildcards (parsing text data)

I have rows which contains HTML tags. e.g.
<b>Abc</b> <strong>Bca</strong>
So I need to cut it out. As I suggest I need to find something like '%<%>%' and make a REPLACE to ''.
How can I do it? Interested for both solutions - MS SQL & Oracle also.
Assuming table is called yourtable and field is called htmltag.
In SQL Server:
SELECT
SUBSTRING(substring(htmltag,charindex('>',htmltag)+1,250),0,CHARINDEX('<',
substring(htmltag,charindex('>',htmltag)+1 ,250),0))
FROM yourtable
SQL FIDDLE
In Oracle
SELECT regexp_replace(htmltag, '<[^>]+>', '') htmltag
FROM yourtable
SQL FIDDLE