Solved

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

Posted on 2011-03-18
10
234 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

630 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