?
Solved

Crosstab query SQL Server

Posted on 2011-09-23
6
Medium Priority
?
236 Views
Last Modified: 2012-05-12
I have a table:

Referred from                     ImpressionCount                      TimeStamp
--------------------------------------------------------------------------------------------
www.google.com                                      50                      2011-09-17
www.blabla.com                                         2                      2011-09-17
www.xyz.com                                           12                      2011-09-17
www.google.com                                      20                      2011-09-18
www.blabla.com                                         7                      2011-09-18
www.xyz.com                                           13                      2011-09-18
...............
...............
...............


How do I make it a crosstab query that gets the last 7 day's data and results look like

Referred from                                      2011-09-17            2011-09-18            2011-09-19  ...
--------------------------------------------------------------------------------------------------------------------------------------------------------
www.google.com                                          50                               20                           10
www.blabla.com                                             2                                 7                             8
www.xyz.com                                               12                                13                           10

0
Comment
Question by:koossa
[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
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 36585676
For SQL Server 2005 and Above


CREATE TABLE CrossTab
(ReferredFrom VARCHAR(100),
 ImpressionCount INT,
 TStamp DATETIME)
 

DECLARE @SQL VARCHAR(MAX)
  , @Columns VARCHAR(MAX)
            
SET @Columns = STUFF((
                       SELECT DISTINCT
                                ',' + QUOTENAME(CONVERT(VARCHAR, tStamp, 105))
                                AS 'data()'
                       FROM     CrossTab
                     FOR
                       XML PATH('')
                     ), 1, 1, '')
                        
                                    
SET @SQL = N'SELECT ReferredFrom, ' + @Columns + '
FROM
(SELECT  ReferredFrom
      , ImpressionCount
      , CONVERT(VARCHAR,tStamp,105) tStamp
FROM    CrossTab) p
PIVOT
(
SUM(ImpressionCount)
FOR tStamp IN
( ' + @Columns + ' )
) AS pvt
'

EXEC ( @SQL
    )

Ref : Using Pivot and Unpivot

0
 
LVL 15

Expert Comment

by:Anuj
ID: 36585685
0
 

Author Comment

by:koossa
ID: 36586107
Isn't there a way of doing if with a select query?
I want to use it in my software using a select query?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 15

Accepted Solution

by:
Anuj earned 1500 total points
ID: 36586377
This is dynamic Pivot query, you can create a stored procedure and include this code inside the stored procedure, later you can call the stored procedure in your application.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 36587380
You can do it like in the example below by usint a temp table instead:

CREATE TABLE #test_table
        (
        Company  VARCHAR(3),
        Year     SMALLINT,
        Quarter  TINYINT,
        Amount   DECIMAL(2,1),
        Quantity DECIMAL(2,1)
        )
GO
 INSERT INTO #test_table
        (Company,Year, Quarter, Amount, Quantity)
 SELECT 'ABC', 2006, 1, 1.1, 2.2 UNION ALL
 SELECT 'ABC', 2006, 2, 1.2, 2.4 UNION ALL
 SELECT 'ABC', 2006, 3, 1.3, 1.3 UNION ALL
 SELECT 'ABC', 2006, 4, 1.4, 4.2 UNION ALL
 SELECT 'ABC', 2007, 1, 2.1, 2.3 UNION ALL
 SELECT 'ABC', 2007, 2, 2.2, 3.1 UNION ALL
 SELECT 'ABC', 2007, 3, 2.3, 2.1 UNION ALL
 SELECT 'ABC', 2007, 4, 2.4, 1.5 UNION ALL
 SELECT 'ABC', 2008, 1, 1.5, 5.1 UNION ALL
 SELECT 'ABC', 2008, 3, 2.3, 3.3 UNION ALL
 SELECT 'ABC', 2008, 4, 1.9, 4.2 UNION ALL
 SELECT 'XYZ', 2006, 1, 2.1, 3.6 UNION ALL
 SELECT 'XYZ', 2006, 2, 2.2, 1.8 UNION ALL
 SELECT 'XYZ', 2006, 3, 3.3, 2.6 UNION ALL
 SELECT 'XYZ', 2006, 4, 2.4, 3.7 UNION ALL
 SELECT 'XYZ', 2007, 1, 3.1, 1.9 UNION ALL
 SELECT 'XYZ', 2007, 2, 1.2, 1.2 UNION ALL
 SELECT 'XYZ', 2007, 3, 3.3, 4.2 UNION ALL
 SELECT 'XYZ', 2007, 4, 1.4, 4.0 UNION ALL
 SELECT 'XYZ', 2008, 1, 2.5, 3.9 UNION ALL
 SELECT 'XYZ', 2008, 2, 3.5, 2.1 UNION ALL
 SELECT 'XYZ', 2008, 3, 1.3, 3.9 UNION ALL
 SELECT 'XYZ', 2008, 4, 3.9, 3.4
GO


SELECT Company,
        Year,
        SUM(CASE WHEN Quarter = 1 THEN Amount   ELSE 0 END) AS Q1Amt,
        SUM(CASE WHEN Quarter = 1 THEN Quantity ELSE 0 END) AS Q1Qty,
        SUM(CASE WHEN Quarter = 2 THEN Amount   ELSE 0 END) AS Q2Amt,
        SUM(CASE WHEN Quarter = 2 THEN Quantity ELSE 0 END) AS Q2Qty,
        SUM(CASE WHEN Quarter = 3 THEN Amount   ELSE 0 END) AS Q3Amt,
        SUM(CASE WHEN Quarter = 3 THEN Quantity ELSE 0 END) AS Q3Qty,
        SUM(CASE WHEN Quarter = 4 THEN Amount   ELSE 0 END) AS Q4Amt,
        SUM(CASE WHEN Quarter = 4 THEN Quantity ELSE 0 END) AS Q4Qty,
        SUM(Amount)   AS TotalAmt,
        SUM(Quantity) AS TotalQty
   FROM #test_table
  GROUP BY Company, Year
  ORDER BY Company, Year


0
 

Author Closing Comment

by:koossa
ID: 36595378
Thank you
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

752 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