Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to use the following syntax:

 

select machid,sum(iif(pvalue > 0,pvalue,0)) ,sum(iif(pvalue<0,pvalue,0)) from valuetable group by machid

 

esentiallly I have a table that has a field that I need to add all of the . values and all of the neg. values seperately to return two distinct calculations.

 

any ideas - query analyzer returns "Incorrect syntax near '>'."

 

Thanks in Advance

Posted

A tip passed onto to me by this forum a while ago....

 

don't use the query analyzer as it's crap!

 

I have found it to report syntax errors on perfectly good sql.

 

result.......write your sql manually to save a whole lot of grief!

My website
Posted

don't use the query analyzer as it's crap!

 

Silliest thing I've heard in a while..

 

select machid,sum(iif(pvalue > 0,pvalue,0)) ,sum(iif(pvalue<0,pvalue,0)) from valuetable group by machid

 

Strange syntax, does SQL Server support iif? Looks like a VB thing. Try using CASE or just a regular IF statement;

 

SELECT
  machid,
  SUM(CASE WHEN pvalue > 0 THEN pvalue ELSE 0),
  SUM(CASE WHEN pvalue < 0 THEN pvalue ELSE 0)
FROM valuetable
  GROUP BY machid

 

As a side though, have you tried SUM(pvalue > 0), SUM(pvalue < 0)? SUM() accepts an expression, so it might work. It's at least worth a shot as it'd simplify your SQL command quite a bit if it does work.

Gamer extraordinaire. Programmer wannabe.
Posted

 

Silliest thing I've heard in a while..

 

 

Is it me wyrd but you seem to always be on the defensive re anyones comments?

 

I'm only relaying what Derek Stone pointed out to me some time ago which is totally true. The analyer does stuff up simple legit sql simple as that!

 

See my post :

 

This ole SQL chestnut....still :-((

My website
  • *Experts*
Posted

You can use IIF in Access (Jet maybe?).

 

I'd use two subselects, if possible. Try something like:

select v1.machid, sum(v1.pvalue), sum(v2.pvalue) from valuetable v1 INNER JOIN valuetable v2 ON v1.machid = v2.machid group by v1.machid

 

If that doesn't work let me know. I can try to work it out for real.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Is it me wyrd but you seem to always be on the defensive re anyones comments?

 

I wasn't being defensive, I was voicing my opinion. :) I admit maybe I worded it a bit rudely, but it wasn't intended as such.

Of course, if you really want, we could get medieval *busts out shield and sworld* :D

Gamer extraordinaire. Programmer wannabe.
Posted

Nah....no worries wyrd...I think my reply came across rude also.

 

Tell ya what lets forget all about it and remain friends.....howver this doesn't mean I'm preposing :-))))

My website

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...