SELECT
LineId, [LineNumber],
ROUND(SUM(Quantity), 3) AS TotalNeed,
SPMS2.dbo.ReturnTotalMRCByLineId(LineId) AS TotalMRC,
SPMS2.dbo.ReturnTotalMIVByLineId(LineId) AS TotalMIV,
SPMS2.dbo.ReturnTotalIMIVByLineId(LineId) AS TotalIMIV,
(TotalMRC-TotalMIV ) AS Shortage
FROM
[SPMS2].[dbo].[ViewMTO]
GROUP BY
lineid, [LineNumber]
but I get these errors:
Msg 207, Level 16, State 1, Procedure LineMaterialStatus, Line 10
Invalid column name 'TotalMRC'.
Msg 207, Level 16, State 1, Procedure LineMaterialStatus, Line 10
Invalid column name 'TotalMIV'.
My question is can we use 'as' as a variable to hold value in SQL Server ?
The normal solution to this question is to use CTEs or subqueries.
I like to point out that SQL Server has an alternative method, outer apply:
SELECT mto.LineId, mto.[LineNumber], round(sum(mto.Quantity), 3) as TotalNeed
v.*, (v.TotalMRC - v.TotalMIV ) as Shortage
FROM [SPMS2].[dbo].[ViewMTO] mto OUTER APPLY
(VALUES (SPMS2.dbo.ReturnTotalMRCByLineId(mto.LineId),
SPMS2.dbo.ReturnTotalMIVByLineId(mto.LineId),
SPMS2.dbo.ReturnTotalIMIVByLineId(mto.LineId)
)
) v(TotalMRC, TotalMIV, TotalIMIV)
GROUP BY mto.lineid, mto.[LineNumber];
Just use a subquery to achieve this:
SELECT LineId,
[LineNumber],
TotalNeed,
TotalMRC,
TotalMIV,
TotalIMIV,
(TotalMRC - TotalMIV) as Shortage
FROM (
SELECT LineId,[LineNumber],
round(sum(Quantity),3) as TotalNeed
,SPMS2.dbo.ReturnTotalMRCByLineId(LineId) as TotalMRC
,SPMS2.dbo.ReturnTotalMIVByLineId(LineId) as TotalMIV
,SPMS2.dbo.ReturnTotalIMIVByLineId(LineId) as TotalIMIV
FROM [SPMS2].[dbo].[ViewMTO]
GROUP BY lineid,[LineNumber]
) a
You can select the output from the first statement as the source subquery for the second one, which lets you use the calculated columns that you created.
You then simply move (TotalMRC - TotalMIV) as Shortage to the outer query which then has the calculated columns available.
Related
Select p.CustomerNumber, p.CustomerName, sum(CAST(a.CRAmountUSD AS DECIMAL(10,2))) AS sumCosts
From Scoring p inner join
CreditNoteDetail a
on p.SerialNumber = a.SerialNumber
where a.SerialNumber = p.SerialNumber --'FJM10696'
Group By p.CustomerNumber, p.CustomerName
having sum(CAST(a.CRAmountUSD AS DECIMAL(10,2))) > CAST(p.Amount AS DECIMAL(10,2))
I have problem, is error like this
Msg 8121, Level 16, State 1, Line 13
Column 'Scoring.Amount' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause."
I want to find a comparison value, the amount of 'CRAmountUSD' in the CreditNoteDetail table is smaller than 'Amount' in the Scoring table based on SerialNumber
This question already has answers here:
Counting DISTINCT over multiple columns
(19 answers)
Closed 9 months ago.
In SQL Server, I have a database mydb, and a table dbo.mytable in it.
I want to see if three columns (Contract_Number, Payment_Number, Task_Number) of the table can form a candidate key, by https://stackoverflow.com/a/34468508/156458
SELECT count (DISTINCT [Contract_Number], [Payment_Number], [Task_Number])
FROM [mydb].[dbo].[mytable]
but the execution gives an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
I was wondering what is going wrong?
Without count, it works fine. How should count and distinct be used together?
I also tried:
SELECT count(*)
FROM (SELECT DISTINCT [Contract_Number], [Payment_Number], [Task_Number]
FROM [PAD_Dev].[dbo].[Line_Level_Custom])
but the execution gives an error:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
Thanks.
Why don't you play with Row_Number ?
;with CTE as
(
SELECT row_number()over(partition by [Contract_Number] order by [Contract_Number]) rownum
,[Contract_Number], [Payment_Number], [Task_Number])
FROM [mydb].[dbo].[mytable]
)
select * from CTE where rownum=1
OR Count function with partition
SELECT
COUNT(*) OVER (PARTITION BY [Contract_Number]) [Contract_Number],
[Payment_Number], [Task_Number]
FROM [mydb].[dbo].[mytable]
*Query is not tested
I want to delete the duplicate records from the table So I used the CTE option but i am getting the below error message. What will be the cause? Am I did anything wrong? Upto select statement it works.
with Empcte as (
select *
, ROW_NUMBER() over (partition by ID ORDER BY ID) as RowNumber
from Empgender
)
SELECT *
FROM Empcte
delete from Empcte
where RowNumber > 1
Msg 208, Level 16, State 1, Line 8
Invalid object name 'Empcte'.
You can't use the same CTE for two different statements. (in this case first a SELECT and then a DELETE) You either have to re-create the CTE for the DELETE, or use a temp table or table variable instead of a CTE.
A CTE is a disposable view. It only persists for a single statement, and then disappears automatically. You can just avoid the SELECT statement:
WITH Empcte as (
SELECT id
,Count() as Rowcount
FROM Empgender
Group by id)
DELETE from Empgender where
Exists ( select 1 from Empgender as eg where Empcte.id = eg.id and Empcte.Rowcount >1 and );
i know there is no limit x,y in sqlserver instead of it i use:
select ROW_NUMBER(),name OVER (ORDER BY name) AS
myrow from pack where myrow > 5 and myrow < 10
but it has following error:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'myrow'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'myrow'.
any idea?
Edit
i saw What is the Equivalent syntax of mysql " LIMIT " clause in SQL Server but it didn't solve my problem.
Try this one (for 2005 and higher) -
SELECT p.name
FROM (
SELECT
name
, myrow = ROW_NUMBER() OVER (ORDER BY name)
FROM dbo.pack
) p
WHERE myrow BETWEEN 5 AND 9
Or try this (for 2012) -
SELECT name
FROM dbo.pack
ORDER BY name
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY
In SQL Server 2012 there it the OFFSET FETCH syntax
However, for older versions you have to use ROW_NUMBER but in a derived table
select
name
from
(
select
name,
ROW_NUMBER() OVER (ORDER BY name) AS myrow
from
pack
) X
where
myrow > 5 and myrow <= 10
Don't use 3 nested TOPs as per the suggested answer in the proposed duplicate
I have a typical SQL Server hierarchical query:
WITH bhp AS (
SELECT name, 0 AS level
FROM dbo.BhpNode
WHERE parent_id IS NULL
UNION ALL
SELECT a.name, level + 1
FROM dbo.BhpNode a
INNER JOIN dbo.BhpNode b
ON b.bhp_node_id = a.parent_id )
SELECT * FROM bhp
This seems to match the various examples of hierarchical queries I've found around the web, but for some reason it's producting this error:
Msg 207, Level 16, State 1, Line 12
Invalid column name 'level'.
I'm sure I'm missing something obvious, but I've stared at it too long to see it. Any idea where I'm going wrong?
Your query isn't recursive - you have to select from bhp inside the second part of the recursive CTE. Try this instead:
WITH bhp AS (
SELECT *, 0 AS [level]
FROM dbo.BhpNode
WHERE parent_id IS NULL
UNION ALL
SELECT b.*, [level] + 1
FROM bhp a
INNER JOIN dbo.BhpNode b
ON a.bhp_node_id = b.parent_id)
SELECT * FROM bhp
In the recursive section of the CTE, one of the tables you reference should be the CTE itself, shouldn't it? At the moment you are just self-joining BhpNode, and it doesn't have a level column itself.