Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 is it possible with GroupBy option?

Author  Topic 

ram.marella
Starting Member

15 Posts

Posted - 2008-06-03 : 15:50:19
All,

I have a problem with SQL query.
my situation is as follows
TABLE A:

Col1 Col2
-------------
Val1 A
Val2 A
Val3 C
Val4 D
Val5 D

In this case i want output like as follows:

I want to do group by on Col2 for specific items like where Col2<>'D' and for rest of the colmns i dont want to do group by.

The final output should be as follows.


Col1 Col2
------------
Val1 A (means min(Col1)when doing group by)
Val3 C
Val4 D
Val5 D


Can any one shed some lights on this and if not possible wih Group by then is there any alternative?

Thanks in advance


Thanks
-----------------
Ram MCP

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-03 : 15:52:59
SELECT MIN(Col1) AS Col1, Col2
FROM YourTable
WHERE Col2 <> 'D'
GROUP BY Col2
UNION
SELECT Col1, Col2
FROM YourTable
WHERE Col2 = 'D'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

ram.marella
Starting Member

15 Posts

Posted - 2008-06-03 : 16:28:35
Thank you so much, in addition to that i have another problem like as follows:

I have to insert the data from TableB to TableA as follows:
TableA:
Col1 Col2 Col3
---------------
TableB
Col1 Col2 Col3
-------------
1 2 false
3 4 true

i have ti use insert abouve two rows into TableA based on the TableB Col3 value. if Col3 TRUE in TableB then insert 1 in tableA, if Col3 in TableB Flase insert 0 in TableA.

Thanks
-----------------
Ram MCP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-04 : 00:18:13
quote:
Originally posted by ram.marella

Thank you so much, in addition to that i have another problem like as follows:

I have to insert the data from TableB to TableA as follows:
TableA:
Col1 Col2 Col3
---------------
TableB
Col1 Col2 Col3
-------------
1 2 false
3 4 true

i have ti use insert abouve two rows into TableA based on the TableB Col3 value. if Col3 TRUE in TableB then insert 1 in tableA, if Col3 in TableB Flase insert 0 in TableA.

Thanks
-----------------
Ram MCP


Have you tried this yourself before asking? Then show us what you've tried till now.A simple query will provide you solution for your problem.
Go to Top of Page
   

- Advertisement -