In the T-sql environment i operate in, i’m unable to create tables. But i can create nested statements ad infinitum, which is what i go with because i have to.
If i had write table permission i could do something like the following
Correct. I have zero write access in any way. I’m not sure where a temp table would be stored. In SAS, and when i used sql server, i could create temp tables, but when i closed the program the temp tables would be deleted. I have no such ability presently.
By temp tables I meant using a # in front of the name you use in the query. That is “stored” only while the query is running. I don’t know all the technical stuff about it (don’t use them myself) so that’s why I asked.
What about declaring variables, can you do that?
I do wonder slightly about why you are using a table for two items, and using two fields for start/end. I will pretend that datetime is important (I only use dates) but that doesn’t really make a difference for what I am proposing.
Are you trying to bucket records into 1q and 2q for instance? Why don’t you use a CASE function in your SELECT statement? Also isn’t the end of one period the start of the next? Use > end dt 1st and <= end dt 2nd to put in 2nd period?
The actual table will have many more quarters. I try to simplify for my examples on line. Then expand what would apply to something small, to something much larger for my actual work purposes. The datetime is important because the field i’m joining on is datetime, and it’s the easiest to just have consistent dataype joins/interactions in my experience.
I would consider using a function to create a “Quarter variable” rather than a join. Q = 4*YEAR(datetime field) + floor((MONTH(datetime field)+2)/3).
It seems easier than creating a table or CASE statement with a lot of parts.
ETA: this approach works best when the end points have a easily defined function, like simple calendar quarter above. One system at my old company was a weekly system, so picking the right business monday/friday was more complicated than the example above.
There are no permissions required to write temp tables, specifically tables written to tempdb. Internally, the server will need the ability to write your queries out to disk if necessary, so it doesn’t make sense to have that restriction.
Try inserting or writing to a table that starts with “#” as mentioned earlier. For example, you can create table or select into a table called #mytemptable
You can get away with using CTEs, but in more complicated situations it can be better to write out to a temp table.