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
Related
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 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 am trying to wrap my head round this issue and I am sure the answer exists here a million times but then I am not searching for the right question.
I have a huge sales table [SALES] and I am extracting
SELECT DISTINCT S1.[ORDER ID], S1.SUPPLIER, SUM(S1.[ORDER TOTAL]) AS SUPPLIERTOTAL
FROM [SALES] S1
LEFT JOIN
(
Select s2.[Order ID], S2.[Supplier], S2.[Supplier Colour], SUM(S2.[Order TOTAL]) AS COLOURTOTAL
FROM [SALES]
WHERE [SALES].[SALESDATE] Between '20160101' and '20170101'
) AS s2
ON s1.[Order ID] = s2.[Order ID]
I have thrown this code together as an illustration as I am not by my work PC at present. My issue is that when I do get the re-select to work it produces the correct order value from the first select.
E.G Lets say the manufacturer was Ford and the total value was 100000 over ten orders it returns the 100000 correctly however on the sub select it appears to take the total value and multiply it by the total number of rows in the table. I am trying to work out what is going on with the data and query but cannot see the issue.
The only factor if its of influence is that the table has no primary key but as I am providing referential integrity with the join didn't believe that would be the case...
Anyone able to answer or come across this issue>
A bit of guessing here, as the question is not too clear, but I think you are looking for something like this:
SELECT S1.[ORDER ID], S1.SUPPLIER, SUM(S1.[ORDER TOTAL]) AS SUPPLIERTOTAL, SUM(S2.COLOURTOTAL) as COLOURTOTAL
FROM [SALES] S1
LEFT JOIN
(
Select s2.[Order ID], S2.[Supplier], S2.[Supplier Colour], S2.[Order TOTAL] AS COLOURTOTAL
FROM [SALES]
WHERE [SALES].[SALESDATE] Between '20160101' and '20170101'
) AS s2
ON s1.[Order ID] = s2.[Order ID]
GROUP BY S1.[ORDER ID], S1.SUPPLIER
Good day,
I have an SQL code that return to me all quantities that I received over time, but I want to display only the latest one
SELECT * FROM
(SELECT DISTINCT
[dbo].[ttcibd001110].[t_cmnf] AS [Manufacturer],
[dbo].[ttcibd001110].[t_item] AS [Item code],
[dbo].[ttcibd001110].[t_dsca] AS [Description],
[dbo].[ttcibd001110].[t_seak] AS [Search key 1],
[dbo].[twhinr110110].[t_trdt] AS [Transaction date],
[dbo].[twhinr110110].[t_cwar] AS [Warehouse],
[dbo].[twhinr110110].[t_qstk] AS [Quantity Inventory Unit]
FROM [dbo].[twhinr110110] LEFT JOIN [dbo].[ttcibd001110]
ON [dbo].[twhinr110110].[t_item]=[dbo].[ttcibd001110].[t_item]
WHERE [dbo].[twhinr110110].[t_koor]='2' AND [dbo].[ttcibd001110].[t_cmnf]='ManufacturerX') AS tabel
WHERE ltrim(tabel.[Item code])='1000045'
Now, from this selection I want to select only the line with the latest Transaction date, but I am stuck.
Can somebody help me in this way?
Thank you!
Change your beginning to
SELECT TOP 1
and after where use
ORDER BY [Transaction date] DESC
How can I count the total number of sales in the "FactInternetSales" table for the selected member of the DATES hierarchy?
(
[Order Date].[DATES].CURRENTMEMBER,
COUNT(DISTINCT([Fact Internet Sales].[Sales Order Number].[Sales Order Number]
)
this is what I have at the moment however it doesn't work :/
Try count on the outside with extra braces to indicate a set of tuples:
COUNT(
{
[Order Date].[DATES].CURRENTMEMBER
*
[Fact Internet Sales].[Sales Order Number].[Sales Order Number]
}
)
with set abc as
EXISTS([Fact Internet Sales].[Sales Order Number].[Sales Order Number],
[Order Date].[DATES].CURRENTMEMBER,
"Fact Internet Sales"
)
member [Measures].[Count]
AS Count(Distinct(abc))
SELECT [Measures].[Count] on 0
from [YourCube]
WHERE Order Date].[DATES].&[01/01/2015] --Some random value for slicer