Solved

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

Posted on 2011-03-18
10
230 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
[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
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
Technology Partners: 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 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