Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Special Grouping

Posted on 2014-02-12
12
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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