Solved

Special Grouping

Posted on 2014-02-12
12
148 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 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 69

Accepted Solution

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Many to one in one row 2 48
Finding Where Clause Value in SQL Views and SP 21 62
T-SQL: problem comparing datetime 4 83
grouping by date only 6 22
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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