Short answer: Yes.
Longer answer: For all intents and purposes, when you do
Code:
SELECT ...
FROM ( SELECT x,y,z FROM sometable ) AS someAlias
WHERE ...
that sub-select (or inner select) is treated as if it's another table. Or perhaps you'd prefer to think of it as a VIEW (if you are using SQL Server) or STORED QUERY (if you are using Access).
In other words, you *COULD* achieve the same results (using SQL Server for this example) by doing:
Code:
CREATE VIEW someAlias
AS
SELECT x,y,z FROM sometable
and then
SELECT ...
FROM someAlias
WHERE ...
But the problem with using a VIEW is that you can't add a "dynamic condition" to it. So in your case, you certainly *COULD* create the VIEW:
Code:
CREATE VEIW G
AS
SELECT Date. SUM(Qty) AS sQTY, SUM(Price) AS sPRICE, SUM(Qty*DGCost) AS sQCOST
FROM GroupedDetails
GROUP BY Date
WHERE Date between '12/01/2008' AND '12/31/2008'
But what happens next month, when you want to change the date range to, say, '1/1/2009' AND '1/31/2009' ???
There are actually two answers to that.
(1) Don't put the date range into the VIEW:
Code:
CREATE VEIW G
AS
SELECT Date. SUM(Qty) AS sQTY, SUM(Price) AS sPRICE, SUM(Qty*DGCost) AS sQCOST
FROM GroupedDetails
GROUP BY Date
And then, when you use the VIEW, limit it:
Code:
SELECT ...
FROM G WHERE G.Date BETWEEN '1/1/2009' AND '1/32/2009'
You might think that the disadvantage in doing this is that the VIEW has to process all the records in GroupedDetails before it can then be used in the outer SELECT and limited to only certain dates. But SQL Server is smart enough that it will optimize your *entire* query. And this is why VIEWs are, often, better than #TEMP tables.
(2) But the other solution is to do as I did in my answer: Don't bother creating stored VIEWs but just create the equivalents "in line" in the overall query.
And, truly, the results in terms of both data and performance should be identical (or damned near so...maybe minor differences in compile time...dunno).
So now, to get more particular:
Quote:
|
In particular I don't understand the FROM [query]. Does that just do a select on the data set in the query as if you were creating a table? And conceptually, how does that help in this case?
|
There's no real logical difference between a table and a VIEW. And, as I've just said, there's no real logical difference between a VIEW and an inner SELECT, such as I used.
Yes, we *could* have accomplished the same thing by doing:
Code:
SELECT Date. SUM(Qty) AS sQTY, SUM(Price) AS sPRICE, SUM(Qty*DGCost) AS sQCOST
INTO #TEMPG
FROM GroupedDetails
GROUP BY Date
WHERE Date between '12/01/2008' AND '12/31/2008'
and then
SELECT Date, SUM(ABS(Qty)*PosNegFlag) AS sQTY, SUM(ABS(Price)*PosNegFlag) AS sPRICE,
SUM(ABS(Qty)*PosNegFlag*DGCost) AS sQCOST
INTO #TEMPT
FROM TransactionDetails
GROUP BY Date
WHERE Date between '12/01/2008' AND '12/31/2008'
and finally
SELECT G.Date, G.sQTY, G.sPRICE, G.sQCOST, T.sQTY, T.sPRICE, T.sQCOST
FROM AS #TEMPG AS G, #TEMPT AS T
WHERE G.Date = T.Date
AND ( G.sQTY<>T.sQTY OR G.sPRICE<>T.sPRICE OR G.sQCOST<>T.sQCOST )
ORDER BY G.Date
And who knows? Maybe SQL Server would even be smart enough to make this version as efficient as the others (though I tend to doubt it).
So... For *MANY* practical purposes you can treat a VIEW much the same as a temporary table. And you can treat an inner SELECT much the same as a VIEW.
There are things you can do with temporary tables that you can't do with VIEWs (primarily, you can build them up "in pieces" instead of all in one step). And there are things you can do with VIEWs that you can't do with inner SELECTs (primarily, you can create them ahead of time and even limit clients to using only STORED PROCs and VIEWs and disallowing ad hoc queries). But for a query such as this one (which really isn't very complicated, as SQL Server queries go), there's little to recommend one route over the other. Perhaps the primary benefit of the version I show is that it *CAN* be all done as an ad hoc query, with no need to "save" something in the DB (that is, even VIEWs must be created and saved in the DB). Many SQL purists would argue that this perceived "benefit" is actually the biggest flaw, just because it *does* use an ad hoc query. Me, I'm pragmatic: Use what works. When it's time to worry about security, *then* encapsulate the whole thing in a very protective STORED PROCEDURE.
Does that help, at all?