Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Help With SQL Select Query

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

boomers

Established Member
Impact
4
First of all the table and data:

Time Date Name Result

13:00 01 Jan 2010 Name01 Profit
13:30 01 Jan 2010 Name02 Profit
13:30 01 Jan 2010 Name02 Loss
14:30 01 Jan 2010 Name03 Loss

What I want to do is display these results, however for the record that is duplicated (Name02) I only want to show the 'Profit' row.

So the query would end up with the following:

13:00 01 Jan 2010 Name01 Profit
13:30 01 Jan 2010 Name02 Profit
14:30 01 Jan 2010 Name03 Loss

The end result is to be displayed within a DataList (dont know if it can be omitted after the SQL query). Would appreciate any help.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
If it's a word Profit / Loss then this selects all the Profit and all the Loss that aren't profit. There are other ways to do this.. this is one.

Select Time, Date, Name, Result
FROM TABLE
WHERE
RESULT = ‘Profit’
UNION

Select Time, Date, Name, Result
FROM TABLE A
WHERE
Result = ‘Loss’
AND NOT EXISTS
(Select 1
FROM TABLE B
WHERE RESULT=‘Profit’
A.Date = B.Date and
A.Name = B.Name)
 
Last edited:
0
Close
Heres the way I would do it!

Code:
SELECT *
FROM `test2`
GROUP BY name
ORDER BY FIND_IN_SET( result, 'Profit,Loss' )
 
0
Close
Heres the way I would do it!

Code:
SELECT *
FROM `test2`
GROUP BY name
ORDER BY FIND_IN_SET( result, 'Profit,Loss' )

Does that even work?

You have select/group by mismatch.

FIND_IN_SET is a nice function but only in MySQL :)

Code:
SELECT *
FROM
(
SELECT name, SUM(FIND_IN_SET(result, 'profit,loss')) AS P_L
FROM table
WHERE 
GROUP BY name
) AS A
WHERE P_L IN (1,2)

Might work (3 is both)
 
0
Close
I setup a test database with his info and it outputs his required return:

Code:
Edit   	Delete  13:00 	01 Jan 2010 	Name01 	Profit
Edit 	Delete 	13:30 	01 Jan 2010 	Name02 	Profit
Edit 	Delete 	14:30 	01 Jan 2010 	Name03 	Loss

Alternatively if your not using mysql and Profit and Loss are the only two words for the result you could use:

Code:
SELECT *
FROM `test2`
GROUP BY name
ORDER BY result DESC

which will give the same results.
 
1
Close
Code:
SELECT *
FROM `test2`
GROUP BY name
ORDER BY result DESC

which will give the same results.

Only if you run MySQL in non ANSI mode, I suppose.

I'm not sure if I hate or like this but I usually stick with something closer to ANSI because it might need to work in DB2/SQL Server.

But I guess I learned something new today! Rep Pts 4 U
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Member card: see how many threads a member started
The member card now shows a Threads number, right after Posts. The member card is the box that opens when you point at a name. Profile...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back