Solved

Special Grouping

Posted on 2014-02-12
12
143 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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