What is the best to way, to get a Union result into different cols
For example
SELECT 1 AS Col1
UNION
SELECT 2 AS Col2
Result:
| Col1 | Col2 |
| 1 | 2 |
The only way I see it, is to create a auxiliary table with two cols and insert each value in the respective col. However, I would like a cleaner and better way.
(There is no unique key between both selects, to make a JOIN)
After a lot of google search, I got an example on merging Header columns and adding rows as per my requirement but it is c1flexgird code I am not able to convert in WPF main form.
https://www.grapecity.com/componentone/docs/wpf/online-flexgrid/MultipleCellRow.html
My data structure also like
col1 | col1 | col2 | col2 | col3 | col3
and I want add rows like below
col1 | Col2 | col3
A | B | A | B | A | B
Please have a look on my issue to fix my problem. Thanks
Is it possible to create a query where you get the results by comparing XML data in one table and the data in another?
Say for example I have an table like so:
StudentRecord Table
StudentID| Student_Name | ScoreData
| |
1 | Mathew | <Root><Math><Score> 10 </Score></Math> </Root>
And in the other table I have a list of passing score
Subject Table
SubjectId | Subject_Name | Passing_Score
| |
1 | Math | 50
In pseudoCode I did something like this
SELECT
Student_Name
FROM StudentRecord sr
WHERE
Data.value('/Root/Math/Score.Value', 'varchar(max)') >= S.Passing_score
The pseudoCode might make it more confusing. But the general idea is that I want to get all the students who scored at least 50 and above, which is based from the subject table.
I'm not sure if this can even be done in the first place.
I was looking for some threads in here that mention optimization in queries, but i couldn't resolve my problem.
I need to perform a query in SQL Server that involve using a select case on my primary select, this is the description of the main table:
WS:
| Oid | model_code | product_code | year |
In my query, I need to select all of this columns plus an extra column that compares to another table if by some criteria the values from my main table exist on my other table, let me explain my other table and then I explain what i mean by this.
TA:
| Oid | model_code | product_code | year |
Both tables have matching columns, so for example, if on my table WS I have this result:
| Oid | model_code | product_code | year |
| 1 | 13 | 123 | 2018 |
And on my TA table I have this:
| Oid | model_code | product_code | year |
| 1 | 25 | 134 | 2016 |
| 2 | 13 | 123 | 2018 |
| 3 | 67 | 582 | 2017 |
I need to print an "Exist" result on that row because the row on my main table match exactly with this 3 column values.
So my query on that row should print something like this:
| model_code | product_code | year | Exist |
| 13 | 123 | 2018 | Yes |
The query I was trying to use to make this happen, was this:
SELECT
WS.Oid, WS.model_code, WS.product_code, Ws.year,
(SELECT
CASE
WHEN EXISTS (SELECT 1 FROM TA
WHERE TA.model_code = Ws.model_code
AND TA.product_code = Ws.product_code
AND TA.[Year] = Ws.[Year])
THEN 'Yes'
ELSE 'No'
END) as 'Exist'
FROM
Ws
And it works, the problem is that on my real tables there are more columns and more rows (about 960,000) and for example, a query around 50,000 elements (using this query) takes more than a minute, and the same query with same elements but without the select case, takes about 2 seconds, so the difference is immense.
I'm sure that a more viable way to achieve this exist, in less time, but I don't know how. any recommendations?
Unless already there, an index on ta (model_code, product_code, year) might help.
CREATE INDEX ta_model_code_product_code_year
ON ta (model_code,
product_code,
year);
Though chances are that the optimizer already rewrites your query in such a way, another thing you could try is to (explicitly) rewrite the query using a left join. I assume oid is NOT NULL in ta.
SELECT ws.oid,
ws.model_code,
ws.product_code,
ws.year,
CASE
WHEN ta.oid IS NULL THEN
'No'
ELSE
'Yes'
END exist
FROM ws
LEFT JOIN ta
ON ta.model_code = ws.model_code
AND ta.product_code = ws.product_code
AND ta.year = ws.year;
With that you want the index from above and maybe try one one ws (model_code, product_code, year) too.
CREATE INDEX ws_model_code_product_code_year
ON ws (model_code,
product_code,
year);
You might also want to play with the order of the columns in the indexes. If for a column more distinct values exist in ta, put it before a column where fewer distinct values exist in ta. But keep the order in both indexes identical, i.e. if you shift a column in the index on ta also move it in the index on ws the same way.
What you want to do is join the two tables together, instead of looking for a matching record for each record. Try something like this:
SELECT
WS.model_code, WS.product_code, Ws.year,
SELECT CASE
WHEN TA.OID IS NOT NULL THEN 'Yes'
ELSE 'No'
END As 'Exist'
FROM WS LEFT OUTER JOIN TA ON
TA.model_code = Ws.model_code
AND TA.product_code = Ws.product_code
AND TA.[Year] = Ws.[Year]
That will print all of the records from the WS table, and if there's a matching record in the TA table, the 'Exist' column will say 'Yes', otherwise it will say 'No'.
This uses one query to do everything. Your original approach would do a completely separate sub-query to check the TA table, and that is creating your performance issue.
You may also want to look at putting indexes on these 3 fields in each table to make the matching go even faster.
I know it is posible to serach multiple columns with one value.
I would like to serach 3-4 columns for 4 maybe 5 values
I want to check if any of my choosen columns have a certain value in them.
Example
Column 1 | Column 2 | Column 3 | Column 4
| | |
Hello | | | = True
| | |
| Goodbye | | = True
| | Hello | Goodbye = True
| | |
| Hello | | = True
| | |
| | Goodbye | = True
In the example I would like SQL to pull the data from all of the lines that have Hello or Goodbye even both in some cases.
Is there a way to do what I want?
There is one more way...
SELECT *
FROM TableName
WHERE 'Value1' IN (Col1,Col2,Col3...) OR 'Val2' in (Col1,Col2,Col3...) OR ...
If it's only 3 or 4 columns, the simplest solution would be something like this:
SELECT *
FROM TableName
WHERE Column1 IN('Hello', 'Goodbye')
OR Column2 IN('Hello', 'Goodbye')
OR Column3 IN('Hello', 'Goodbye')
OR Column4 IN('Hello', 'Goodbye')
Forgot to follow with my solution: I needed to join 2 tables and search across the columns. They ****ed up and made the id of t1 a varchar, smh, and some of them had nulls so we needed to check for them lest our results were ****ed (this is why the selected answer wouldn't work for me). You don't need the aliases but if you were going deeper it helps keep things straight.
Use "+" operator to add columns to a WHERE, check for nulls, and caste if you need too.
SELECT *
FROM Table1 AS t1
LEFT OUTER JOIN Table2 AS t2
ON t1.id = t2.id
WHERE( ISNULL(CONVERT(VARCHAR,t1.id),'') + ISNULL(t1.name,'') + ISNULL(t1.desc,'') + ISNULL(t2.company,'')) LIKE '%xbox%'