Solved

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

Posted on 2011-03-18
10
223 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
 

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
Backup Your Microsoft Windows Server®

Backup 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.

 
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 40

Accepted Solution

by:
Sharath earned 500 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 34

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now