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
Related
I want to
Filter a data set
Take distinct of a column
take out top 10 rows of that data set with the distinct column
The code I am using is
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
)
;
and the error I am getting is
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ';'.
Where am I going wrong?
You have to give a name to the subquery:
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
) AS my_subquery
ORDER BY businessdata
;
make sure you also an ORDER BY to correctly set the order and thus make TOP meaningful
How do you go about referring to a pivoted column name so that you can include ISNULL around it? In my example below I get the following error:
Msg 207, Level 16, State 1, Line 3
Invalid column name '1234'.
The query:
SELECT *
FROM
(SELECT
ISNULL([1234], 0) as test1,
*
FROM
[Report]('2019-01-01', '2020-01-01')) a
PIVOT
(SUM(amount)
FOR account IN ([1234])
) p
In your sub-query a ISNULL([1234], 0) as test1, you have alias the name to test1
So in the pivot query, you should use the alias name test1 and not [1234]
SELECT *
FROM (
SELECT
ISNULL([1234], 0) as test1,
*
FROM [Report]('2019-01-01', '2020-01-01')
) a
PIVOT (
SUM(amount)
FOR account IN ([test1]) -- change to `test1` here
) p
I don't know why the following query is not working:
SELECT whs_code, pdt_code,case_dt_yyyymmdd, fresh_frozen_status,
SUM(qty_cases_on_hand)-Qty, SUM(qty_weight_on_hand)-Wt, operation
FROM
(
SELECT whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation,SUM(qty_cases_on_hand) AS Qty, SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
)
WHERE operation='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation`
The error is :
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'WHERE'.
To make it simple to understand what i am trying to do here, please see the example
I need data as result of
(SELECT x FROM tbl_table Where column y='SU')-(SELECT x FROM tbl_table Where column y='RU')
SELECT ru.whs_code,
ru.pdt_code,
ru.case_dt_yyyymmdd,
ru.fresh_frozen_status,
ru.operation,
ru.Qty - su.Qty AS Qty_Diff,
ru.Wt - su.Wt AS Wt_Diff
FROM
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) ru,
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) su
WHERE ru.whs_code = su.whs_code
AND ru.pdt_code = su.pdt_code
AND ru.case_dt_yyyymmdd = su.case_dt_yyyymmdd
AND ru.fresh_frozen_status = su.fresh_frozen_status
AND ru.operation = su.operation;
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
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 ;