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
Related
I got an error when I tried to solve this problem. First I need to count all values of 2 tables then I need in where condition get all max values.
My code:
Select *
FROM (
select Operator.OperatoriausPavadinimas,
(
select count(*)
from Plan
where Plan.operatoriausID= Operator.operatoriausID
) as NumberOFPlans
from Operator
)a
where a.NumberOFPlans= Max(a.NumberOFPlans)
I get this error
Msg 147, Level 15, State 1, Line 19
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
I don't know how to solve this.
I need get this http://prntscr.com/p700w9
Update 1
Plan table contains of http://prntscr.com/p7055l values and
Operator table contains of http://prntscr.com/p705k0 values.
Are you looking for... an aggregate query that joins both tables and returns the record that has the maximum count?
I suspect that this might phrase as follows:
SELECT TOP(1) o.OperatoriausPavadinimas, COUNT(*)
FROM Operatorius o
INNER JOIN Planas p ON p.operatoriausID = o.operatoriausID
GROUP BY o.OperatoriausPavadinimas
ORDER BY COUNT(*) DESC
If you want to allow ties, you can use TOP(1) WITH TIES.
You can use top with ties. Your query is a bit hard to follow, but I think you want:
select top (1) with ties o.OperatoriausPavadinimas, count(*)
from plan p join
operator o
on p.operatoriausID = o.operatoriausID
group by o.OperatoriausPavadinimas
order by count(*) desc;
Manipulate this data in order to return the total value (Units * Price) of the client holdings in each stock.
select sh.StockCode , sum(sh.Units*sp.PriceNZD) as 'TotalPrice'
from StockPrice sp
inner join StockHoldings sh on sp.StockCode=sh.StockCode
group by sh.StockCode
order by TotalPrice desc;
Error: Msg 8117, Level 16, State 1, Line 1
Operand data type nvarchar is invalid for multiply operator.**
Any idea?
You have to cast data to proper format:
select sh.StockCode ,
sum(CAST(sh.Units AS INT)*CAST(sp.PriceNZD AS DECIMAL(18,2))) AS TotalPrice
from StockPrice sp
inner join StockHoldings sh on sp.StockCode=sh.StockCode
group by sh.StockCode
order by TotalPrice desc;
If you are using SQL Server 2012+ you could use TRY_CAST to avoid invalid conversions (invalid data will be treated as NULL)
Proper solution: alter your schema and store data with correct datatypes.
Select
tblActivity.RoomID,
tblRoomEquipment.EquipmentType,
COUNT(tblActivity.Name) AS NumberOfActivities
from tblActivity
inner join tblRoomEquipment
on tblRoomEquipment.RoomID= tblActivity.RoomID
where tblRoomEquipment.EquipmentType = 'W'
group by tblActivity.RoomID
In here I want to display the number of times a RoomID that has a specific equipment type in this case it is 'W' so I used the Name column from tbl Activity to determine that meaning the number of times W occurs in that Name the ID will be displayed its pretty confusing to explain.
So heres the question itself: List the number of activities that take place in rooms which have a Whiteboard in them. (5)
The error:
Msg 8120, Level 16, State 1, Line 1 Column
'tblRoomEquipment.EquipmentType' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY
clause.
SELECT
a.RoomID,
re.EquipmentType,
COUNT(a.Name) AS NumberOfActivities
FROM tblActivity as a
INNER JOIN tblRoomEquipment as re ON re.RoomID= a.RoomID
WHERE re.EquipmentType = 'W'
GROUP BY a.RoomID, re.EquipmentType
Goal: Show the ID, title of the books and the pub. Year for the book with the highest sales amount; include ties. Use the total extended cost (quantity * order price) when determining the sales of a book.
Rules:
use subquery only
no joins
no union
only subquery
Query:
SELECT book_id,
title,
year_publd
FROM bkinfo.books
where book_id =
(
SELECT TOP 1 WITH TIES book_id, quantity*order_price as Extended_cost
from bkorders.order_details
order by quantity*order_price DESC
)
;
Error:
Msg 116, Level 16, State 1, Line 10 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
The subquery shows the book_id with the highest amount but when running it as a whole, I receive an error. Please explain what I should do.
;with cte as
(
select book_id, quantity*order_price as Extended_cost from bkorders.order_details
order by Extended_cost DESC
)
select top 1 with ties * from cte
SELECT book_id, title, year_publd
FROM bkinfo.books
where book_id =
(
SELECT TOP 1 book_id
from bkorders.order_details
order by quantity*order_price DESC
)
;
SELECT part_number,
price,
(Select max(sent_to_t) AS 'T' From [CIMSDB].[dbo].[price] where sent_to_t < '06/04/2013' Group by part_number)
FROM [CDB].[dbo].[part]PAB Inner Join [CDB].[dbo].[price] PR
ON PA.part_id = PR.part_id
Order By part_number
What I am trying to do is get a list of parts, and thier prices, for the most recent send_to_t date that is before 6/4. I am currently recieving -"Error Message
Server: Msg 164, Level 15, State 1, Line 1
Each GROUP BY expression must contain at least one
column that is not an outer reference."
You don't need a MAX at all (which is uncorrelated with the main query above, hence the error). Even if correlated, it would give you the most recent send_to_t value before some date but not the price on that date. It would simply give the most recent price with an unconnected date.
You asked (my bold)
get a list of parts, and their prices, for the most recent send_to_t date that is before 6/4
This gives you the most recent price for a part before 06/04/2013
Basically, a "top 1 per group" but with a date filter)
SELECT
part_number,
price,
sent_to_t
FROM
(
SELECT
part_number,
price,
sent_to_t,
ROW_NUMBER() OVER (PARTITION BY part_number ORDER BY sent_to_t DESC) AS rn
FROM
[CDB].[dbo].[part] PAB
Inner Join
[CDB].[dbo].[price] PR ON PAB.part_id = PR.part_id
WHERE
sent_to_t < '06/04/2013'
) X
WHERE
X.rn = 1
ORDER BY
part_number;
Note 06/04/2013 is ambiguous: is it 6th April or 4th June? You should use yyyymmdd for safety