I've been trying to select the status of doing a LIKE comparison:
SELECT (`my_column` LIKE '%thepattern'%) AS `did_match`
Thanks!
Edit: Just to clarify this was used so I could get this:
SELECT
(CASE WHEN `title` LIKE '%keyword%' THEN 20 ELSE 0 END) +
(CASE WHEN `desc` LIKE '%keyword%' THEN 10 ELSE 0 END)
AS `match`
FROM myTable
ORDER BY `match`
You need to use:
SELECT t.my_column AS did_match
FROM YOUR_TABLE t
WHERE t.my_column LIKE '%thepattern%'
Replace YOUR_TABLE with the name of your table...
Without a FROM clause, MySQL (and SQL Server) will allow the query, but not against any data.
Not sure I understand the question, and it looks like OMG Ponies has figured it out better than I have, but in case this helps:
SELECT CASE WHEN `my_column` LIKE '%thepattern%'
THEN 'did_match' ELSE 'no_match' END CASE AS MYTEST
FROM ...
Link to CASE statement documentation.
Related
I have a column where I flag two values (0 and 1). How can I select all from the table but only counting the value 1 from the flag column?
SQL Server
You need a COUNT with CASE expression, like this:
COUNT(CASE WHEN flag = 1 then 1 END)
If you want to return all of your values and also have a total count (using sort of logic) you could use a window function aggregation using over like so:
select
t.*
, count(case when flag = 1 then 1 end) over(partition by 1) as flag_1_count
from t
Err, are you asking how to perform a simple select statement or am I missing something...
Like this?
SELECT *
FROM [YourTableName]
WHERE FlagColumnName = 1
This should do it
Select count(0)
from YourTableName
where ValueField = 1
I think, you mean something like this:
SELECT COUNT(*) FROM [YOUR_TABLE] WHERE [FLAGCOLUMN] = 1;
Not quite sure I understand the question, but wouldn't it be something like:
SELECT * FROM your_table WHERE flag = 1
I needed help with a question and what would be the most clean way of doing this in SQL SERVER.
I am basically writing a query that checks if a customer number is inside another subquery then it should return the servicename for that customer number. This is my attempt and it is not working.
Do you guys have any suggestions?
CASE WHEN aa.cust_no in (SELECT Cust_no FROM #Tabl1) THEN (SELECT ServiceName FROM #Tabl1) END AS Target
I get what you're trying to do, but you syntax needs to be changed. You can try a LEFT JOIN.
This query will give you an idea of what your statement should look like
Select tabl1.ServiceName Target
From SomeTable aa
Left Join #Tabl1 tabl1
On aa.cust_no = tabl1.Cust_no
If you want to put something else if a match is not found in #Tabl1, then you will need to use a WHEN, or a COALESCE.
When tabl1.ServiceName Is NOT NULL Then tabl1.ServiceName Else 'Unknown Target' End
OR
Coalesce (tabl1.ServiceName, 'Unknown Target') Target
instead of case expression try select ServiceName in this way:
SELECT
(SELECT TOP 1 ServiceName FROM #Tabl1 where Cust_no = aa.cust_no) AS Target
FROM ...
I think ure missing one parameter look
CASE Expression
WHEN Value1 THEN Result1
WHEN Value2 THEN Result2
ELSE Alternative
END
I hope this could help...
CASE aa.cust_no
WHEN (SELECT Cust_no FROM #Tabl1 WHERE #Tabl1.cust_no) THEN (SELECT ServiceName FROM #Tabl1 WHERE #Tabl1.cust_no)
ELSE 'NO ServiceName'
END AS Target
I believe this should get what you are looking for.
SELECT serviceName
FROM table_A
WHERE cust_no IN (
SELECT cust_no
FROM table_B
);
I have a Select statement like
select AVG(CAST(DATEDIFF(Day,CreatedDate,FirstTouchDate) AS BIGINT) else null end) AS avgdaystofirstattempt,
AVG(CAST(DATEDIFF(Day,CreatedDate,Completeddate) AS BIGINT)) AS AvgDaystoComplete
from Table1
how can i write the condition for column avgdaystofirstattempt - FirstTouchdate not like '%1900%' in the select itself but not in where clause.
thanks in advance
You can selectively include data in an aggregate statement by using CASE. It's not really clear from your question, but I think you want something like this:
SELECT AVG(
CASE
WHEN DATEDIFF(Day,CreatedDate,FirstTouchDate) < 1900 THEN NULL
ELSE CAST(DATEDIFF(Day,CreatedDate,FirstTouchDate) AS BIGINT)
END
) AS avgdaystofirstattempt,
AVG(CAST(DATEDIFF(Day,CreatedDate,Completeddate) AS BIGINT)) AS AvgDaystoComplete
FROM Table1
As stated by the question, I'm trying to formulate a query that has a case statement in the column results, and then I want to include that column in the query's group by statement. To give a concrete example, here is all little of what my query looks like:
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
GROUP BY SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2, CASE_COLUMN
Before coming here, I checked out a few websites, including this one, to try solve my problem. I've tried adding another alias after the CASE keyword like is shown in the linked web page but have had no luck. The error message I continue to receive is the following:
[Error] Script lines: 127-151 ----------------------
CASE_COLUMN IS NOT VALID IN THE CONTEXT WHERE IT IS USED. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.53.71
Has anyone else run into the issues I'm facing and been able to use a GROUP BY on the results of a CASE statement? Any help would be appreciated. Oh, and the DB2 version is a z/OS instance, version 10 (DSN10015)
The alias isn't available to use in the GROUP BY because when GROUP BY happens the alias isn't defined yet:
Here's the order:
1.FROM
2.WHERE
3.GROUP BY
4.HAVING
5.SELECT
6.ORDER BY
You can work around that with:
SELECT column1,column2,case_column
FROM (
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
) a
GROUP BY COLUMN1, COLUMN2, CASE_COLUMN
Or just use the case you use in SELECT in GROUP BY
You can either use the case as is in the group by, like this:
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
GROUP BY SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END
or use a sub-query like this:
select COLUMN1, COLUMN2, CASE_COLUMN
from (
SELECT SOME_TABLE_ALIAS.COLUMN1, OTHER_TABLE_ALIAS.COLUMN2,
CASE
WHEN SOME_TABLE_ALIAS.COLUMN3 IS NOT NULL THEN 'A'
ELSE 'B'
END AS CASE_COLUMN
FROM SOME_TABLE SOME_TABLE_ALIAS
... (other table joins and where clauses)
) a
GROUP BY COLUMN1, COLUMN2, CASE_COLUMN
I checked the Search, but none seem to answer this Question.. I expect it to be farely simple though:
I have a query that results in two columns, but I need it to result in two rows.. Anyone know how?
this is the query:
SELECT (SELECT COUNT(Id) AS Expr1
FROM Table
WHERE (Description LIKE 'door%')) AS Door,
(SELECT COUNT(Id) AS Expr1
FROM Table AS Table_1
WHERE (Description LIKE 'window%')) AS Window
The result I GET is (of course):
[Door] [Window]
56 34
The result I'd LIKE to have is the following:
[OPTION] [NROfRecords]
Door 56
Window 34
Any Ideas? Thanks in advance!
You can use UNPIVOT, I would advise rewriting the query though to below:
select *
from
(
SELECT
sum(case when Description LIKE 'door%' then 1 else 0 end) Door,
sum(case when Description LIKE 'window%' then 1 else 0 end) Window
from Table1
) x
unpivot
(
NrOfRecords
for [Option] in (Door, Window)
) u
See SQL Fiddle with Demo
SELECT 'Door' AS Option, COUNT(id) FROM table WHERE description LIKE 'door%'
UNION ALL
SELECT 'Window' AS Option, COUNT(id) FROM table WHERE description LIKE 'window%'
OR...
WITH
filtered AS
(
SELECT
CASE WHEN description LIKE 'door%' THEN 'Door'
WHEN description LIKE 'window%' THEN 'Window'
ELSE 'Other' END AS option,
*
FROM
yourTable
WHERE
description LIKE 'door%'
OR description LIKE 'window%'
)
SELECT
option,
COUNT(id)
FROM
filtered
GROUP BY
option
Or...
SELECT
lookup.option,
COUNT(id)
FROM
(
SELECT 'door' AS option
UNION ALL
SELECT 'window' AS option
)
AS lookup
INNER JOIN
yourTable
ON yourTable.description LIKE lookup.option + '%'
GROUP BY
lookup.option
select
case when description like 'door%' then 'door'
when description like 'window%' then 'window'
else ''
end as [desc],
count(id)
from table
where description like 'door%' or description like 'window%'
group by
(
case when description like 'door%' then 'door'
when description like 'window%' then 'window' else '' end
)
Solution like this is following DRY principle - you do not repeat Door or Window anywhere.
It also easy to add another entities here, so you do not repeat a logic.
select
C.description,
count(*)
from Table1 as t
inner join (
select 'door%', 'Door' union all
select 'window%', 'Window'
) as C(pattern, description) on t.description like c.pattern
group by C.description
sql fiddle demo