Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Special Grouping

Posted on 2014-02-12
12
Medium Priority
?
154 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 1000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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
 
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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

824 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