Solved

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

Posted on 2011-03-18
10
229 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

713 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