SQL Select with a function - sql

I have the following SQL Statement :
SELECT
RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),''))) AS ItemNo,
RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),''))) AS Season,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'') AS Variant1,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'') AS Variant2,
(SELECT *
FROM [dbo].[B2BGetSpringFinal] ( LAGKART.VARENUMMER,
LAGKART.VARIANT1,
LAGKART.VARIANT2
)) AS SpringAvailable
FROM
LAGKART
But I get this error :
Msg 170, Level 15, State 1, Line 8
Incorrect syntax near '.'.
But if I call the function with fixed values :
SELECT
RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),''))) AS ItemNo,
RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),''))) AS Season,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'') AS Variant1,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'') AS Variant2,
(SELECT *
FROM [dbo].[B2BGetSpringFinal] ( '6261',
'Black',
'S'
)) AS SpringAvailable
FROM
LAGKART
I get the desired result.
Any ideas?
Br
Mads

In SQL Server 2000, Only constants and #local_variables can be passed to table-valued functions. In SQL 2005 and greater this was fixed. You could try using a scalar function to get the SpringAvailable column value instead, or look at upgrading to a newer SQL Server version.

if your server supports CTEs you could try this one:
WITH a as (
SELECT VARENUMMER,
RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),''))) AS ItemNo,
RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),''))) AS Season,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'') AS Variant1,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'') AS Variant2,
FROM LAGKART
)
select *,
(SELECT * FROM [dbo].[B2BGetSpringFinal] ( a.VARENUMMER,
a.VARIANT1,
a.VARIANT2
)) AS SpringAvailable
from a ;

You can use APPLY (CROSS or OUTER) to pass column(s) value(s) as arguments to a function:
SELECT RTRIM(LTRIM(REPLACE(LAGKART.VARENUMMER,CHAR(2),''))) AS ItemNo,
RTRIM(LTRIM(REPLACE(LAGKART.SXSON,CHAR(2),''))) AS Season,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT1,CHAR(2),''))),'') AS Variant1,
ISNULL(RTRIM(LTRIM(REPLACE(LAGKART.VARIANT2,CHAR(2),''))),'') AS Variant2,
SpringAvailable.*
FROM LAGKART
CROSS APPLY
(
SELECT *
FROM [dbo].[B2BGetSpringFinal] ( LAGKART.VARENUMMER, LAGKART.VARIANT1,LAGKART.VARIANT2 )
) AS SpringAvailable

Related

Pervasive SQL order by with if

In Pervasive SQL 11 I could use a IF statement in the ORDER BY:
SELECT *
FROM (
SELECT
D1001 as 'part_number',
'' as 'required_date',
'' as 'confirmed_date'
FROM PULAGER
WHERE
D1001 LIKE '1121%'
UNION
SELECT
D5410 as 'part_number',
D5511 as 'required_date',
D5513 as 'confirmed_date'
FROM PUIKOKRO
WHERE
D5410 LIKE '1121%'
) as t1
ORDER BY part_number, IF (confirmed_date = '', required_date, confirmed_date)
But after an upgrade version 15.10.031, I get the error "Reference to column name not allowed in ORDER BY with UNION". No error if I remove the IF statement. Any suggestions?
First order by part_number and then order by required_date or confirmed_date depending on the state of confirmed_date.
I solved it by moving the IF statement to the SELECT to create a new column 'sort_date' and wrapping it all with another SELECT. Doesn't feel like the most beautiful solution, but it works.
SELECT * FROM (
SELECT t1.*, IF (confirmed_date = '', required_date, confirmed_date) as 'sort_date' FROM t1
) ORDER BY part_number, sort_date

Oracle SQL use subquery value in another subquery

Before I go any further please mind that I am not well experienced with SQL.
I have one query that is getting a single value (netto value) such as:
WITH cte_value_net AS (
SELECT
nvl(min(value_net),0) as value_net
FROM (
SELECT
i.serial as serial,
nvl(lag(i.value_net) OVER (PARTITION BY i.serial ORDER BY i.month), i.value_net) as value_net
FROM
inventory i
WHERE
i.ctypde IN (
SELECT
ctypde
FROM
appar ap
WHERE
ap.serial = in_serial -- this is the variable I want to set
)
AND
i.month IN (to_char(add_months(sysdate, -1), 'YYYYMM'), to_char(add_months(sysdate, -2), 'YYYYMM'))
AND
i.serial = in_serial -- this is the variable I want to set
) vn
GROUP BY vn.serial
)
In here I have to feed in the variable in_serial that I thought I could get from another subquery such as:
SELECT
(SELECT * FROM cte_value_net) AS value_net
FROM (
SELECT
lap.serial AS in_serial
FROM
applap lap
)
but I can not wrap my head around it why this in_serial is not visible to my custom CTE. Could someone explain me how can I propagate the value from subquery like this?
The error I am obviously getting is:
SQL Error [904] [42000]: ORA-00904: "IN_SERIAL"
Unfortunately I do not have any sample data. What I want to achieve is that I could feed in the returned in_serial from main subquery to my CTE.
Before I can get value_net I need my main query to return the in_serial, otherwise I do not have access to that value.
The trick I use is to produce an extra CTE that I usually call params that includes a single row with all computed parameters. Then, it's a matter of performing a CROSS JOIN with this CTE in any other CTE, subquery or main query, as needed.
For example:
with
params as ( -- 1. Create a CTE that returns a single row
select serial as in_serial from applap
),
cte_value_net AS (
select ...
from inventory i
cross join params -- 2. cross join against the CTE anywhere you need it
where ...
and i.serial = params.in_serial -- 3. Use the parameter
)
select ...
First, your second query is not syntactically correct, as there's a ',' before the FROM. You can write your query like this:
WITH cte_value_net AS (
SELECT
serial, nvl(min(value_net),0) as value_net
FROM (
SELECT
i.serial as serial,
nvl(lag(i.value_net) OVER (PARTITION BY i.serial ORDER BY i.month), i.value_net) as value_net
FROM
inventory i
WHERE
i.ctypde IN (
SELECT
ctypde
FROM
appar ap
WHERE
ap.serial = i.serial
)
AND
i.month IN (to_char(add_months(sysdate, -1), 'YYYYMM'), to_char(add_months(sysdate, -2), 'YYYYMM'))
) vn
GROUP BY vn.serial
)
select ...
from cte_value_net s join applap lap on (lap.serial=s.serial)
(adjust query to your schema ....)

How to resolve datatype length difference errors while using unpivot in SQL Server?

I am running below SQL statements in SQL Server which is causing issues due to difference in length of the column types (name=nvarchar(100), address=nvarchar(250)).
select distinct
Id, Label, [Value]
from
(select distinct
coalesce([Value], 'unknown') as Id,
coalesce([Value], 'unknown') + ':' + I as label,
coalesce([Value], 'unknown') as [Value]
from
[dummyDB].[test].[test]
unpivot
([Value] for I in (name, address)) as dataTable
) as t
Error:
Msg 8167, Level 16, State 1, Line 7
The type of column "address" conflicts with the type of other columns specified in the UNPIVOT list.
How to get this resolved?
If you use APPLY and VALUES to unpivot the data instead, you don't get this error. Using these tools is more versatile that the UNPIVOT operator anyway, so I personally prefer them:
SELECT T.ID,
V.Label,
V.[Value]
FROM dbo.Test T
CROSS APPLY (VALUES('Name',T.Name),
('Address',T.Address))V(Label,Value);
If you have non string-type columns, you'll need to explicitly convert them (possibly with a style code):
SELECT T.ID,
V.Label,
V.[Value]
FROM dbo.Test T
CROSS APPLY (VALUES('Name',T.Name),
('Address',T.Address),
('SomeDate',CONVERT(nvarchar(10),T.SomeDate,112)),
('SomeInt',CONVERT(nvarchar(5),T.SomeInt)))V(Label,Value);

how to find percentage or area of overlapped area of a polygon

In SQL Server 2008 R2 I have created some polygons. I have also selected which polygon is overlapping another one Now I want to know overlapped area or percentage.
For area I am trying following SQL
SELECT location.STArea()
FROM
( SELECT location.STIntersection(
(SELECT LOCATION
FROM TEST.dbo.cn_overlap_trn
WHERE plot_no = '657065700016801' ) )
FROM TEST.dbo.cn_overlap_trn
WHERE plot_no ='657065700016701') ;
in above SQL
SELECT location.STIntersection(
(SELECT LOCATION
FROM TEST.dbo.cn_overlap_trn
WHERE plot_no = '657065700016801' ) )
FROM TEST.dbo.cn_overlap_trn
WHERE plot_no ='657065700016701';
is running fine but when I add STArea() function it results an error
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near ';'.
You need an Alias for your subquery say A and also to your functions specially in your inner query for example:
Select location.STArea() as Area from
(
select
location.STIntersection(
(
select location from TEST.dbo.cn_overlap_trn where plot_no = '657065700016801'
)
) as Intersection
from TEST.dbo.cn_overlap_trn
where plot_no ='657065700016701'
) A ;

SQL Server View Incorrect syntax

i have the following DB:
I want to create view that give this information in a row:
document_id Creator Manager Special Archive
(Creator,Manager,Special,Archive) are AuthorityName values
So fisrt i created a view:
SELECT dbo.DocumentAuthorities.DocumentId, dbo.DocumentAuthorities.AuthorityId, dbo.Authorities.AuthorityName, dbo.Authorities.AuthorityLevel
FROM dbo.DocumentAuthorities CROSS JOIN
dbo.Authorities
then i tried this:
SELECT * FROM
(
SELECT Documents.Id, AuthorityName.AuthorityId, AuthorityName.AuthorityName
FROM AuthorityName CROSS JOIN Documents
) src
PIVOT
(
max(dbo.AuthorityName.AuthorityName)
FOR AuthorityName
IN ([Creator],[Director],[Special],[Archive])
)
but i get this error:
Msg 102, Level 15, State 1, Line 11 Incorrect syntax near ')'.
try this:
You have to give an alias name for your pivot table
SELECT * FROM
(
SELECT Documents.Id, AuthorityName.AuthorityId, AuthorityName.AuthorityName
FROM AuthorityName CROSS JOIN Documents
) src
PIVOT
(
max(dbo.AuthorityName.AuthorityName)
FOR AuthorityName
IN ([Creator],[Director],[Special],[Archive])
) P -- < HERE