Solved

Special Grouping

Posted on 2014-02-12
12
133 Views
Last Modified: 2014-03-31
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
.....
0
Comment
Question by:Taras
  • 6
  • 5
12 Comments
 
LVL 11

Assisted Solution

by:David Kroll
David Kroll earned 250 total points
ID: 39853224
select t1.customerid, t1.task1, t2.task2, t3.task3, t4.task4
from tblCustomer t1
left outer join tblCustomer t2 on t2.customerid = t1.customerid and (t2.task2 is null or t2.task2 <> '')
left outer join tblCustomer t3 on t3.customerid = t1.customerid and (t3.task3 is null or t3.task3 <> '')
left outer join tblCustomer t4 on t4.customerid = t1.customerid and (t4.task4 is null or t4.task4 <> '')
0
 

Author Comment

by:Taras
ID: 39853336
dkroolCTN.
I got the same result - table something is missing.
I need one row per CustomerID not several.
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 39853345
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
0
 

Author Comment

by:Taras
ID: 39853389
I got error here :
select t2.task2 from tblCustomer t2 on.


 
what is this "on" without join???
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 39853398
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
0
 

Author Comment

by:Taras
ID: 39853630
This query is runign now for 1 hour still no result out ,  I have 10 tasks. I need something faster?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:David Kroll
ID: 39853646
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)
0
 

Author Comment

by:Taras
ID: 39853819
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
0
 

Author Comment

by:Taras
ID: 39853849
It is not properly lined up
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 39854080
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 <> ''
0
 

Author Comment

by:Taras
ID: 39854131
Still not one Row per CustomerID.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 39854294
SELECT
    CustomerID,
    MAX(Task1) AS Task1,
    MAX(Task2) AS Task2,
    MAX(Task3) AS Task3,
    MAX(Task4) AS Task4
FROM tblCustomer
GROUP BY
    CustomerID
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now