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...
Related
I am trying to get the time it takes between 2 items before they are sold if that specific item had been sold more than 2 times and then how would I then be able to use the alias soldtime later for orderby
but I get this error:
Column 'sales.InvoiceDetailItem.DateTimeCreated' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 4
Column 'file.Item.DateTimeCreated' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SELECT
DATEDIFF(DAY,PS.[DateTimeCreated],P.[DateTimeCreated]) AS 'SoldTime'
, p.[Name]
, PS.[ItemCode]
, COUNT(*)
FROM
[ISTABLocalDB].[file].[Item] P
inner join [sales].[InvoiceDetailItem] PS on P.[LastInvoice_ID] = PS.Invoice_ID
GROUP BY
p.[Name], PS.[ItemCode]
HAVING
COUNT(*) >= 2
order by p.[Name]
I guess you want something like this:
SELECT
DATEDIFF(DAY,MIN(PS.[DateTimeCreated]),MAX(P.[DateTimeCreated])) AS 'SoldTime'
, p.[Name]
, PS.[ItemCode]
, COUNT(*)
FROM
[ISTABLocalDB].[file].[Item] P
inner join [sales].[InvoiceDetailItem] PS on P.[LastInvoice_ID] = PS.Invoice_ID
GROUP BY
p.[Name], PS.[ItemCode]
HAVING
COUNT(*) >= 2
order by p.[Name];
you can select count( PS.[ItemCode])
why you are using '*' ?
SELECT
DATEDIFF(DAY,PS.[DateTimeCreated],P.[DateTimeCreated]) AS 'SoldTime'
, p.[Name]
, PS.[ItemCode]
, count( PS.[ItemCode])
FROM
[ISTABLocalDB].[file].[Item] P
inner join [sales].[InvoiceDetailItem] PS on P.[LastInvoice_ID] = PS.Invoice_ID
GROUP BY
p.[Name], PS.[ItemCode]
HAVING
count( PS.[ItemCode]) >= 2
order by p.[Name]
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.
I am still extremely new at SQL and have recently been having issues with these 3 specific subqueries for several days.
This one is supposed to return 3 columns, only returns records of vendors from "CA" and uses a derived table.
-- Query 6
SELECT VendorID, VendorName, Vendors.VendorState
FROM Vendors
JOIN (SELECT VendorState
FROM Vendors) AS SubVendors ON Vendors.VendorID = SubVendors.VendorID
WHERE Vendors.VendorState = 'CA'
ORDER BY VendorID;
But when I run it, I get this error:
Msg 207, Level 16, State 1, Line 6
Invalid column name 'VendorID'.
This one returns 4 columns, also uses a derived table and the subquery is coded in the 'FROM' clause.
-- Query 7
SELECT
InvoicesMain.VendorID,
MAX(InvoiceTotal) AS MaxInvoice,
MIN(InvoiceTotal) AS MinInvoice,
AVG(InvoiceTotal) AS AvgInvoice
FROM
Invoices AS InvoiceMain
JOIN
(SELECT TOP 10
VendorID, AVG(InvoiceTotal) AS AvgInvoice
FROM Invoices
GROUP BY VendorID
ORDER BY AvgInvoice DESC) AS TopVendor ON InvoicesMain.VendorID = TopVendor.VendorID)
GROUP
InvoicesMain.VendorID
ORDER BY
MaxInvoice DESC;
But I get this error:
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'.
And lastly, I am honestly not sure how to do this one; it is supposed to be restated as a subquery:
SELECT
InvoiceNumber, InvoiceDate, InvoiceLineItemAmount
FROM
Invoices
JOIN
InvoiceLineItems ON Invoices.InvoiceID = Invoicelineitems.InvoiceID
WHERE
VendorID = 122
ORDER BY
InvoiceDate;
Any tips, please?
On Query 6, you didn't select the VendorID on your subquery, that's why the error occurred. Try this query instead:
SELECT VendorID, VendorName, Vendors.VendorState
FROM Vendors JOIN
(SELECT VendorState, VendorID
FROM Vendors) AS SubVendors
ON Vendors.VendorID = SubVendors.VendorID
WHERE V.VendorState = 'CA'
ORDER BY VendorID;
For Query 7 you are just missing a ) on your subquery.
SELECT InvoicesMain.VendorID, MAX(InvoiceTotal) AS MaxInvoice, MIN(InvoiceTotal) AS MinInvoice, AVG(InvoiceTotal) AS AvgInvoice
FROM Invoices AS InvoiceMain
JOIN
(SELECT TOP 10 VendorID, AVG(InvoiceTotal)) AS AvgInvoice
FROM Invoices
GROUP BY VendorID
ORDER BY AvgInvoice DESC) AS TopVendor
ON InvoicesMain.VendorID = TopVendor.VendorID)
GROUP InvoicesMain.VendorID
ORDER BY MaxInvoice DESC;
For the last item, you can reinstate this as subquery by selecting InvoiceID from the table InvoiceLineItems just like in Query 6
SELECT InvoiceNumber, InvoiceDate, InvoiceLineItemAmount
FROM Invoices JOIN
(SELECT InvoiceID FROM Invoicelineitems) As I
ON Invoices.InvoiceID = I.InvoiceID
WHERE VendorID = 122
ORDER BY InvoiceDate;
I want to count the number of etape an Employee work in a project
My table temps are:
Temps:
noProjet
noEtape
noEmploye
annee
mois
jrsHm
My query is
select
t.noEmploye, t.NoProjet, c.count
from
temps t
inner join
(select
noProjet, noEtape, count(*) as count
from
temps
group by
noProjet) c on t.noProjet = c.noProjet
order by
noEmploye
Error I get is
Msg 8120, Level 16, State 1, Line 1
Column 'temps.noEtape' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
The error is self explanatory. Remove noEtape column from subquery. If column is not in the group list you can not select it unless you apply some aggregations to that column:
select t.noEmploye, t.NoProjet, c.count
from temps t
inner join (select noProjet, count(*) as count
from temps
group by noProjet ) c on t.noProjet = c.noProjet
order by noEmploye
Or:
select t.noEmploye, t.NoProjet, c.count
from temps t
inner join (select noProjet, max(noEtape) as noEtape, count(*) as count
from temps
group by noProjet ) c on t.noProjet = c.noProjet
order by noEmploye
Is this what you want?
select distinct
noEmploye,
noProjet,
sum(noEtape) over (
partition by noProjet
) as EtapesdeProjet
from Temps
order by noEmploye
Maybe you can do this by using next query:
select
t.noEmploye,
t.NoProjet,
c.count
from temps t
inner join (select noProjet, noEtape, count(*) as count from temps group by noProjet, noEtape) as c
on t.noProjet = c.noProjet
order by noEmploye
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;