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 2005 Forums
 Transact-SQL (2005)
 Required a solution

Author  Topic 

kknbrao
Starting Member

2 Posts

Posted - 2009-02-18 : 00:49:31
Hi,

I have one table like follows

Table Name -sourceTable

Column name and datatype like follow
sdate datetime
case varchar(50)
price double

actual data like follow
sdate,case,price
1-jan-2008,a1,10.00
1-jan-2008,a2,20.00
2-jan-2008,b1,30.00
1-jan-2008,b4,40.00
2-jan-2008,b3,15.00
2-jan-2008,a1,12.00
2-jan-2008,b1,13.00

now i divided as some gropus like follows
group1 can contains a1,a2,a3,a4
group2 can contains b1, b2, b3 ,b4


problem
i want records from both groups (group1 & group2). but it should have atleast one record in both groups in a day


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-18 : 02:01:24
[code]SELECT sdate
FROM
(
SELECT *,CASE WHEN [case] in ('a1','a2','a3','a4') then 'group1'
WHEN [case] in ('b1', 'b2', 'b3' ,'b4') then 'group2'
END AS Grouping
FROM sourceTable
)t
GROUP BY sdate
HAVING SUM(case when Grouping = 'Group1' THEN 1 ELSE 0 END) >0
AND SUM(case when Grouping = 'Group2' THEN 1 ELSE 0 END) >0[/code]
Go to Top of Page

kknbrao
Starting Member

2 Posts

Posted - 2009-02-18 : 02:11:19
thanks for your solution but i want like follows

the following records only should produce

2-jan-2008,b1,30.00
2-jan-2008,a1,12.00

every date should contain atleast one record for every group.
Go to Top of Page
   

- Advertisement -