NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Help With SQL Select Query

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 10-02-2010, 01:17 PM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Jun 2005
Posts: 518
boomers has a spectacular aura aboutboomers has a spectacular aura about
 



Help! Help With SQL Select Query


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
????: NamePros.com http://www.namepros.com/programming/680193-help-with-sql-select-query.html
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.
__________________
Free Horse Racing Tips
boomers is offline   Reply With Quote
Old 10-02-2010, 09:13 PM   #2 (permalink)
Senior Member
 
defaultuser's Avatar
Join Date: May 2009
Location: internet@ctivist.com
Posts: 4,790
defaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatness
 



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)
__________________
A Member of: IdeationTeam.com
HowToBeADomainer.com - a Domaining How To
AuthorEditor.com
Last edited by defaultuser; 10-02-2010 at 09:21 PM.
defaultuser is online now   Reply With Quote
Old 10-03-2010, 11:54 AM   #3 (permalink)
NamePros Regular
 
baxter's Avatar
Join Date: Apr 2006
Posts: 360
baxter is just really nicebaxter is just really nicebaxter is just really nicebaxter is just really nice
 


Ethan Allen Fund Save The Children
Heres the way I would do it!

Code:
SELECT *
FROM `test2`
GROUP BY name
ORDER BY FIND_IN_SET( result, 'Profit,Loss' )
__________________
Canadian Domain Registrar Ready.ca
baxter is offline   Reply With Quote
Old 10-03-2010, 12:19 PM   #4 (permalink)
Senior Member
 
defaultuser's Avatar
Join Date: May 2009
Location: internet@ctivist.com
Posts: 4,790
defaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatness
 



Originally Posted by baxter View Post
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)
__________________
A Member of: IdeationTeam.com
HowToBeADomainer.com - a Domaining How To
AuthorEditor.com
defaultuser is online now   Reply With Quote
Old 10-04-2010, 07:03 AM   #5 (permalink)
NamePros Regular
 
baxter's Avatar
Join Date: Apr 2006
Posts: 360
baxter is just really nicebaxter is just really nicebaxter is just really nicebaxter is just really nice
 


Ethan Allen Fund Save The Children
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.
__________________
Canadian Domain Registrar Ready.ca
baxter is offline   Reply With Quote
Old 10-05-2010, 07:14 PM   #6 (permalink)
Senior Member
 
defaultuser's Avatar
Join Date: May 2009
Location: internet@ctivist.com
Posts: 4,790
defaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatnessdefaultuser Has achieved greatness
 



Originally Posted by baxter View Post
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=680193

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
defaultuser is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:38 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger