Case statement gives error cound not convert 'ABC' to a float8 - sql

I am trying to run below sql statement but getting could not convert to a float. By default return type of case is taking as float. Tried casting it to varchar in Row_num , Name but didn't help. Any help be helpful.
Database = Vertica
SELECT
Name,
ID,
row_num,
(Case when Row_num = 1 Then Name END) +
(Case When Row_num = 2 Then Name END)
FROM table ;

I would try this:
SELECT
Name,
ID,
row_num,
(SELECT Name FROM table WHERE Row_num = 1) +
(SELECT Name FROM table WHERE Row_num = 2) AS ConcatNames
FROM table ;

To concatenate strings in Vertica, use ||:
SELECT Name, ID, row_num,
((Case when Row_num = 1 Then Name END) ||
(Case When Row_num = 2 Then Name END)
)
FROM table ;
I'm not quite sure what this is supposed to do, because one or both case expressions will return NULL.

Related

VBA ACCESS: Select/Filter exact + previous&next rows based on specific value

I'm trying to optimize the filtering of data in one report/table and I've encountered a challenge.
Table is located in m.access, so any vba access code or sql query should work here.
So far I've tried few options, but could not achieve expected results:
select prev_type, type, next_type
from (
select *,
lag(type) over (order by id) as prev_type,
type,
lead(type) over (order by id) as next_type
from table
) as t
where type = "type";
Basically I want to display from below table three rows:
row with Type = 'D'
previous row to the one with Type 'D'
next row to the one with Type 'D'
enter image description here
Try with a subquery:
Select * From YourTable
Where Abs([ID] - (Select ID From YourTable Where [Type] = 'D')) <= 1
For multiple Ds, join the subquery:
Select
*
From
YourTable ,
(Select ID From YourTable Where [Type] = 'D') As T
Where
Abs(YourTable.[ID] - T.ID) <= 1

sql transformation - rows to column

i have been trying to solve this one image
my initial idea is like this
select name,
CASE
when count(name) = 1 then get first distinct value
when count(name) = 2 then get first distinct value
else get first distinct value
END as val1,
CASE
when count(name) = 1 then null
when count(name) = 2 then get second distinct value
else get second distinct value
END as val2,
CASE
when count(name) = 1 then null
when count(name) = 2 then null
else get third distinct value
END as val3
into desired_table
from source_table
group by name
is my attempt feasible? if so, how do i access the first, second and third distinct values?
use pivot . Your output table was incorrect. The correct form is available in db<>fiddle.
select name,x as value1,y as value2,z as value3
from
(
select *
from t1
) as SourceTable
pivot
(
max(value) for value in(x,y,z)
) as PivotTable
demo in db<>fiddle
You can use conditional aggregation along with row_number():
select name,
max(case when seqnum = 1 then value end) as value_1,
max(case when seqnum = 2 then value end) as value_2,
max(case when seqnum = 3 then value end) as value_3
into desired_table
from (select s.*,
row_number() over (partition by name order by value) as seqnum
from source_table s
) s
group by name;

SQL: How do I ignore duplicates in a select statement while preferring one type of entry over another?

Each entry has an ID (random string of numbers and letters), a Name (string), and a type (string "A" or "B").
Some entries share the same ID and Name, but have different types.
I'm trying to write a select statement that ignores entries of type B when there is an entry using the same ID of type A.
As far as I understand, DISTINCT wont work as it relies on the elements matching in all columns, and can not differentiate based on a column.
Here's one way...
with type_a as
(select distinct id, name, type
from table_name
where type = 'A'
),
type_b as
(select distinct id, name, type
from table_name
where type = 'B'
and id not in (select id from type_a)
)
select * from type_a
union
select * from type_b
Use NOT EXISTS:
select t.*
from tablename t
where t.type = 'A'
or not exists (select 1 from tablename where id = t.id and name = t.name and type = 'A')
If the name should not be involved in the condition, then use this:
or not exists (select 1 from tablename where id = t.id and type = 'A')
Or use RANK() window function:
select t.id, t.name, t.type
from (
select t.*
rank() over (partition by id, name order by case when type = 'A' then 1 else 2 end) rnk
from tablename
) t
where t.rnk = 1
Or remove name from partition if it is not relevant.

How to update value if one of the field has multiple same values in SQL

In my database table, one of the fields has the same values. I want to change this value with random number or string which is appended to this field value for uniqueness.
Sample Data
Here 'Ma' has 5 records and so on. I want to change Name Ma01, Ma02 etc.
Id Name Count
1 Ma 5
2 Ga 6
3 Gu 5
How can do with SQL query
Try this
UPDATE TBL
SET Name = A.Name
FROM
(
SELECT
Id,
Name + CAST(ROW_NUMBER() OVER (PARTITION BY Name ORDER BY (SELECT NULL)) AS NVARCHAR(500)) AS NAME,
Count
FROM
TBL
WHERE
NAME IN
(
SELECT T.NAME FROM TBL T
GROUP BY T.NAME
HAVING COUNT(1) > 1
)
) A
WHERE
TBL.Id = A.ID
Try this:
Select Name + convert(varchar(2), row_number()over(partition by Name order by Name))
From tablename
You just try this.
Declare #i int = 1
update yourtable
set ID = #i , #i = #i + 1
Select Code based on NEER`s code with leading zero added, tested on MSSQL database.
SELECT
Id,
Name,
Name + RIGHT('00'+CAST(ROW_NUMBER() OVER (PARTITION BY Name ORDER BY (SELECT Name)) AS NVARCHAR(MAX)),2) AS Updatedname
FROM
tablename
WHERE
Name IN
(
SELECT T.NameFROM tablename T
GROUP BY T.Name
HAVING COUNT(Name) > 1
)
GROUP BY Name,Id
ORDER BY Name

How to get the first not null value from a column of values in Big Query?

I am trying to extract the first not null value from a column of values based on timestamp. Can somebody share your thoughts on this. Thank you.
What have i tried so far?
FIRST_VALUE( column ) OVER ( PARTITION BY id ORDER BY timestamp)
Input :-
id,column,timestamp
1,NULL,10:30 am
1,NULL,10:31 am
1,'xyz',10:32 am
1,'def',10:33 am
2,NULL,11:30 am
2,'abc',11:31 am
Output(expected) :-
1,'xyz',10:30 am
1,'xyz',10:31 am
1,'xyz',10:32 am
1,'xyz',10:33 am
2,'abc',11:30 am
2,'abc',11:31 am
You can modify your sql like this to get the data you want.
FIRST_VALUE( column )
OVER (
PARTITION BY id
ORDER BY
CASE WHEN column IS NULL then 0 ELSE 1 END DESC,
timestamp
)
Try this old trick of string manipulation:
Select
ID,
Column,
ttimestamp,
LTRIM(Right(CColumn,20)) as CColumn,
FROM
(SELECT
ID,
Column,
ttimestamp,
MIN(Concat(RPAD(IF(Column is null, '9999999999999999',STRING(ttimestamp)),20,'0'),LPAD(Column,20,' '))) OVER (Partition by ID) CColumn
FROM (
SELECT
*
FROM (Select 1 as ID, STRING(NULL) as Column, 0.4375 as ttimestamp),
(Select 1 as ID, STRING(NULL) as Column, 0.438194444444444 as ttimestamp),
(Select 1 as ID, 'xyz' as Column, 0.438888888888889 as ttimestamp),
(Select 1 as ID, 'def' as Column, 0.439583333333333 as ttimestamp),
(Select 2 as ID, STRING(NULL) as Column, 0.479166666666667 as ttimestamp),
(Select 2 as ID, 'abc' as Column, 0.479861111111111 as ttimestamp)
))
As far as I know, Big Query has no options like 'IGNORE NULLS' or 'NULLS LAST'. Given that, this is the simplest solution I could come up with. I would like to see even simpler solutions.
Assuming the input data is in table "original_data",
select w2.id, w1.column, w2.timestamp
from
(select id,column,timestamp
from
(select id,column,timestamp, row_number()
over (partition BY id ORDER BY timestamp) position
FROM original_data
where column is not null
)
where position=1
) w1
right outer join
original_data as w2
on w1.id = w2.id
SELECT id,
(SELECT top(1) column FROM test1 where id=1 and column is not null order by autoID desc) as name
,timestamp
FROM yourTable
Output :-
1,'xyz',10:30 am
1,'xyz',10:31 am
1,'xyz',10:32 am
1,'xyz',10:33 am
2,'abc',11:30 am
2,'abc',11:31 am