I am trying to run a union all query in hive
select * from tabName where col1='val1' and col2 = 'val2' limit 10 union all select * from tabName where col1='val1' and col2 = 'val3' limit 10;
but i get
FAILED: ParseException line 1:105 missing EOF at 'union' near '10'
I also tried
( select * from tabName where col1='val1' and col2 = 'val2' limit 10 ) as a union all ( select * from tabName where col1='val1' and col2 = 'val3' limit 10 ) as b;
but i got
FAILED: ParseException line 1:109 missing EOF at 'as' near ')'
what am i doing wrong ?
Use select from subquery:
select * from
( select * from tabName where col1='val1' and col2 = 'val2' limit 10 ) a
union all
select * from
( select * from tabName where col1='val1' and col2 = 'val3' limit 10 ) b;
I offer another way using with clause:
with query1 as (
select *
from tabName
where col1 = 'val1' and col2 = 'val2'
limit 10
),
query2 as (
select *
from tabName
where col1 = 'val1' and col3 = 'val3'
limit 10
)
select * from query1
union all
select * from query2
Related
I have a col1 in tbl1 which has data as under:
C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\
now I want to insert a '\' after C:\ABC\1245_PQR\125\xyz\ROW and before '20MAOSAD12\'
and the data format is same but it changes in all row, for example -
C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\
C:\ABC\3456_ADR\515\xpo\ROWadMAOSAD23\
C:\ABC\1547_DFR\255\RDS\ROW14SDFS15\
Can someone please help.
Thanks
You can use replace function
SELECT REPLACE('C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\', '\ROW', '\ROW\')
To be more precise
SELECT REPLACE(col1, '\ROW', '\ROW\') from tbl1
Alternatively you could consider the STUFF operator:
STUFF ( character_expression , start , length , replaceWith_expression )
SELECT STUFF('C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\',28,0, '\')
SELECT STUFF('SomeColumn',28,0, '\') FROM SomeTable WHERE SomeColumn=SomeValue
Also, I made this one - a bit lengthy
-- Step 1
Select all rows that have ...xyz.... in col1 and col2 is xyz
select * from tbl where col2 = 'row' and col1 like '%xyz%'
-- Step 2
-- From the rows above, calculate the start index of ...xyz....... in col1
select charindex('xyz', col1) from (select col1 from tbl where col2 = 'xyz' and col1 like '%xyz%') tmp
-- Step 3
-- Split the col1 from CharIndex + 3 (size of xyz)
select left(col1, charindex('xyz', col1) + 2 ), substring(col1, charindex('xyz', col1) + 3, LEN(col1) ), ( left(col1, charindex('xyz', col1) + 2 ) + '\' + substring(col1, charindex('xyz', col1) + 3, LEN(col1) ))
from (select col1 from tbl where name = 'xyz' and col1 like '%xyz%') tmp
-- Step 4
-- Update !!
update tbl
SET col1 = ( left(col1, charindex('xyz', col1) + 2 ) + '\' + substring(col1, charindex('xyz', col1) + 3, LEN(col1) ))
where col2 = 'xyz' and col1 like '%xyz%' and col1 not like '%xyz\%'
Select * from TABLENAME WHERE "CLAUSE"
It will print the result in a single row.
Col 1 Col 2 ...... Col N
Val 1 Val 2 ...... Val N
I need
Col 1 Val 1
Col 2 Val 2
.
.
.
Col N Val N
A little time consuming to do on a regular basis, but:
select COL_NAME, COL_DATA
from (SELECT * FROM table_name
WHERE clause)
unpivot ( COL_NAME FOR COL_DATA IN ( COL1 as 'COL1'
,COL2 as 'COL2'
,COL3 as 'COL3'
,COL4 AS 'COL4')
)
Bear in mind that you also need to cast all of the values to the same data-type as Oracle won't mix datatypes in the same column, so if COL1-3 are number, but COL4 is varchar, then you would
select COL_NAME, COL_DATA
from (SELECT * FROM table_name
WHERE clause)
unpivot ( COL_NAME FOR COL_DATA IN ( TO_CHAR(COL1) as 'COL1'
,TO_CHAR(COL2) as 'COL2'
,TO_CHAR(COL3) as 'COL3'
,COL4 AS 'COL4')
)
select 'col 1', col1 from TABLENAME WHERE "CLAUSE"
UNION ALL
select 'col 2', col2 from TABLENAME WHERE "CLAUSE"
UNION ALL
...
select 'col n', coln from TABLENAME WHERE "CLAUSE"
order by 1
I need to do a SELECT like :
Select
col1, col2, (expression) as colA
from tablex ,
but the expression depends on an external variable #per so, the select would be something like :
SELECT
col1, col2,
case
#per = 1 then (col00 + col01) as colA
#per = 2 then (col00 + col01 + col02) as colA
#per = 3 then (col00 + col01 + col02 + col03) as colA
end
FROM tableX
How do I do this?
Thanks
This is the code assuming you are using T-SQL:
SELECT col1,col2,
case
WHEN #per =1 then (col00+col01)
WHEN #per =2 then (col00+col01+col02)
WHEN #per =3 then (col00+col01+col02+col03)
end as colA
FROM tableX
SELECT col1,col2, colA =
CASE #per
WHEN 1 THEN (col00+col01)
WHEN 2 THEN (col00+col01+col02)
WHEN 3 THEN (col00+col01+col02+col03)
ELSE 0
END
FROM tableX
New to SQL and I am struggling with a query to have multiple values from a column counted and added together. Here is the query
select count (*) as NEWCOL
from table1
where COL1 = 'val1' and COL2 = 'val1' and COL3='val1' or 'val2'
your where condition looks flawed:
select count (*) as NEWCOL
from table1
where COL1 = 'val1'
and COL2 = 'val1'
and (COL3 = 'val1' or COL3 = 'val2')
;
Try this:
SELECT
COUNT(y.*) as NewCol
,x.[allfields] as WhatyouWantitToBe
FROM
table1 y
OUTER APPLY
(SELECT
x.col1 + ' ' + x.col2 + ' ' + x.col3 AS [allFields]
FROM
table1 x
WHERE
x.col1 = y.col1
AND
x.col2 = y.col2
AND
x.col3 = y.col3) AS x
WHERE
y.COL1 = 'val1'
AND y.COL2 = 'val1'
AND (y.COL3 = 'val1'
OR
y.COL3 = 'val2')
And then again, that's what can be done with what you mentioned. A concatenation of varchar field as well as a count.
I have a form that is giving me information like INPUT1, INPUT2. I need to select from COL1 and COL2 where COL1 = INPUT1, COL2 = INPUT2 or the other way around, COL1 = INPUT2, COL2 = INPUT1.
SELECT * FROM table WHERE
(COL1 = INPUT1 AND COL2 = INPUT2)
OR (COL1 = INPUT2 AND COL2 = INPUT1);
SELECT * FROM table
WHERE ( COL1=INPUT1 AND COL2=INPUT2 )
OR ( COL1=INPUT2 AND COL2=INPUT1 )
select * from table_name
where ( COL1=INPUT1 AND COL2=INPUT2 ) OR ( COL1=INPUT2 AND COL2=INPUT1 ) ;