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

x
?
Solved

How can my query be put into six columns by 1 row?

Posted on 2011-03-18
10
Medium Priority
?
242 Views
Last Modified: 2012-05-11
Okay,   The query below gives me the exact output I am looking for except it is single column & single row. Put this exact query into 6 columns, 1 row.   How can this be done?!

SELECT     COUNT(DISTINCT DimInvoiceDetail.Invoice_AK) AS 'Inv #'
FROM         AdHoc.DimInvoiceDetail AS DimInvoiceDetail INNER JOIN AdHoc.DimCustomer AS DimCustomer ON DimInvoiceDetail.Customer_SK = DimCustomer.Customer_SK INNER JOIN
AdHoc.DimCompany AS DimCompany ON DimInvoiceDetail.Company_SK = DimCompany.Company_SK INNER JOIN AdHoc.SalesData AS SalesData ON DimInvoiceDetail.InvoiceDetail_SK = SalesData.InvoiceDetail_SK INNER JOIN AdHoc.DimDate ON DimInvoiceDetail.InvoiceDate_SK = AdHoc.DimDate.Date_SK
WHERE     (DimInvoiceDetail.OrderType NOT IN ('DE', 'DH', 'DJ', 'DK', 'DL', 'DM', 'E2', 'E4', 'W2', 'W4', 'W5', 'W6')) AND (SalesData.GPPercentage <= 0.05) AND (AdHoc.DimDate.Date_AK BETWEEN @StartDate AND @EndDate) AND (DimCompany.CompanyDesc IN (@CompanyName))
0
Comment
Question by:rhservan
10 Comments
 

Author Comment

by:rhservan
ID: 35166511
Below is the desired output structure. Six columns one row.  
 Desired Query Output
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 35166903
What defines what you want to count as "Filter1", Filter2", etc.?

Without knowing how you want to diferentiate the various counts, I can't help you.
0
 

Author Comment

by:rhservan
ID: 35167024
That woulld be column name assignment based on each of the six columns created.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rhservan
ID: 35167343
Actually 8080 like I said in the original frame.  Use the exact query for all six columns,  I just want the six columns and you can name them anything you want. The result example is just that an example showing the structure of the desired output of 6 columns and 1 row.  The count differentiation will be done later after the six columns are created through the query.  All of the filtering I need right now is in the 'where statement' so just use what is there for all six columns.  Hope this is clear and thanks for your assistance.  
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35179663
Basically, what you want can be done in one of 2 ways:

1- Use Pivot/Unpivot. This doesn't work on earlier versions of SQL, or more recent ones but with a lower compatibility setting (lowest it can work is 9.0, I think)

2- Use SUM(CASE). Something like SELECT SUM(CASE WHEN mycondition='Filter1' THEN 1 ELSE 0 END) Filter1, SUM(CASE WHEN mycondition='Filter2' THEN 1 ELSE 0 END) Filter2
etc...
0
 
LVL 18

Expert Comment

by:chrismc
ID: 35182021
As Cluskitt said, "PIVOT" is your best bet and it does work in SQL 2008 and 2008 R2.

Here's the relevant Microsoft reference on how to use it;
http://msdn.microsoft.com/en-us/library/ms177410.aspx

If you are ultimately doing this for a reporting services report, this can be left for the report to do using the matrix options.
0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 35184538
try like this.
;WITH CTE 
     AS (SELECT DimInvoiceDetail.Invoice_AK, 
                DimInvoiceDetail.OrderType, 
                SalesData.GPPercentage, 
                AD.Date_AK, 
                DimCompany.CompanyDesc 
           FROM AdHoc.DimInvoiceDetail AS DimInvoiceDetail 
                INNER JOIN AdHoc.DimCustomer AS DimCustomer 
                  ON DimInvoiceDetail.Customer_SK = DimCustomer.Customer_SK 
                INNER JOIN AdHoc.DimCompany AS DimCompany 
                  ON DimInvoiceDetail.Company_SK = DimCompany.Company_SK 
                INNER JOIN AdHoc.SalesData AS SalesData 
                  ON DimInvoiceDetail.InvoiceDetail_SK = SalesData.InvoiceDetail_SK 
                INNER JOIN AdHoc.DimDate AD 
                  ON DimInvoiceDetail.InvoiceDate_SK = AD.Date_SK), 
     CTEFilter1 
     AS (SELECT 'Filter1'                  AS Filter, 
                COUNT(DISTINCT Invoice_AK) AS 'Inv #' 
           FROM CTE 
          WHERE OrderType NOT IN ('DE','DH','DJ','DK', 
                                  'DL','DM','E2','E4', 
                                  'W2','W4','W5','W6')), 
     CTEFilter2 
     AS (SELECT 'Filter2'                  AS Filter, 
                COUNT(DISTINCT Invoice_AK) AS 'Inv #' 
           FROM CTE 
          WHERE SalesData.GPPercentage <= 0.05), 
     CTEFilter3 
     AS (SELECT 'Filter3'                  AS Filter, 
                COUNT(DISTINCT Invoice_AK) AS 'Inv #' 
           FROM CTE 
          WHERE Date_AK BETWEEN @StartDate AND @EndDate), 
     CTEFilter4 
     AS (SELECT 'Filter4'                  AS Filter, 
                COUNT(DISTINCT Invoice_AK) AS 'Inv #' 
           FROM CTE 
          WHERE CompanyDesc IN (@CompanyName)) 
SELECT c1.[Inv #], 
       c2.[Inv #], 
       c3.[Inv #], 
       c4.[Inv #] 
  FROM CTEFilter1 c1 
       CROSS JOIN CTEFilter2 c2 
       CROSS JOIN CTEFilter3 c3 
       CROSS JOIN CTEFilter4 c4

Open in new window

0
 
LVL 35

Expert Comment

by:James0628
ID: 35187196
Maybe one of the posts above has given you a solution, but if not ...

 I still don't understand what you're trying to do.  As 8080_Diver asked, what defines your columns?  You've got a query that produces a single count.  You apparently want to split that into 6 different counts, but split it based on what?

 Sharath_123 has created separate counts based on the individual conditions in your WHERE, with one count for each condition.  Maybe that's what you were looking for, but you said that you wanted 6 columns, and there are only 4 conditions in your WHERE (or much more than 6, if you do each OrderType test separately).

 James
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

885 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