Help on basic SQL View query

scoveg13

New Member
Joined
Dec 26, 2014
Messages
25
Hello,

I'm trying to compare two columns based on # of lives and I want to select the greater of the two and have the result be placed in a new column.

Here's my current query:

SELECT [id], [account], [# of full time equivalents], [number of eligible employees]
FROM MaxLivesVol
Where [# of full time equivalents] > [number of eligible employees] or [# of full time equivalents] < [number of eligible employees]

Pulling the data is fine, it's just getting the result of the where clause as a standalone column - something like "MaxLives" is the issue,
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It would have to go in the select clause to be in the results as a column. The query is not clear and almost looks to be self-contradictory. Perhaps provide an example and explain better what you want to see.
 
Upvote 0
It would have to go in the select clause to be in the results as a column. The query is not clear and almost looks to be self-contradictory. Perhaps provide an example and explain better what you want to see.

Basically I'm tryng to compare two columns and have the result in a separate column
 
Last edited:
Upvote 0
By way of example, this would tell you which is greater and show the result in a third column:
Code:
SELECT 
    [id], 
    [account], 
    [# of full time equivalents],
    [number of eligible employees],
    [# of full time equivalents]>[number of eligible employees] as ComparedValues
FROM MaxLivesVol

in MSAccess a -1 means true and a 0 means false, so your results are -1 for true and 0 for false.
 
Upvote 0
By way of example, this would tell you which is greater and show the result in a third column:
Code:
SELECT 
    [id], 
    [account], 
    [# of full time equivalents],
    [number of eligible employees],
    [# of full time equivalents]>[number of eligible employees] as ComparedValues
FROM MaxLivesVol

in MSAccess a -1 means true and a 0 means false, so your results are -1 for true and 0 for false.

This is helpful but I need the actual # not just True/False
 
Upvote 0
What number do you want to see?
 
Upvote 0
the greater of the two columns - #of full time equivalents and number of eligible employees.

if # of full time equivalents is 10 and the number of eligible employees is 12 for Company ABC, I need 12 to show up in a new column - "Compared Values" column.

if # of full time equivalents is 50 and the number of eligible employees is 45 for Company ABC, I need 50 to show up in the "Compared Values" column as well.
 
Upvote 0
In this case you can try:
Code:
SELECT 
    [id], 
    [account], 
    [# of full time equivalents],
    [number of eligible employees],
	switch(
		[# of full time equivalents]>[number of eligible employees],[# of full time equivalents],
		[# of full time equivalents]<[number of eligible employees],[number of eligible employees],
		1=1,-1
		)
FROM MaxLivesVol

You can remove the line breaks in the switch function, though.

There are two cases you did not specify what you want so the function returns -1 for both of these:
1) if the two values are equal
2) if one or both of the values is NULL.
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,126
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top