RowNumber Case Statement shows Invalid column - sql

Issue: I have a query in T-SQL which I have ROW_NUMBER as RowNumber set and Partitioned on a column called UnitNumber in my SELECT. I want to create a CASE statement that says:
CASE
WHEN RowNumber = 1 then un.SqFt Else ''
End as GrpSqft
However, I am receiving an error that my RowNumber column is invalid. Am I placing this in the wrong location? I am a rookie to Sql and am having trouble understanding how to define the column as a "built-in" column. I get:
[ Invalid column name 'RowNumber'. ]
Desired Result is to have a column "GrpSqft" with all SqFt values that fall into RowNumber=1
Thank you,

Needs to be
CASE
WHEN ROW_NUMBER() OVER (PARTITION BY UnitNumber ORDER BY BLAH DESC) = 1 THEN un.sqft ELSE ''
END AS GrpSqft

Related

How to use Decode Function in the case where column has string values

I have a view xxabc_v (shown below), I need to update the "Code" column to Null wherever it is N/A when "Value" column sum (900+(-900)=0) becomes zero for the "field_name" values (Demand A+Demand B) for the "Date" 01-Apr-21.
How can I put the decode logic to code column in the above case?
Table structure and expected output:
You don't want decode() because a much simpler method works:
select nullif(code, 'N/A')
This returns NULL when code takes on the specified value.
If you actually want to change the data, then you want update:
update t
set code = NULL
where code = 'N/A';
EDIT:
I see, you have an extra condition. So, use case:
(case when code = 'N/A' and
sum(value) over (partition by id, date) = 0
then NULL
else code
end)
I assumed that you need date wise id wise sum when to sum(). Please check this out:
select date,id,(case when sum(value)over(partition by date,id)=0 and code='N/A' then NULL
else Code end)code, field_name,value
from tablename

Select with IF statement on postgresql

I have a code like that:
select
tbl.person
,COUNT(distinct tbl.project)
,if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
from
my_table tbl
group by
1
And it returns me that error message:
SQL Error [42601]: ERROR: syntax error at or near "then"
I didn't got it. As I saw on documentation, the if statement syntax appears to be used correctly
IF is to be used in procedures, not in queries. Use a case expression instead:
select
tbl.person
,COUNT(distinct tbl.project)
,sum(case when tbl.stage like '%SIGNED%' then tbl.value else 0 end) as test
from
my_table tbl
group by
1
Notes:
tbl.stage is not part of the group by, so it should most probably be enclosed within the aggregate expression, not outside of it
all values returned by a case expression need to have the same datatype. Since sum(tbl.value) is numeric, the else part of the case should return 0 (number), not '0' (string).
In Postgres, I would recommend using filter:
select tbl.person, COUNT(distinct tbl.project)
sum(tbl.value) filter (where tbl.stage like '%SIGNED%') as test
from my_table tbl
group by 1;
if is control flow logic. When working with queries, you want to learn how to think more as sets. So the idea is to filter the rows and add up the values after filtering.
replace
if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
with
sum(case when tbl.stage like '%SIGNED%' then tbl.value end) as test

SQL query to sort data while insert , first numbers then alphabets an last symbols

I am getting trouble to write SQL sort Query,
I have table as follows
And I want to sort above data as, First should number and then alphabets and last special symbols like following table.
First numbers should sort like 1,2,3,11,22,33
then Alphabets should sort like a ,b,c,..z
and then symbols,
like following table
I have tried many ways but still not getting correct way, please help me to write query.
You can use a CASE WHEN on the ORDER BY to create some groups:
SELECT *
FROM table_name
ORDER BY
CASE WHEN LEFT(FilterParameterValue, 1) LIKE '[0-9]' OR LEFT(FilterParameterValue, 2) LIKE '-[0-9]' OR LEFT(FilterParameterValue, 2) LIKE '+[0-9]' THEN 1 ELSE 0 END DESC, -- group for numbers
CASE WHEN ISNUMERIC(FilterParameterValue) = 1 THEN CAST(FilterParameterValue AS MONEY) ELSE NULL END ASC, -- order the numeric values
CASE WHEN LEFT(FilterParameterValue, 1) LIKE '[A-Za-z]' THEN 1 ELSE 0 END DESC, -- group for chars from A to Z (capital / non capital)
colName
demo on dbfiddle.uk
Try using regex in order by clause.. For ex
ORDER BY IF(FilterParameterValue RLIKE '^[a-z]', 1, 2), FilterParameterValue
You can try to cast your Column to Numeric Type then ordered
SELECT * FROM table_name ORDER BY TRY_CAST(Column_Name as Type)

ORA-00936 Missing expression on ROW NUMBER

I've checked linked questions and still haven't found an answer.
SELECT *
FROM
( SELECT contract_number,
ROW NUMBER() OVER (PARTITION BY contract_number
ORDER BY ID) RowNumber
FROM contracts ) a
WHERE a.RowNumber = 1
This code throws ORA-00936 error. It underlines ROW NUMBER(), what's missing here?
Have you got the syntax right for row number? It should be ROW_NUMBER()
in the 4th line ROW NUMBER() i used and in the last line you are using WHERE a.RowNumber = 1
the function name differ from each other
this is the only error

Using Top 1 in SQL instead of Looping

I have the following vba code that I am converting to SQL...
Do While Not sRst1.EOF
sRst1.MoveLast
sRst1.MoveFirst
If (sRst1!Field1 = "FileName") Then
Exit Do
End If
sCtr = sCtr + 1
sRst1.MoveNext
Loop
So basically I need to grab the first record where field1 is equal to FileName, which I could probably do by selecting TOP(1) where field1 = "FileName" if I'm not mistaken? Also, I need to return a count of how many records it took to get to that record. How could I do that?
WITH t AS (SELECT *,ROW_NUMBER() OVER(ORDER BY (SELECT 0)) row_num FROM sRst1)
SELECT field1,row_num FROM t WHERE field1 = 'FileName'
As FrankPl points out, row_num will be effectively random if you don't tell SQL what order you want. Replace "(SELECT 0)" with "field1" or some other column to fix this.