I've created the following but it keeps coming up with the error message You tried to execute a query that does not include the specified expression 'ICE Team' as part of an aggregate function.
SELECT ztSub.[Master Sheet].[ICE Team], ztSub.[date], Count(ztSub.[Count])
FROM (SELECT [Master Sheet].[ICE Team],[Master Sheet].[Visit Date (planned for)] AS [date],Count([Master Sheet]![Visit Date (planned for)]) AS [Count]
FROM [Master Sheet]
UNION
SELECT [Master Sheet].[ICE Team],[Master Sheet].[Date retasked to?] AS [date], Count ([Master Sheet]![Date retasked to?]) AS [Count]
FROM [Master Sheet] ) AS ztSub
GROUP BY ztSub.[Master Sheet].[ICE Team];
This now works. I incorporated a sum of the [Count of Dates] and its given me my results.
SELECT [Total].[Ice Team] AS [Ice Team], ztSub.Period, Sum(ztSub.[Count of Dates]) AS [SumOfCount of Dates]
FROM (SELECT Total.[Ice Team], Total.[Re-Visited] AS Period, Count([Total]![Re-Visited]) AS [Count of Dates]
FROM Total
GROUP BY [Ice Team], [Re-Visited]
UNION SELECT Total.[Ice Team], Total.Visited AS Period, Count([Total]![Visited]) AS [Count of Dates]
FROM Total
GROUP BY [Ice Team], Visited) AS ztSub
GROUP BY [Total].[Ice Team], ztSub.Period;
If the only result you are looking for is a list of dates and count of occurences, you might want to try a query with a union:
SELECT date, count(*)
FROM (
(SELECT date_col1 AS date
FROM table1)
UNION
(SELECT date_col2 AS date
FROM table1) )
GROUP BY date
EDIT FOR UPDATED QUESTION:
Your error comes from the fact that you do the count in your inner queries but the group by on your outer query. If you look at one of your inner queries separately:
SELECT [Master Sheet].[ICE Team],[Master Sheet].[Visit Date (planned for)] AS [date],Count([Master Sheet]![Visit Date (planned for)]) AS [Count]
FROM [Master Sheet]
then we still get the same exact error. This is caused by the fact that you do a count of your dates in the select clause without specifying what to do with the [ICE Team] value. You might want to include a group by-clause for the [ICE Team] value. You already did for the outer query, but it should be done in this inner query too. Same goes for your Visit Date (planned for) value, otherwise you'll get the same error for that one.
Try this query, although I'm not sure it'll give the results as you want them too. (For that, you'd have to add a clear example of input and expected output to your question.)
SELECT ztSub.[Master Sheet].[ICE Team], ztSub.[date], Count(ztSub.[Count])
FROM (SELECT [Master Sheet].[ICE Team],[Master Sheet].[Visit Date (planned for)] AS [date],Count([Master Sheet]![Visit Date (planned for)]) AS [Count]
FROM [Master Sheet]
group by [ICE Team],[Visit Date (planned for)]
UNION
SELECT [Master Sheet].[ICE Team],[Master Sheet].[Date retasked to?] AS [date], Count ([Master Sheet]![Date retasked to?]) AS [Count]
FROM [Master Sheet]
group by [ICE Team], [Date retasked to?] ) AS ztSub
GROUP BY ztSub.[Master Sheet].[ICE Team],date;
Related
I'm trying to develop a simple query in which I'm trying to do Count of distinct values from a field, i have tried to use DISTINCT in query which throws error.
Please guide me to fix this code, thanks.
SELECT TSData.[Upd Bay], Sum(TSData.RT) AS RT, Sum(TSData.OT) AS OT, Count(DISTINCT TSData.[EMP No]) AS EmpCount
FROM TSData
WHERE (((TSData.[TS Date])=Nz([Forms]![TSDataDeptSummary]![CmbTSDate],Date()-1)))
GROUP BY TSData.[Upd Bay];
enter image description here
You can separately query those parts and combine as subqueries:
select A.[Upd Bay], A.[TS Date], DCnt.EmpCnt
from
(
SELECT A.[Upd Bay], A.[TS Date]
FROM TSData A
WHERE A.[Ts Date]=Nz([Forms]![TSDataDeptSummary]![CmbTSDate],Date()-1)
group by A.[Upd Bay], A.[TS Date]
) A
inner join
(
select [Upd Bay], count([Emp No]) as EmpCnt
from (
select distinct B.[Upd Bay], B.[Emp No]
from TSData B
where B.[Ts Date]=Nz([Forms]![TSDataDeptSummary]![CmbTSDate],Date()-1)
) as Dnt
group by [Upd Bay]
) as DCnt
on A.[Upd Bay]=DCnt.[Upd Bay]
I am trying to select the max date over the year, but it is not working. Any ideas on what to do?
SELECT a.tkinit [TK ID],
YEAR(a.tkeffdate) [Rate Year],
max(a.tkeffdate) [Max Date],
tkrt03 [Standard Rate]
FROM stageElite.dbo.timerate a
join stageElite.dbo.timekeep b ON b.tkinit = a.tkinit
WHERE a.tkinit = '02672'
and tkeffdate BETWEEN '2014-01-01' and '12-31-2014'
GROUP BY a.tkinit,
tkrt03,
a.tkeffdate
Perhaps you only want it by year and not rolled up by calendar date. For SQL server you can try this.
SELECT
…
MaxDate = MAX(a.tkeffdate) OVER (PARTITION BY a.tkinit, YEAR(a.tkeffdate)))
…
Or you could modify the query above to group by the year instead of date-->
GROUP BY a.tkinit,
tkrt03,
YEAR(a.tkeffdate)
You seem to want only one row and all the columns. Use ORDER BY and TOP:
SELECT TOP (1) tr.tkinit as [TK ID],
YEAR(tr.tkeffdate) as [Rate Year],
a.tkeffdate as [Max Date],
tkrt03 as [Standard Rate]
FROM stageElite.dbo.timerate tr JOIN
stageElite.dbo.timekeep tk
ON tk.tkinit = tr.tkinit
WHERE tr.tkinit = '02672' AND
tr.tkeffdate >= '2014-01-01' AND
tr.tkeffdate < '2015-01-01'
ORDER tr.tkeffdate DESC;
Note that I also fixed your date comparisons and table aliases.
I'm using this query to get previous 12 months Sales.
SELECT *
FROM (SELECT
gp_etablissement [ETABLISSEMENT],
datename(month, dateadd(m,-1,getdate())) [Month],
COUNT(1) [Sales Count]
FROM PIECE
GROUP BY gp_etablissement,
datename(month, dateadd(m,-1,getdate()))) AS MontlySalesData
PIVOT( SUM([Sales Count])
FOR Month IN ([January],[February],[March],[April],[May],
[June],[July],[August],[September],[October],[November],
[December])) AS MNamePivot
I got this error :
Each GROUP BY expression must contain at least one column that is not
an outer reference.
I've changed many columns in the group by, but same problem.
Use the below code for eliminating the error. The pivot doesn't work with the subquery that way when the group by uses date parts.
;WITH CTE
AS
(
SELECT
gp_etablissement [ETABLISSEMENT],
datename(month, dateadd(m,-1,getdate())) [Month],
COUNT(1) [Sales Count]
FROM PIECE
GROUP BY gp_etablissement,
datename(month, dateadd(m,-1,getdate()))
)
SELECT *
FROM CTE
PIVOT( SUM([Sales Count])
FOR Month IN ([January],[February],[March],[April],[May],
[June],[July],[August],[September],[October],[November],
[December])) AS MNamePivot
I have my query:
SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER
(ORDER BY [Shipment Date]) as [Total Running Costs]
FROM...
This gets me 3 columns:
Shipment Date | Running Costs | Total Running Costs
I would like to add a fourth column to this query which has the same value for all rows, and the same number of rows as my original query results.
I know you could add for example '999'as Something to the search results, but how can I do the same for a sum of another column (example: Imagine the total sum of the a column in another table is 1500, and I want to have 1500 for all rows in the fourth column. Something like select sum(column_name)?
The database engine is MSSQL.
You can use a nested query
SELECT [Shipment Date], [Amount] as [Running Costs], [Total Running Costs], SUM([Total Running Costs] OVER ())
FROM
(
SELECT [Shipment Date], [Amount] as [Running Costs], Sum([Amount]) OVER
(ORDER BY [Shipment Date]) as [Total Running Costs]
FROM...
)
Nested window function should also work
SUM(SUM([Running costs]) OVER (ORDER BY [Shipment Date])) OVER ()
I have looked through the forum and can find a variety of examples to solve my problem but just cannot put everything together.
My situation is typical that I would like to show the Top 10 customers (Orders.[Customer Name]) by group (Shop_Lookup.ShopGroup]) for their total revenue.
I can get so far in producing the overall Top 10 regardless of ShopGroup but just cannot get my head around getting the Sub Query to work. My current code is -
SELECT TOP 10 Orders.[Customer Name],
Sum(Orders.[Actual Revenue]) AS [SumOfActual Revenue],
Orders.[This Month],
Shop_Lookup.[ShopGroup]
FROM Orders
INNER JOIN Shop_Lookup ON Orders.[ShopID] = ShopLookup.[ShopID]
WHERE ((Orders.[This Month])="current")
GROUP BY Orders.[Customer Name], Orders.[This Month], Shop_Lookup.[ShopGroup]
ORDER BY Sum(Orders.[Actual Revenue]) DESC;
Completely AIR CODED ! Proceed with caution.
You can use Sub Query to get this !
SELECT
Orders.[Customer Name],
Sum(Orders.[Actual Revenue]) AS [SumOfActual Revenue],
Orders.[This Month],
Shop_Lookup.[ShopGroup]
FROM
Orders
INNER JOIN
Shop_Lookup
ON
Orders.[ShopID] = ShopLookup.[ShopID]
WHERE
(
(Orders.[This Month] = 'Current')
AND
(Orders.ShopID IN
(SELECT
TOP 10 ShopID
FROM
Orders AS Dupe
WHERE
Dupe.ShopID = Orders.ShopID
)
)
)
GROUP BY
Orders.[Customer Name],
Orders.[This Month],
Shop_Lookup.[ShopGroup]
ORDER BY
Sum(Orders.[Actual Revenue]) DESC;
More information on Subqueries : http://allenbrowne.com/subquery-01.html