Retrieving a unique dataset from XML fields in SQL 2008 R2 - sql

I currently have several rows (say around 100 for args sake) in a table, and within that table I have an XML field which contains data as follows (example):
<cf1>summer</cf1>
.. I might then have another row or rows containing:
<cf1>winter</cf1>
.. and for completeness, another few rows perhaps containing multiples of:
<cf1>spring</cf1>
So my question is:
How to write a query/proc to return me a unique resultset of all possible xml nodes in my xml field?
I guess I can return say 100 rows, and then using C# to filter that down.. but I am guessing that with SQL 2008 there are far better ways of doing that!

The idea is that you use XQuery to grab the data in a subquery and just treat it as another row in the table. Like such:
SELECT DISTINCT Season
FROM
(SELECT CAST(Xml_Field.query('data(/cf1)') AS VARCHAR) AS Season
FROM My_Xml_Table)
This query will return:
Season
------
summer
winter
spring

Related

How to run sql query and loop through a column then pass that column data into another query from a second database

I am fairly new to SQL. I am using SQL Server 2014. I want to run a query on a database which returns a column of ID's. I am wondering if it is possible to loop over the column of ID's from the first database and pass them into another database to collect additional info.
Attempted to Google the answer but I'm not able to find a helpful scenario that mimics what I am looking for.
SELECT *
FROM dbo.MYDB1
WHERE CreatedLoc = 123
The above example spits out data but I only care about the ID column
I than want to loop over the ID column and for each run them on another database.
SELECT *
FROM dbo.MYDB2
WHERE ID IN (array of ids here, not hardcoded but dynamic)
Assuming appropriate permissions, you can access a different database than the one you're currently connected to using a fully qualified databasename.schemaname.tablename (or view, etc.)
If your databases are MyDB1 and MyDB2, you can run a query that looks something like this:
SELECT * from MyDB2.dbo.Table2
where ID IN (
SELECT ID from MyDB1.dbo.Table1 where CreatedLoc = 123
)

SQL or statement vs multiple select queries

I'm having a table with an id and a name.
I'm getting a list of id's and i need their names.
In my knowledge i have two options.
Create a forloop in my code which executes:
SELECT name from table where id=x
where x is always a number.
or I'm write a single query like this:
SELECT name from table where id=1 OR id=2 OR id=3
The list of id's and names is enormous so i think you wouldn't want that.
The problem of id's is the id is not always a number but a random generated id containting numbers and characters. So talking about ranges is not a solution.
I'm asking this in a performance point of view.
What's a nice solution for this problem?
SQLite has limits on the size of a query, so if there is no known upper limit on the number of IDs, you cannot use a single query.
When you are reading multiple rows (note: IN (1, 2, 3) is easier than many ORs), you don't know to which ID a name belongs unless you also SELECT that, or sort the results by the ID.
There should be no noticeable difference in performance; SQLite is an embedded database without client/server communication overhead, and the query does not need to be parsed again if you use a prepared statement.
A "nice" solution is using the INoperator:
SELECT name from table where id in (1,2,3)
Also, the IN operator is syntactic sugar built for exactly this purpose..
SELECT name from table where id IN (1,2,3,4,5,6.....)
Hoping that you are getting the list of ID's on which you have to perform a query for names as input temp table #InputIDTable,
SELECT name from table WHERE ID IN (SELECT id from #InputIDTable)

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 - using result of query as input to another query

I got a DB with many tables, and I'm able to make a query that results in each record being the tables' names.
I created an example here:
http://sqlfiddle.com/#!4/fa87b/3
Where what I need is another query that uses the existent one 'iterating' the name in result table.
What I want now is to query all the tables for one specific column.
Like for example:
SELECT column1 FROM (all tables which names are in a query result).

How can I return multiple rows as a single row in T-SQL?

A few months ago our vendor added a capability to our ticketing system which lets us add any number of custom fields to a ticket. I'd like to query these fields out along with the other call information for reporting purposes, but each extensible field is stored as a row in the database. So basically you have something like this:
ext_doc_no call_record value
1 1001 Test
2 1001 test2
3 1001 moretest
What I'd like is to query back:
1001 Test test2 moretest
I've tried to use PIVOT, but that's rather demanding about things like using an aggregate function. Any other ideas on how to do this?
EDIT: I also tried querying each row separately into the main query, and using a function... but both methods are way too slow. I need something to get all the rows at once, PIVOT them and then join into the main query.
Try to look at this answer.
It does exactly what you want to do.
See here Concatenate Values From Multiple Rows Into One Column Ordered SQL 2005+
or for a 2000 version Concatenate Values From Multiple Rows Into One Column
What you want to do is a pivot (some systems call it a crosstab query). That should help you google for additional help, but generally you need to know what columns you expect before writing the query.
return data as XML
SELECT ...
FROM ...
...JOIN....
FOR XML AUTO
http://www.sqlservercentral.com/scripts/Miscellaneous/32004/
Using the script from above page.
DECLARE #Values VARCHAR(1000)
SELECT #Values = COALESCE(#Values + ', ', '') + Value
FROM .....
WHERE ....
SELECT #ValuesEND
EDIT: I don't want to be rude. But you could find this by searching "combining multiple rows into one".