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
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
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;
I have a data set in which I would like to update a column PREVACCEPTID.
The update is based on the contents of the same table, a sample data is shown below:
The column should be updated after a search to see if station has had previous acceptances and what was this?
If we SELECT all DISTINCT 'ACCEPTID' for station A we would get the below.
I want to use this DISTINCT ACCEPTID to populate 'PREVACCEPTID'.
So whereever I have an entry with for e.g. '142692', I would lookup the sub-table and check if there are exists any previous ACCEPTID s, if that is the case populate with the previous one, in this case '142691' (see after results table as they are populated)
I have tried a few things now, I am getting an error for the below:
UPDATE a
SET a.PREVACCEPTID = (CASE
WHEN COUNT(DISTINCT b.ACCEPTID) = 1
THEN b.ACCEPTID
WHEN COUNT(DISTINCT b.ACCEPTID) > 1
AND b.ACCEPTID <> MIN(a.ACCEPTID)
THEN b.ACCEPTID - 1
END)
FROM dbo.table a
RIGHT JOIN dbo.table b ON b.STATION = a.STATION
AND b.PERIOD = a.PERIOD
AND b.ACCEPTID = a.ACCEPTID
I get this error:
Msg 157, Level 15, State 1, Line 326
An aggregate may not appear in the set list of an UPDATE statement.
The end result is per below:
I think a cte would be better option, but i have never used one.
Thanks in advance.
If I interpret your question and subsequent comments correctly, I assume you want the previous AcceptID to be populated to be last AcceptID for a given set of rows sharing the same Station and Period. Last I assume would be defined by the time components (StackDate and QTime). And, in the case where there is only one row for a given Station and Period, you'd want the Previous AcceptID to be set to be the same as AcceptID for that row.
Under the above conditions, below is a query that will work. Note: Replace table 'Test' with your own table name.
UPDATE t SET PrevAcceptID =
ISNULL(
(SELECT TOP 1 AcceptID
FROM Test t2
WHERE t2.Station = t.Station AND t2.Period = t.Period AND t2.AcceptID < t.AcceptID ORDER BY StackDate DESC, QTime DESC),
AcceptID)
FROM Test AS t
Picture of current state and desired state
Need help MSSQL query to bring table with two columns into one showing unique value of field 1 and corresponding values in field 2 (in that order)? See picture attached in link above
Your request sounds quite strange, but if you want to do it, one way is to use rollup + grouping_id like this:
select case when grouping_id(field2) = 1 then field1 else field2 end
from yourtable
group by field1, rollup(field2)
order by field1, grouping_id(field2) desc, field2
Usually you'd use this for subtotals, but using it with case you can add additional rows to the data.
This will return:
x
a
b
c
d
y
e
f
g
z
h
i
Edit: How to list table + column names:
select case when grouping_id(c.name) = 1 then t.name else c.name end
from sys.tables t join sys.columns c on t.object_id = c.object_id
group by t.name, rollup(c.name)
order by t.name,c.name
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