MSSQL Concatenate three columns and then use WHERE - sql-server

I am using MSSQL server. What I am trying to do is concatenate 3 columns from the same table into one result so I can then use the WHERE clause on the results.
This is the command I would like to run:
select col1 + col2 + col3 as result from table where result like '%term%'
However I keep getting hit with Invalid column name 'result'
I have seen lots of answers about concatenating columns, but none seem to use the WHERE clause on them.
Thanks

Try This
SELECT col1 + col2 + col3 AS result
FROM TABLE
WHERE col1 LIKE '%term%'
OR col2 LIKE '%term%'
OR col3 LIKE '%term%'
OR
SELECT *
FROM (
SELECT col1 + col2 + col3 AS result
FROM TABLE
) t
WHERE result LIKE '%term%'

DECLARE #Search VARCHAR(10)='term'
SELECT Concat(col1, col2, col3) AS result
FROM table
WHERE Charindex(#Search, Concat(col1, col2, col3)) > 0

Use inner table to return result and search on returned column.
SELECT * FROM
(select col1 + col2 + col3 as result from [table] ) a
where result like '%term%'

Related

Who can write a more efficient query?

I have this query, which works, but is there a more efficient way to write it?
SELECT *
FROM table (NOLOCK)
WHERE ((COL1 = 'aaa' AND COL2 = 'ee')
OR (COL1 = 'bbb' AND COL2 = 'ff')
OR (COL1 = 'ccc' AND COL2 = 'gg'))
First you will need a composite index with both COL1 and COL2 on the same index.
CREATE INDEX IX_TABLE_COL1_COL2
ON table (COL1, COL2);
Then I would use a union of simpler queries, that the engine won't have problems optimizing individually through the previous index. Expressions with OR seems to be harder for the engine to locate an optimal execution plan.
SELECT *
FROM table
WHERE COL1 = 'aaa' AND COL2 = 'ee'
UNION ALL
SELECT *
FROM table
WHERE COL1 = 'bbb' AND COL2 = 'ff'
UNION ALL
SELECT *
FROM table
WHERE COL1 = 'ccc' AND COL2 = 'gg'

Error while creating pivot on SQL Server

I am facing error in last like while creating a pivot table on SQL Server.
Following is the code:
SELECT
COL1, 'X'
FROM
(SELECT COL1, COL2
FROM TABLE_X
WHERE COL3 = 'B' AND COL4 = 'Activation') AS SourceTable
PIVOT
(COUNT(COL1)
FOR COL2 IN ('X')
) AS PivotTable
Error:
Incorrect syntax near 'X'.
Thanks in advance.
Column COL1 will not exist in the Pivot result since it is the Aggregated column.
you can change this example to just
SELECT
*
FROM
(SELECT COL1, COL2
FROM TABLE_X
WHERE COL3 = 'B' AND COL4 = 'Activation') AS SourceTable
PIVOT
(COUNT(COL1)
FOR COL2 IN ([X]) -- put the values in square brackets instead of single quote
) AS PivotTable
and you should only get a single column back named X

Identifying which value in where clause is incorrect?

My SQL kung-fu is not strong. I want to know if I can identify which value of a WHERE clause is incorrect.
So say I execute the following SQL statement....
SELECT col1, col2, col3 FROM dbo.myTable WHERE col1 = 'ABC' and col2 = 123
I want to be able to identify either that the value 'ABC' for col1 doesn't exist OR that the value 123 for col2 does not exist.
This would be within a single query, as I want to avoid two queries if possible.
Where I execute the first, and if rows exist then I execute the second if no rows then I can derive that col2 is wrong.
SELECT col1, col2, col3 FROM dbo.myTable WHERE col1 = 'ABC'
SELECT col1, col2, col3 FROM dbo.myTable WHERE col1 = 'ABC' and col2 = 123
I have considered
SELECT 1 as qRow, col1, col2, col3 FROM dbo.myTable WHERE col1 = 'ABC'
UNION
SELECT 2 as qRow, col1, col2, col3 FROM dbo.myTable WHERE col1 = 'ABC' and col2 = 123
and based on the qRow value i can determine which value is missing within the table, but wanted to now if there was any other solution
If you want to find where col1 is not ABC or col2 is not 123, you can do this:
SELECT col1 ,
col2 ,
col3
FROM dbo.myTable
WHERE col1 <> 'ABC'
OR col2 <> 123
Are you looking for something like
SELECT col1, col2, col3,
CASE
WHEN col1 = 'ABC' and col2 = 123 THEN 'both_true'
WHEN col1='ABC' THEN 'col1_true'
ELSE 'col2_true'
END AS value_check
FROM dbo.myTable WHERE col1 = 'ABC' or col2 = 123
I think probably you can use NOT EXISTS like
SELECT col1, col2, col3
FROM dbo.myTable
WHERE NOT EXISTS ( select 1 from dbo.myTable
where col1 = 'ABC' or col2 = 123)
SELECT col1, col2, col3, case when col1 <> 'ABC' then 'X' end as 'MissingCol1Abc',
case when col2 <> 123 then 'X' end as MissingCol2
FROM dbo.myTable
WHERE col1 = 'ABC' OR col2 = 123
Or did you mean you want to know when the column is null?
SELECT col1, col2, col3, case when is null col1 then 'X' end as 'MissingCol1Abc',
case when col2 is null then 'X' end as MissingCol2
FROM dbo.myTable
WHERE col1 = 'ABC' OR col2 = 123
If you use an OR instead of AND you could add a CASE statement to show the matching column:
SELECT col1, col2, col3,
CASE WHEN col1 = 'ABC' THEN 'True' ELSE 'False' END as Col1Match,
CASE WHEN col2 = '123' THEN 'True' ELSE 'False' END as Col2Match
FROM dbo.myTable
WHERE col1 = 'ABC' OR col2 = 123
Sample SQL Fiddle
You will see in the sample fiddle, that rows are only returned where one or both items match.

Removing duplicate combinations from result set in SQL Server

I have a table with two columns with data like this:
1,2
1,3
1,4
2,1
2,2
3,1
I want to select just unique combinations, so out of those I would end up with:
1,2
1,3
1,4
2,2
because 1,2 is the same combination as 2,1 etc
How would I go about that in a SQL statement?
In reality, my table has a third column and I want to add a where clause based on that third column so that only those rows are considered
SELECT * FROM (
SELECT
CASE WHEN Col1 <= Col2 THEN Col1 ELSE Col2 END AS Col1,
CASE WHEN Col1 <= Col2 THEN Col2 ELSE Col1 END AS Col2
FROM
MyTable
) Ordered
GROUP BY
Col1, Col2
You could do it without the subquery by GROUPing on the CASE expressions, but it's longer to read.
Another way to achieve the same thing:
SELECT a, b
FROM tableX
WHERE a <= b
AND (other conditions)
UNION
SELECT b, a
FROM tableX
WHERE a > b
AND (other conditions) ;
This variation may be different (regarding efficiency), depending on the indexes you have:
SELECT *
FROM
( SELECT a, b
FROM tableX
WHERE (other conditions)
UNION
SELECT b, a
FROM tableX
WHERE (other conditions)
) AS tmp
WHERE a <= b ;
You can try something like:
select distinct col1, col2 from table
where col2 + '-' + col1 not in (select col1 + '-' + col2 from your_table)
Notice that you have to concatenate the fields and it depends of the column type (col1 + '-' + col2 works well with char and varchar types)
How about:
SELECT
COL1, COL2, COUNT(*)
FROM
Your_Table
GROUP BY
COL1, COL2

How to filter rows by values of one column?

I need to get several columns form sql query. Then I have to filter this answer by the "distinct" values of one column, but in the output I need to have all columns, not only this which values has to be distinct. Can anybody help me? Order by clause is not an answer for me.
A,B,C,D
E,F,G,H
I,J,C,L
M,N,Z,H
Above is a simple rows output. Please have a look onto 3rd column. Let's assume that we don't know how many rows do we have. I need to select only rows which has distinct value in 3rd column. (C,G,Z) - We need to filter anyone from "C" rows.
I've arbitrarily chosen to use col1 to break ties on col3. You can adjust the order by portion of the partition to suit your needs.
/* Set up test data */
declare #test table (
col1 char(1),
col2 char(1),
col3 char(1),
col4 char(1)
)
insert into #test
(col1, col2, col3, col4)
select 'A','B','C','D' union all
select 'E','F','G','H' union all
select 'I','J','C','L' union all
select 'M','N','Z','H'
/* Here's the query */
;with cteRowNumber as (
select col1, col2, col3, col4,
row_number() over (partition by col3 order by col1) as RowNumber
from #test
)
select col1, col2, col3, col4
from cteRowNumber
where RowNumber = 1
Returns
col1 col2 col3 col4
----------------------------
A B C D
E F G H
M N Z H
ROLL UP or CUBE could be helpful for your problem, since they can aggregate (i.e. subtotal) data based on the GROUP BY and still return the individual rows.

Resources