# Beginner SQL help

**URL:** <https://community.goactuary.com/t/beginner-sql-help/1374>\
**Category:** Software/Technology\
**Created:** [December 2, 2020, 4:29pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374 "2020-12-02T16:29:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 4:29pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/1 "2020-12-02T16:29:31Z")

</div>

I have extensive background programming in SAS.  
I’m relatively new to SQL.  
I need lots of help.  
This is where I’ll post all of my requests for help.

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 4:33pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/2 "2020-12-02T16:33:01Z")

</div>

I need a particular value for a particular field to be something else.

In the db I’m reading, I have a bunch of records where LOB = ‘X’. Whenever that happens I need it to be changed to LOB = ‘XX’. There are lots of other LOBs, of course. A, B, C, D, and so on all the way up to X, but whenever it’s ‘X’ I need it to be ‘XX’.

Here’s my code right now:

SELECT [VALUATION\_DATE], [COMPANY], [LOB], Sum(PREMIUM) AS ‘PREMIUM’  
FROM [SOURCE]  
GROUP BY [VALUATION\_DATE], [COMPANY], [LOB]  
ORDER BY [VALUATION\_DATE], [COMPANY], [LOB]

There must be some sort of WHEN clause I throw in there.

---

<div class="post-metadata">

**Author:** ![dr\_t\_non-fan](https://community.goactuary.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dr\_t\_non-fan](https://community.goactuary.com/u/dr_t_non-fan)\
**Post date:** [December 2, 2020, 4:35pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/3 "2020-12-02T16:35:33Z")

</div>

CASE  
WHEN LOB = ‘X’  
THEN ‘XX’  
ELSE LOB  
END  
as NEWLOB

Aside: Looks better when indenting the “When” “then” “Else” lines, but this website ignores leading spaces.  
Some people prefer this all on one line.  
CASE WHEN LOB = ‘X’ THEN ‘XX’ ELSE LOB END as NEWLOB

You’d think that the word “IF” would be used, but “IF” has another use, for declaring variables, as opposed to creating new fields.  
And, yes, you’ll have to repeat the “CASE WHEN” in the GROUP BY area, excluding the “AS NEWLOB” part.

---

<div class="post-metadata">

**Author:** ![SredniVashtar](https://community.goactuary.com/user_avatar/community.goactuary.com/srednivashtar/32/2894_2.png) [@SredniVashtar](https://community.goactuary.com/u/SredniVashtar)\
**Post date:** [December 2, 2020, 4:50pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/4 "2020-12-02T16:50:29Z")

</div>

What the good Dr said.

Should add you can add as many “when… then…” statements as you’d like.

---

<div class="post-metadata">

**Author:** ![dr\_t\_non-fan](https://community.goactuary.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dr\_t\_non-fan](https://community.goactuary.com/u/dr_t_non-fan)\
**Post date:** [December 2, 2020, 4:54pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/5 "2020-12-02T16:54:57Z")

</div>

Yes. CASE WHEN…WHEN…WHEN…(yada yada yada)…ELSE … END as LOBSTERBISQUE

(Edited, for SV.)

---

<div class="post-metadata">

**Author:** ![SredniVashtar](https://community.goactuary.com/user_avatar/community.goactuary.com/srednivashtar/32/2894_2.png) [@SredniVashtar](https://community.goactuary.com/u/SredniVashtar)\
**Post date:** [December 2, 2020, 4:59pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/6 "2020-12-02T16:59:31Z")

</div>

Does SQL allow you to yada yada over sex?

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 6:48pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/7 "2020-12-02T18:48:55Z")

</div>

Thank you for all that.

> [@dr\_t\_non-fan](#):
>
> And, yes, you’ll have to repeat the “CASE WHEN” in the GROUP BY area, excluding the “AS NEWLOB” part.

[rant]I’ve always found this particularly annoying.[/rant]

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:07pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/8 "2020-12-02T19:07:38Z")

</div>

**NEW QUESTION.**

I have a WHERE clause in my statement:

`SELECT... WHERE [COMPANY] = 'ABC' ...GROUP BY...ORDER BY...`

I don’t need to see COMPANY in the summary report but if I don’t include it I’ll get some sort of `'COMPANY' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause` error.

In SAS I could DROP that variable. Is there any thing similar in SQL or am I stuck with that variable like I’m stuck with my in-laws?

---

<div class="post-metadata">

**Author:** ![BruteForce](https://community.goactuary.com/user_avatar/community.goactuary.com/bruteforce/32/175_2.png) [@BruteForce](https://community.goactuary.com/u/BruteForce)\
**Post date:** [December 2, 2020, 7:11pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/9 "2020-12-02T19:11:57Z")

</div>

You shouldn’t need to include Company in your Select clause just because it’s in the Where clause. The error message you posted is referring to Company already being in the Select clause, but not the Group by clause (or an aggregating function).

Remove it from your Select clause and see if that fixes it. Or maybe I’m missing something!

---

<div class="post-metadata">

**Author:** ![ALivelySedative](https://community.goactuary.com/user_avatar/community.goactuary.com/alivelysedative/32/1337_2.png) [@ALivelySedative](https://community.goactuary.com/u/ALivelySedative)\
**Post date:** [December 2, 2020, 7:12pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/10 "2020-12-02T19:12:38Z")

</div>

I got halfway through typing the same thing when this popped up. Agree with BF at first glance.

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:13pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/11 "2020-12-02T19:13:00Z")

</div>

> [@BruteForce](#):
>
> You shouldn’t need to include Company in your Select clause just because it’s in the Where clause. The error message you posted is referring to Company already being in the Select clause, but not the Group by clause (or an aggregating function).
> 
> Remove it from your Select clause and see if that fixes it. Or maybe I’m missing something!

Well, I need to tell it to only use records where [COMPANY] = ‘ABC’.  
How would I do that without it being in the SELECT clause?

Where do I put the WHERE [COMPANY] = ‘ABC’?

---

<div class="post-metadata">

**Author:** ![BruteForce](https://community.goactuary.com/user_avatar/community.goactuary.com/bruteforce/32/175_2.png) [@BruteForce](https://community.goactuary.com/u/BruteForce)\
**Post date:** [December 2, 2020, 7:14pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/12 "2020-12-02T19:14:18Z")

</div>

Just to clarify, your Where clause is after your From clause, right?

SELECT [VALUATION\_DATE], _[COMPANY],_ [LOB], Sum(PREMIUM) AS ‘PREMIUM’  
FROM [SOURCE]  
**WHERE [COMPANY] = ‘X’**  
GROUP BY [VALUATION\_DATE], _[COMPANY],_ [LOB]  
ORDER BY [VALUATION\_DATE], _[COMPANY],_ [LOB]

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:14pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/13 "2020-12-02T19:14:47Z")

</div>

> [@BruteForce](#):
>
> Just to clarify, your Where clause is after your From clause, right?

Yes.

Do I get rid of it from the Select clause & the others and just leave it after FROM ?

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:16pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/14 "2020-12-02T19:16:00Z")

</div>

> [@1695814](#):
>
> Do I get rid of it from the Select clause & the others and just leave it after FROM ?

Yes, that is what I should do.

Thanks, everybody.

---

<div class="post-metadata">

**Author:** ![BruteForce](https://community.goactuary.com/user_avatar/community.goactuary.com/bruteforce/32/175_2.png) [@BruteForce](https://community.goactuary.com/u/BruteForce)\
**Post date:** [December 2, 2020, 7:16pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/15 "2020-12-02T19:16:04Z")

</div>

If by “it” you mean “Company”, then yes. Remove that from everything except your Where clause.

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:16pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/16 "2020-12-02T19:16:20Z")

</div>

:tup:

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:34pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/17 "2020-12-02T19:34:19Z")

</div>

Allow me to vent. This is stupid.

I only want the left 6 characters of the valuation date, so in the SELECT clause I put `left([VALUATION_DATE],6) as [VALDATE]`…but in the GROUP BY & ORDER BY clauses I still need to put `[VALUATION_DATE]`.

That’s ludicrous! Why do I have to rename the thing in the SELECT clause if I don’t even use that name in the GROUP & ORDER BY clauses? (That’s more of a rhetorical question, but if you have an answer for it, I’ll listen.)

---

<div class="post-metadata">

**Author:** ![dr\_t\_non-fan](https://community.goactuary.com/letter_avatar_proxy/v4/letter/d/a8b319/32.png) [@dr\_t\_non-fan](https://community.goactuary.com/u/dr_t_non-fan)\
**Post date:** [December 2, 2020, 7:39pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/18 "2020-12-02T19:39:44Z")

</div>

Simple rule: if some field (new or existing) is in the SELECT, it needs to be in the Group BY, if you are summing or doing any math function to a field.  
Personally, I’d add “left([VALUATION\_DATE],6)” to the Group By clause.  
Otherwise you will get multiple instances of “left([VALUATION\_DATE],6)” in your result, one for each separate instance where “left([VALUATION\_DATE],6)” is the same, but the rest of [VALUATION\_DATE] is different.

---

<div class="post-metadata">

**Author:** ![ALivelySedative](https://community.goactuary.com/user_avatar/community.goactuary.com/alivelysedative/32/1337_2.png) [@ALivelySedative](https://community.goactuary.com/u/ALivelySedative)\
**Post date:** [December 2, 2020, 7:52pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/19 "2020-12-02T19:52:32Z")

</div>

:iatp:

eta: actually approve of both posts. I also use SAS more frequently, and also am very irritated that the alias can’t be used in a subsequent order/group by statement. And also always forget this fact the first time.

---

<div class="post-metadata">

**Author:** ![1695814](https://community.goactuary.com/user_avatar/community.goactuary.com/1695814/32/38506_2.png) [@1695814](https://community.goactuary.com/u/1695814)\
**Post date:** [December 2, 2020, 7:56pm UTC](https://community.goactuary.com/t/beginner-sql-help/1374/20 "2020-12-02T19:56:01Z")

</div>

> [@dr\_t\_non-fan](#):
>
> Simple rule: if some field (new or existing) is in the SELECT, it needs to be in the Group BY, if you are summing or doing any math function to a field.  
> Personally, I’d add “left([VALUATION\_DATE],6)” to the Group By clause.  
> Otherwise you will get multiple instances of “left([VALUATION\_DATE],6)” in your result, one for each separate instance where “left([VALUATION\_DATE],6)” is the same, but the rest of [VALUATION\_DATE] is different.

I understand all of that, but why do I need to **rename** it (e.g., “[VALDATE]”) in the SELECT statement if I never, ever use that new name again? I need to use the original variable name of [VALUATION\_DATE] in the GROUP & ORDER BY clauses.

[Next page](https://community.goactuary.com/t/beginner-sql-help/1374.md?page=2)
