sql server pivot syntax - sql

I need to do row column transpose and tried a following query
select txn_date,
case when remarks is NULL then 'bank' else remarks end as remarks
from nibl
PIVOT
(
count(txn_date)
FOR remarks IN (bank, remit)
) as pivot
the query above is giving syntax error as below
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'pivot'.

pivot is a reserved word in SQL Server so it is ) as pivot that fails here.
Use another alias ) as p.

did u try it in this way...
select * from
(select txn_date,
case when remarks is NULL then 'bank' else remarks end as remarks
from nibl
) srs
PIVOT
(
count(txn_date)
FOR remarks IN (bank, remit)
) as pivot

Related

Incorrect syntax near ')' Error in the CTE

I wrote the following CTE (Common Table Expression)
WITH PRODUCTION_CTE(ShortProdNo,BoatRefNumber,ProdNo, CustomerPoNumber,LoadDate, Trailer, VeadaBuilding)
AS
(
SELECT
FBS.BoatNumber AS ShortProdNo,
UOD.BoatRefNumber AS BoatRefNumber,
FBS.ProdNo AS ProdNo,
UOD.CustomerPoNumber AS CustomerPoNumber,
FBS.Shipped AS LoadDate,
FBS.TruckNum AS Trailer,
(CASE
WHEN Rtrim(UOD.CustomerPoNumber)='VEADA-VS1' THEN 'Bldg10'
ELSE 'Bldg4'
END) AS VeadaBuilding
FROM SysproCompanyV.dbo.FlatBenningtonShipping as FBS
INNER JOIN SysproCompanyV.dbo.UsrOrderDetails as UOD
ON FBS.BoatNumber=UOD.BoatRefNumber)
I am getting the following Error from the above CTE:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near ')'.
I am not sure why it is happening as Inner Joins are allowed in the CTE, all the parenthesis are closed and the names are correctly declared.
You need to call the CTE (i.e. PRODUCTION_CTE) IMMEDIATE after declaration :
;with PRODUCTION_CTE as (
. . .
)
select pc.*
from PRODUCTION_CTE pc

Error in SQL Server query - inner join

I am trying to add Percentile value for each record as new column. But i am getting error in my SQL query. Can anyone please help to solve it.
Error message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'inner'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b where [Country] = 'AUSTRALIA'
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
You have your where statement in the wrong place. Joins are formally part of the from statement and thus come before criteria. To have your criterion at the bottom check the correct table you use the alias.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
where b.[Country] = 'AUSTRALIA'

Why an I getting Invalid Column Name in Pivot Table

I am making a Pivot Table in SQL. I am trying to get IsNull(ItemQty, 0) to work. The way I have the code below won't work as it throws the following error. If I change the top Select statement to just Select * from () it'll work. But, that doesn't allow me to do the IsNull then.
The ShipWeekMod and ItemQty are results of CASE statements in the vtc_ItemForecastSummary view. Any thoughts?
Msg 207, Level 16, State 1, Line 16 Invalid column name 'ShipWeekMod'.
Msg 207, Level 16, State 1, Line 16 Invalid column name 'ItemQty'.
select
OfferTypeShipYear, EDPNO, OfferType, ShipYear, ShipWeekMod, IsNull(ItemQty, 0) as ItemQty
from
(
select OfferTypeShipYear, EDPNO, OfferType, ShipYear, ShipWeekMod, ItemQty as ItemQty from vtc_ItemForecastSummary
) DataTable
PIVOT
(
SUM(ItemQty)
FOR ShipWeekMod
IN (
[2],[4],[6],[8],[10],[12],[14],[16],[18],[20],[22],[24], [26],[28],[30],[32],[34],[36],[38],[40],[42],[44],[46],[48],[50],[52]
)
) PivotTable
You can't reuse the column that you pivot on in the select. Instead you have to apply the isnull function to each column returned by the pivot. As this can be tedious to do you might consider rewriting the query to use dynamic sql instead.
It should probably look like this:
select
OfferTypeShipYear, EDPNO, OfferType, ShipYear,
isnull([2],0) as [2],
isnull([4],0) as [4],
isnull([6],0) as [6],
isnull([8],0) as [8]
... etcetera
from (
select
OfferTypeShipYear, EDPNO, OfferType,
ShipYear, ShipWeekMod, ItemQty
from vtc_ItemForecastSummary
) DataTable
PIVOT (
SUM(ItemQty)
FOR ShipWeekMod
IN (
[2],[4],[6],[8],[10],[12],[14],[16],[18],[20],[22],[24],
[26],[28],[30],[32],[34],[36],[38],[40],[42],[44],[46],[48],[50],[52]
)
) PivotTable

SQL Select with a function

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

Help with SQL CASE Statement in SELECT Query

Here is my SQL statement:
SELECT [Item], SUM([Quantity]) AS SumOfQuantity, SUM([Price Each]) AS SumOfTotal,
([SumOfTotal] / [SumOfQuantity]) As Average,
CASE
WHEN [Average] <= 6 THEN SumOfTotal
ELSE 6*[SumOfQuantity] END AS GrossComm
FROM Data
GROUP BY [Item];
When I try to execute this query, I get the error message:
Syntax error (missing operator) in query expression 'CASE WHEN [Average] <= 6 THEN SumOfTotal ELSE 6*[SumOfQuantity] END AS GrossComm
Any ideas?
Thanks!
You can't reference a derived field by name in the CASE statement. Instead of [average] use the formulas.
You actually may have a larger issue since all your fields are based on other derived fields, so you might have to write out the formula for each one multiple times.
It's likely there is also an issue with your other fields that the CASE is obscuring. Basically don't refer to a calculated/aliased field in the same query by name, since it won't work.
If this is SQL Server you COULD do a workaround using a CTE:
;WITH CTE AS
(
SELECT [Item], SUM([Quantity]) AS SumOfQuantity, SUM([Price Each]) AS SumOfTotal,
FROM Data
GROUP BY [Item]
)
SELECT *, ([SumOfTotal] / [SumOfQuantity]) As Average,
CASE
WHEN ([SumOfTotal] / [SumOfQuantity]) <= 6 THEN SumOfTotal
ELSE 6*[SumOfQuantity] END AS GrossComm
FROM CTE