05-31-2011, 10:06 AM | #1 | |||
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
SQL Help
Is it possible to set a variable within a select statement?
I've got this code: Quote:
It's inside a select statement. I'd like to be able to change the local variables @B and @E, then run this code again, creating another column. Thanks in advance! |
|||
05-31-2011, 10:19 AM | #2 |
Coordinator
Join Date: Nov 2003
|
What is the DBMS you are using?
|
05-31-2011, 10:20 AM | #3 |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
It's called 'huh?'
|
05-31-2011, 10:21 AM | #4 |
Coordinator
Join Date: Nov 2003
|
MS/SQL, MySql, Oracle etc....
It's very doable in PL/SQL if you are running Oracle, otherwise I'm not sure |
05-31-2011, 10:23 AM | #5 |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Oh, I think I can answer that! I use Microsoft SQL Server Management Studio.
|
05-31-2011, 10:23 AM | #6 |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Obviously you're dealing with someone who knows very little about what he's doing here.
|
05-31-2011, 10:24 AM | #7 |
Coordinator
Join Date: Nov 2003
|
ah... then that's a good thing (I think) but not in my area of expertize... hopefully somebody can help you out
|
06-01-2011, 12:17 PM | #8 | |
Pro Starter
Join Date: Oct 2005
Location: Washington, DC
|
Quote:
I'm not sure you're asking the question the right way. 1) What do you mean by "creating another column" as to the first part of the question, I'm going to attempt to answer but I may be answering the wrong question. DECLARE @NewVar DATETIME SELECT @NewVar = column FROM table That's really not that explanatory, but... you can declare a variable and set it that way. How it fits into your entire query (which you did not paste) is hard to say. I'm also not sure that you want your new variable to be set based on a column in a select, or if you just want to do: [original query] SET @B = [new value] If you can give us some more information, that would be helpful.
__________________
Sixteen Colors ANSI/ASCII Art Archive "...the better half of the Moores..." -cthomer5000 |
|
06-01-2011, 01:31 PM | #9 | |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Well, here's my query in entirety now, and this will probably show what I was trying to do, but ended up doing the long way:
Quote:
|
|
06-01-2011, 01:38 PM | #10 | |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
So basically, I have the same block of code except for the dates are changed, 11 times. It wasn't a huge deal to copy and paste the code, then change the dates, but it'd be nice for future programs to be able to run that code, then change the dates.
But now I've got another problem -- when I run this, it errors because Quote:
I tried adding Min(I) as I, Max(P) as P, Max(polannprem) as polannprem, To my second set of select statements, but I got the same error. |
|
06-01-2011, 01:53 PM | #11 |
Coordinator
Join Date: Nov 2003
|
Why do you need the group by in there? Your CASE WHEN statements aren't aggregate functions.
|
06-01-2011, 03:09 PM | #12 |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Well I want to group by those fields. So for each combination of those 6 fields, I want the sum of the EP in each quarter.
|
06-01-2011, 03:13 PM | #13 |
Coordinator
Join Date: Nov 2003
|
In that case, you'd need the SUM() in there around your case statements I would have thought. CASE WHEN is not an aggregate function, so that's why it's giving you the error.
Disclaimer that this isn't my first language... so I may be off base. |
06-01-2011, 03:14 PM | #14 |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Yeah, I was just realizing that, too.
|
06-01-2011, 03:22 PM | #15 |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Okay, I think I have what I want. I have to change all those I's and P's back to the full length name, I think, which I'll do tomorrow. But I think then it will work.
|
06-02-2011, 03:08 PM | #16 | |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
I think I've got it!
Quote:
Last edited by Passacaglia : 06-02-2011 at 03:11 PM. |
|
06-02-2011, 03:13 PM | #17 | |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Then:
Quote:
I probably could have just summed the case statements and not had to do a nested query, but whatever. It'll make people here more impressed by it if it's longer. |
|
06-02-2011, 07:02 PM | #18 |
Head Coach
Join Date: Dec 2009
|
vote pass
|
06-03-2011, 12:47 PM | #19 |
Grizzled Veteran
Join Date: Sep 2003
Location: Fresno, CA
|
|
06-08-2011, 10:19 AM | #20 |
Pro Starter
Join Date: Oct 2005
Location: Washington, DC
|
Sorry, I got swamped.. Looks like you found your solution, but I'll try to take a look at it later today.
__________________
Sixteen Colors ANSI/ASCII Art Archive "...the better half of the Moores..." -cthomer5000 |
06-10-2011, 09:58 AM | #21 | |
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Quote:
Normally, I'd agree. Although, the people who are going to use it know even less SQL than I do. So they'll just open the file, change the dates, and run it, with only a passing glance at the rest of the code. But hey, I came here asking you guys for help on how to make it more efficient, so it's not like I'm intentionally making it longer! |
|
06-10-2011, 10:09 AM | #22 | ||
Coordinator
Join Date: Oct 2000
Location: Big Ten Country
|
Quote:
Thanks! Yeah, I'm not worried too much about changing the variables anymore, since someone who does know more about this stuff is going to make it into a function. But as I'm improving this, I have some more questions. Here's my code now: Quote:
1. So the first query is selecting the same fields from two different tables -- From NA.dbo.Traditional_Active and From NA.dbo.Traditional_Rejected. But in both cases, I'm restricting which rows is takes (the left 3 digits of Plan_Number_DLM are 930, in this case). What I want is for people to be able to easily type in what restrictions they want to put on the data, but I'd like them to be able to do it in one spot (preferably high up near where the dates are set) instead of having to remember to do it in both places. Is this possible? 2. I have this as a nested query because I'm defining "I" and "P" in the first query, then using them in the 2nd query, and that's the only way I know how to do it. Is it possible to use "I" and "P" in my case/when statements that define Exposure and EarnedPremium, without having to make a separate query? Along the same lines, EarnedPremium could be more easily defined as Exposure * Annualized_Premium, but it seemed easier to just recalculate it then to create another query that can use Exposure. Thanks again! |
||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
|
|