I have a hive table 'Order_Header_frm_spark_6'. I would like to select only those records that are in mm/dd/yyyy format and print those records as 'rejected', else 'not_rejected'. But when I use the query given below, I see all the records as not_rejected. Input file. Output of hive query is given below.
Query :
[select * ,case when Order_Date = '%/%' then 'Reject' else 'not_rejected' END from Order_Header_frm_spark_6;]
Hive Output
Try with like operator
select * ,case when Order_Date like '%/%' then 'Reject' else 'not_rejected' END from Order_Header_frm_spark_6;
Thanks
select * ,case when order like '//____' then 'Reject' else 'not_rejected' END from Order_Header_frm_spark_6;
See comment for correct query.
For Hive
Related
How to change column value by CASE command depending on condition without giving adding a new column to table?
The only way I know is by adding new column:
SELECT
t1.*
,CASE
what='costs' THEN amount*(-1)
ELSE
sales
END AS NewAmount
FROM t1
Is there a way to get the results as on the picture below? Note that sometimes the condition is specified by values in more than one column (what=costs AND country=Atlantida)
Select just the columns that you want:
SELECT t1.what,
(CASE WHEN what = 'costs' THEN amount*(-1)
ELSE sales
END) AS Amount
FROM t1
Yes, there is way,
Instead of select *, use required column names only.
SELECT
t1.what
,CASE
WHEN what='costs' THEN amount*(-1)
ELSE
sales
END AS Amount
FROM t1
Don't you want amount when not cost?
SELECT
t1.*
,CASE when what='costs' THEN amount*(-1)
ELSE
amount
END AS NewAmount
FROM t1
I have following query to select the view to be used in the query, however I get the error:
FROM keyword not found where expected
select *, (CASE WHEN 'employee' = 'employee' THEN employee ELSE vw END) FROM type1
select type1.*,
(CASE WHEN 'employee' = 'employee'
THEN employee
ELSE vw
END)
FROM type1
I always prefix with the tablename/table alias to the * and it works!!
We just need to specify, fetch all the column from this table, when we specify combination of wildcard and other select expressions!
You cannot use * and individual columns together in select statement.
SELECT (CASE WHEN 'employee' = 'employee' THEN 'employee' ELSE 'vw' END)
FROM dual
You cant do a *, in Oracle without an alias/TableName(.) in front of it. List out the columns you want to see in the result set or add a table alias/tablename.
You cannot set the name of the view the select statement shall retrieve data from in the column list part of the statement.
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
I have a table with several rows, and several columns. It looks like this:
Name Description
X PASS
X PASS
X FAIL
I want it to return only one row. If all of them are PASS, return PASS.
If one or more of them are FAIL, then return FAIL.
What's the best way to go about achieving this in SQL Server 2008?
EDIT: The values in the name column will always be the same.
Depending on the database indexes, and assuming you want one row returned per unique name, I would look at the performance of
select
name,
min([description]) as description
from
tableA
group by
name
compared to the other solutions
SELECT TOP 1 CASE Description WHEN 'FAIL' THEN 'FAIL' ELSE 'PASS' END
FROM DaTable
ORDER BY Description
OP: Is it possible that the table is empty? In that case this query won't return any rows, obviously.
EDIT
According to aquinas's comment I created a modified query without ordering:
SELECT CASE COUNT(Description) WHEN 0 THEN 'FAIL' ELSE 'PASS' END
FROM DaTable
WHERE Description = 'FAIL'
This query will return PASS if DaTable is empty.
This is the simplest solution you will find:
SELECT MIN(Description) FROM tbl
If there's at least one FAIL, then our result column will contain FAIL, otherwise, it will contain PASS.
You can use EXISTS to get the existance of a row containing "FAIL".
You could also try something like:
SELECT TOP 1 COALESCE(tFail.Description,t.Description)
FROM myTable AS t
LEFT JOIN myTable AS tFail ON tFail.Name = t.Name AND tFail.Description = 'FAIL'
WHERE t.Name = 'x'
Here is the query:
--DROP TABLE result
CREATE TABLE result(Name varchar(10),Description varchar(20))
--select * from result
INSERT INTO result
VALUES('X','PASS'),('X','PASS'),('X','FAIL')
;WITH CTE(descp,cnt) as (SELECT [description],COUNT(*) as cnt FROM result group by [description])
SELECT CASE WHEN COUNT(*) > 1 then 'FAIL' when COUNT(*)=1 then MAX(descp) else 'PASS' END FROM CTE
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.