I get the following error :
Msg 8120, Level 16, State 1, Line 1
Column 'TempRH.utilisateur_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
When executing the following query :
SELECT MAX(TempRH.poids)as poids,TempRH.utilisateur_id
FROM TempRH
INNER JOIN (SELECT TempRH.utilisateur_id
FROM TempRH
GROUP BY TempRH.utilisateur_id
HAVING COUNT(*)>2) t
ON t.utilisateur_id =TempRH.utilisateur_id
Please help me .
TRY THIS
SELECT *
FROM(
SELECT MAX(TempRH.poids)as poids,TempRH.utilisateur_id
FROM TempRH
INNER JOIN
(SELECT TempRH.utilisateur_id
FROM TempRH
GROUP BY TempRH.utilisateur_id
HAVING COUNT(*)>2)t
ON t.utilisateur_id =TempRH.utilisateur_id
GROUP BY TempRH.utilisateur_id
)x
Because you are using an Aggregate function, you need to GROUP BY any other columns from your SELECT statement. Adding
GROUP BY TempRH.utilisateur_id
to the end of your query will resolve the error.
Because you are selecting utilisateur_id together with the Max(poids), you will need a group by on the outer level as well.
One point - if you are joining back to the same table from what looks like to be a key, it should be possible to simplify your query as follows:
SELECT MAX(TempRH.poids)as poids, TempRH.utilisateur_id
FROM TempRH
GROUP BY TempRH.utilisateur_id
HAVING COUNT(*) > 2;
Related
in the select statement below I have 707 row
select detail_serial, Price from apa_invoice_detail
and I have another select statement which is
Select max(detail_serial)
,asc_item.item_name_2
,asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2
and this one gives me 197 row
I need to get the price from the first statement for the detail_serial in the second statement (for only 197 row)
I tried this:
select detail_serial, Price from apa_invoice_detail where detail_serial in
(Select max(detail_serial)
,asc_item.item_name_2
,asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2)
but it gives me "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
How can I solve this ???
try removing the columns in the inner select statement
select detail_serial, Price from apa_invoice_detail where detail_serial in
(Select max(detail_serial)
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2)
Use EXISTS instead of IN.
;WITH SecondQueryResults AS
(
Select max(detail_serial) detail_serial
, asc_item.item_name_2
, asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2
)
select
detail_serial,
Price
from
apa_invoice_detail T
where
EXISTS (SELECT 'serial exists on query result' FROM SecondQueryResults AS S WHERE S.detail_serial = T.detail_serial)
... or just select 1 column (the detail_serial) inside your IN subquery.
SELECT DISTINCT(t1.Ticker),t2.SecurityID,t2.ClosePrice,t2.QuoteDateTime FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2
ON t2.SecurityID =t1.SecurityID
WHERE t2.QuoteDateTime IN (SELECT max(QuoteDateTime) FROM [Hub].[SecurityMaster].[SecurityPrices]) AND t1.SecurityTypeName = 'REIT'
I get an output with no data. The subquery doesn't run along with the other filter in the WHERE clause. I am not sure what I am doing wrong. Can somebody please help!
If you are trying to get the lastest row from SecurityPrices for each Ticker, one option is to use cross apply():
select --distinct /* distinct not needed if `Ticker` is unique on `smd`
smd.Ticker
, sp.SecurityID
, sp.ClosePrice
, sp.QuoteDateTime
from [Hub].[SecurityMaster].[SecurityMasterDetails] as smd
cross apply (
select top 1
i.SecurityID
, i.ClosePrice
, i.QuoteDateTime
from [Hub].[SecurityMaster].[SecurityPrices] i
where i.SecurityID = smd.SecurityID
order by i.QuoteDateTime desc
) as sp
where SecurityTypeName = 'REIT' /* which table does this column belong to? */
I think your query would be
SELECT DISTINCT TOP 1 WITH TIES
t1.Ticker,
t2.SecurityID,
t2.ClosePrice,
t2.QuoteDateTime
FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2 ON t2.SecurityID =t1.SecurityID
WHERE SecurityTypeName = 'REIT'
ORDER BY t2.QuoteDateTime DESC
You aren't getting results because the max(QuoteDateTime) record doesn't have SecurityTypeName = 'REIT'. I think you want the max(QuoteDateTime) for this SecurityTypeName, so this can be done with an INNER JOIN.
SELECT DISTINCT
(t1.Ticker),
t2.SecurityID,
t2.ClosePrice,
t2.QuoteDateTime
FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2
ON t2.SecurityID =t1.SecurityID
INNER JOIN
(SELECT max(QuoteDateTime) DT FROM [Hub].[SecurityMaster].[SecurityPrices]) P on P.DT = t2.QuoteDateTime
WHERE SecurityTypeName = 'REIT'
EDIT
Your data doesn't have what you think it does, I suspect. Here is how you can check...
--Find the SecurityID that matches the max date
SELECT
SecurityID ,
max(QuoteDateTime) DT
FROM [Hub].[SecurityMaster].[SecurityPrices]
GROUP BY SecurityID
--I'm betting this ID isn't in your SecurityMasterDetails where the Type is REIT
SELECT DISTINCT
SecurityID
FROM SecurityMasterDetails
WHERE SecurityTypeName = 'REIT'
Since the SecurityID returned in the first query isn't in the second query result set, you are going to get NULL results.
SQL Server :
SELECT tblModule.Title , Count (tblActivity.Name) AS NumberOfActivities
From tblActivity
INNER JOIN tblModule
ON tblModule.ModuleID = tblActivity.ModuleID
Order by tblModule.Title
In here I am trying to display the number of times Name is displayed in tblActivity for each Module Title , How would I do this the error is :
Msg 8120, Level 16, State 1, Line 1
Column 'tblModule.Title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
And the actual question : For each module, list the module title and the number of activities scheduled for the module.(5)
SELECT m.Title, COUNT_BIG(a.Name) AS NumberOfActivities
FROM dbo.tblActivity a
JOIN dbo.tblModule m ON m.ModuleID = a.ModuleID
GROUP BY m.Title
ORDER BY m.Title
or
SELECT m.Title, NumberOfActivities
FROM (
SELECT ModuleID, COUNT_BIG(*) AS NumberOfActivities
FROM dbo.tblActivity
GROUP BY ModuleID
) a
JOIN dbo.tblModule m ON m.ModuleID = a.ModuleID
ORDER BY m.Title
anyway, please provide actual and excepted result...
I am trying to inner join 2 temp tables
I know this can be done, I have done this before but i completely forgot how to do it
Please advise meBelow is the query that I try to execute
select tmp1.*, tmp2.cnt from
(
select
1 as ClassificationType,
tblMatches.IdGame,
tblMatches.IdPlayer,
sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
from
tblMatches
group by IdPlayer, IdGame
) as tmp1
inner join (select IdWinner, count(IdWinner) as cnt from tblCompetitions where IdWinner = tmp1.IdPlayer) as tmp2
on tmp2.IdWinner = tmp1.IdPlayer
This will fail with
I think I am not allowed to use tmp1 in the subquery that create tmp2
Msg 4104, Level 16, State 1, Line 17
The multi-part identifier
"tmp1.IdPlayer" could not be bound.
You are not trying to join two temp tables, but two derived tables.
You cannot access the inner data of one derived table in outside of it unless it's in the SELECT clause.
Try the following:
select tmp1.*, tmp2.cnt from
(
select
1 as ClassificationType,
tblMatches.IdGame,
tblMatches.IdPlayer,
sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
from
tblMatches
group by IdPlayer, IdGame
) as tmp1
inner join (select IdWinner, count(IdWinner) as cnt from tblCompetitions GROUP BY IdWinner) as tmp2
on tmp2.IdWinner = tmp1.IdPlayer
select
1 as ClassificationType,
tmp1.IdGame,
tmp1.IdPlayer,
sum(tmp1.Score) as Score,
sum(tmp1.Points) as Points,
sum(tmp1.OpponentScore) as OpponentScore,
count(tmp1.ID) as MatchesCount,
count(distinct tmp1.IdCompetition) as CompetitionsCount,
count(tmp2.IdWinner) as cnt
from
tblMatches tmp1
inner join
tblCompetitions tmp2
on tmp2.IdWinner = tmp1.IdPlayer
group by
tmp1.IdPlayer,
tmp1.IdGame
The where clause in tmp2 duplicates the join condition:
inner join (select IdWinner, count(IdWinner) as cnt
from tblCompetitions
where IdWinner = tmp1.IdPlayer) as tmp2
on tmp2.IdWinner = tmp1.IdPlayer
Just remove the where clause. In addition, like Astander noted in his now deleted post, the second query needs a group by too:
inner join (select IdWinner, count(IdWinner) as cnt
from tblCompetitions
group by IdWinner) as tmp2
on tmp2.IdWinner = tmp1.IdPlayer
The reason you can't reference the outer query from a subquery is that this would make the right part of the join depend on the left part of the join.
You shouldn't actually need the second subquery. What about this?
SELECT tmp1.ClassificationType, tmp1.IdGame, tmp1.IdPlayer,
COUNT(tblCompletions.IdWinner) as cnt FROM
(
SELECT
1 as ClassificationType,
tblMatches.IdGame,
tblMatches.IdPlayer,
sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
FROM
tblMatches
GROUP BY IdPlayer, IdGame
) as tmp1
INNER JOIN tblCompletions ON tmp1.IdPlayer = tblCompletions.IdWinner
GROUP BY tmp1.ClassificationType, tmp1.IdGame, tmp1.IdPlayer
i want the Output of the Following Query in One row
i want to merge Below SQL Query
Help me please
select Provider_ID,Circel_ID,count(distinct td_all.ID),t_det.BillNoTemp from TAPINOUT_DIFFERENCES_ALL td_all
inner join TransferDetails t_det on td_all.bill_no=t_det.Bill_No
where td_all.bill_no not in (select bill_no from TAPINOUT_DIFFERENCES_ALL where Status='Open') and sourcename='TransferDetails'
group by td_all.Provider_ID,td_all.Circel_ID,t_det.BillNoTemp
order by td_all.Provider_ID,td_all.Circel_ID
select td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp ,count(distinct td_all.ID)as count from TAPINOUT_DIFFERENCES_ALL td_all
INNER JOIN TAPIN_Details TAP_DET ON td_all.FILENAME=TAP_DET.FLNAME
where td_all.SOURCENAME='TransferDetails' and td_all.Status='Open'
group by td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp
order by td_all.Provider_ID,td_all.Circel_ID
select td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp,count(distinct td_all.ID)AS COUNT from TAPINOUT_DIFFERENCES_ALL td_all
inner join TAPIN_Details TAP_DET on td_all.FILENAME=TAP_DET.FLNAME
where td_all.anb_comments='Invoice Not Found'
group by td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp order by td_all.Provider_ID,td_all.Circel_ID
select td_all.Provider_ID,td_all.Circel_ID,t_det.BillNoTemp,count(distinct td_all.ID) from TAPINOUT_DIFFERENCES_ALL td_all
inner join TransferDetails t_det on td_all.bill_no=t_det.Bill_No
where td_all.anb_comments='IT File not found'
group by td_all.Provider_ID,td_all.Circel_ID,t_det.BillNoTemp order by td_all.Provider_ID,td_all.Circel_ID
I think you're looking for the UNION operator, which lets you append the results of multiple queries into a single result set.
It works like so:
SELECT columns FROM tbl1 WHERE criteria
UNION
SELECT columns FROM tbl2 WHERE criteria
Use UNION keyword between statements.
SELECT bla, bla2 FROM table1
UNION ALL
SELECT bla3, bla4 FROM table2