Taras
asked on
Special Grouping
I have a table tblCustomer with next fields and values.
CustomerID Task1 Task2 Task3 Task4
101 Yes
101 Yes
101 Null
101 Null
102 Yes
102 Null
102 Yes
102 Null
103 Yes
103 Null
103 Null
103 Yes
...
I need select statement that will give me result
CustomerID Task1 Task2 Task3 Task4
101 Yes Yes Null Null
102 Yes Null Yes Null
103 Yes Null Null Yes
.....
CustomerID Task1 Task2 Task3 Task4
101 Yes
101 Yes
101 Null
101 Null
102 Yes
102 Null
102 Yes
102 Null
103 Yes
103 Null
103 Null
103 Yes
...
I need select statement that will give me result
CustomerID Task1 Task2 Task3 Task4
101 Yes Yes Null Null
102 Yes Null Yes Null
103 Yes Null Null Yes
.....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select t1.customerid, t1.task1,
(select t2.task2 from tblCustomer t2 on t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 on t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 on t2.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1
(select t2.task2 from tblCustomer t2 on t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 on t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 on t2.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1
ASKER
I got error here :
select t2.task2 from tblCustomer t2 on.
what is this "on" without join???
select t2.task2 from tblCustomer t2 on.
what is this "on" without join???
sorry, those ons should be wheres.
select t1.customerid, t1.task1,
(select t2.task2 from tblCustomer t2 where t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 where t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 where t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1
select t1.customerid, t1.task1,
(select t2.task2 from tblCustomer t2 where t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 where t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 where t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1
ASKER
This query is runign now for 1 hour still no result out , I have 10 tasks. I need something faster?
Make sure you have indexes on each of the task fields and customerid. Also add the (nolock) command below.
select t1.customerid, t1.task1,
(select t2.task2 from tblCustomer t2 (nolock) where t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 (nolock) where t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 (nolock) where t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1 (nolock)
select t1.customerid, t1.task1,
(select t2.task2 from tblCustomer t2 (nolock) where t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 (nolock) where t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 (nolock) where t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1 (nolock)
ASKER
It is still not pulling out one row per CustomerId. Now It is repeating each preset task in all rows for CustomerID except Task1.
Something like this:
CustomerID Task1 Task2 Task3 Task4
101 Yes Yes
101 Yes
101 Yes
101 Yes
102 Yes Yes
102 Yes
102 Yes
102 Yes
Something like this:
CustomerID Task1 Task2 Task3 Task4
101 Yes Yes
101 Yes
101 Yes
101 Yes
102 Yes Yes
102 Yes
102 Yes
102 Yes
ASKER
It is not properly lined up
select t1.customerid, t1.task1,
(select t2.task2 from tblCustomer t2 (nolock) where t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 (nolock) where t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 (nolock) where t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1 (nolock)
where t1.task1 is null or t1.task1 <> ''
(select t2.task2 from tblCustomer t2 (nolock) where t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '') as task2,
(select t3.task3 from tblCustomer t3 (nolock) where t3.customerid = t1.customerid and (t3.task3 is null or t3.task2 <> '') as task3,
(select t4.task2 from tblCustomer t4 (nolock) where t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '') as task4
from tblCustomer t1 (nolock)
where t1.task1 is null or t1.task1 <> ''
ASKER
Still not one Row per CustomerID.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I got the same result - table something is missing.
I need one row per CustomerID not several.