Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Add a column Count from another table Expand / Collapse
Author
Message
Posted Monday, January 21, 2013 2:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 03, 2013 12:44 PM
Points: 19, Visits: 29
Currently I have this query.

SELECT C.listcode, C.year, COUNT(NamesTest.year) as Mailed
FROM
(
SELECT * FROM
(SELECT DISTINCT listcode FROM NamesTest) as A
CROSS JOIN
(SELECT DISTINCT year FROM NamesTest) as B
) as C
LEFT JOIN NamesTest ON C.listcode = NamesTest.listcode AND C.year = NamesTest.year
Where c.Year > 2007
GROUP BY c.listcode, c.year
Order By c.listcode, c.year

I want to add the count results of this query to it:

SELECT        listcode, year, COUNT(year) AS Sold
FROM forteInfotest
GROUP BY listcode, year

But every time I add a join the count from the forteInfotest table is the same as the other count. Can anyone help me. Thanks in advance!
Post #1409720
Posted Monday, January 21, 2013 3:33 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641, Visits: 8,273
Doesn't sound too tough. Please take a look at the first link in my signature for best practices when posting questions. Once we have the necessary information you will find lots of people around here willing and able to help pretty quickly.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1409732
Posted Tuesday, January 22, 2013 1:20 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 5,618, Visits: 10,990
-- To obtain accurate rowcounts from the two tables, you need to aggregate them separately
;WITH Matrix (listcode,[year]) AS (
(SELECT DISTINCT listcode FROM NamesTest) as A
CROSS JOIN
(SELECT DISTINCT [year] FROM NamesTest WHERE [Year] > 2007) as B
)
SELECT mx.listcode, mx.[year], n.Mailed, f.Sold
FROM Matrix mx
LEFT JOIN ( -- Using COUNT with a column name can be confusing, avoid - unless also specifying DISTINCT
SELECT listcode, [year], COUNT(*) AS Mailed
FROM NamesTest
GROUP BY listcode, [year]
) n
LEFT JOIN (
SELECT listcode, [year], COUNT(*) AS Sold
FROM forteInfotest
GROUP BY listcode, [year]
) f

Without any data to test against (see Sean's post above), this is a best guess.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1409853
Posted Tuesday, January 22, 2013 6:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 03, 2013 12:44 PM
Points: 19, Visits: 29
Ok I did not know I needed to post any data. Here is a little bit:

Listcode Year Mailed
102EL 2008 11488
102EL 2009 11305
102EL 2010 3607
102EL 2011 46
170MD 2008 25657
170MD 2009 0
170MD 2010 15131
170MD 2011 9039

That is the results of the first query. Here is the second:

listcode year Sold
102EL 2006 1
102EL 2007 13
102EL 2008 2
102EL 2009 11
102EL 2010 3
102EL 2011 1

Does that help?
Post #1409998
Posted Tuesday, January 22, 2013 6:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 5,618, Visits: 10,990
ncurran217 (1/22/2013)
Ok I did not know I needed to post any data. ..


No worries, you're new here. Have a read of this article, it will help you formulate some test data.

Have you tried the query I posted above?




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1410002
Posted Tuesday, January 22, 2013 6:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 03, 2013 12:44 PM
Points: 19, Visits: 29
Sorry yes I tried your query you created and it did not work. Gave me three errors on three different lines:

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'as'.
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near 'f'.

I am reading the link of Forum Etiquette and trying to find where to get to the Text Mode of Query Analyzer, to be able to put the sample data in here. Sorry for the headaches.
Post #1410006
Posted Tuesday, January 22, 2013 6:59 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 03, 2013 12:44 PM
Points: 19, Visits: 29
Also, to get the numbers I posted as a result from my query I would have to post data with 100k rows.
Post #1410013
Posted Tuesday, January 22, 2013 7:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 5,618, Visits: 10,990
ncurran217 (1/22/2013)
Also, to get the numbers I posted as a result from my query I would have to post data with 100k rows.


Here's how to create some sample data - and with it a working query

SELECT *
INTO #namesTest
FROM (
SELECT Listcode = '102EL', [Year] = '2008'
FROM (SELECT TOP(11488) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2009'
FROM (SELECT TOP(11305) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2010'
FROM (SELECT TOP(3607) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2011'
FROM (SELECT TOP(46) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '170MD', '2008'
FROM (SELECT TOP(25657) n = 1 from sys.columns a, sys.columns b) d
--SELECT '170MD', '2009' 0
UNION ALL
SELECT '170MD', '2010'
FROM (SELECT TOP(15131) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '170MD', '2011'
FROM (SELECT TOP(9039) n = 1 from sys.columns a, sys.columns b) d
) q
-- (76273 row(s) affected)

SELECT *
INTO #forteInfotest
FROM (
SELECT listcode = '102EL', [year] = '2006'
FROM (SELECT TOP(1) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2007'
FROM (SELECT TOP(13) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2008'
FROM (SELECT TOP(2) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2009'
FROM (SELECT TOP(11) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2010'
FROM (SELECT TOP(3) n = 1 from sys.columns a, sys.columns b) d
UNION ALL
SELECT '102EL', '2011'
FROM (SELECT TOP(1) n = 1 from sys.columns a, sys.columns b) d
) q
-- (31 row(s) affected)


-- query
;WITH Matrix (listcode,[year]) AS
(
SELECT *
FROM
(SELECT DISTINCT listcode FROM #NamesTest) a
CROSS JOIN
(SELECT DISTINCT [year] FROM #NamesTest WHERE [Year] > 2007) b
)
SELECT mx.listcode, mx.[year], n.Mailed, f.Sold
FROM Matrix mx
LEFT JOIN ( -- Using COUNT with a column name can be confusing, avoid - unless also specifying DISTINCT
SELECT listcode, [year], COUNT(*) AS Mailed
FROM #NamesTest
GROUP BY listcode, [year]
) n ON n.Listcode = mx.listcode AND n.year = mx.year
LEFT JOIN (
SELECT listcode, [year], COUNT(*) AS Sold
FROM #forteInfotest
GROUP BY listcode, [year]
) f ON f.Listcode = mx.listcode AND f.year = mx.year
ORDER BY mx.listcode, mx.[year]




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1410020
Posted Tuesday, January 22, 2013 7:23 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 03, 2013 12:44 PM
Points: 19, Visits: 29
Alright, that worked. How did you get the code for the sample data or did you just type that all up? Also, is there something simple to show the Nulls as 0 instead?
Post #1410025
Posted Tuesday, January 22, 2013 7:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 5,618, Visits: 10,990
Have a look at the code I used for the sample data, if there's anything you are unsure of then ask. Note that the sample data tables I created for this are #temp tables, you will need to remove the # from my code for it to work against your tables.

Use ISNULL() to deal with nulls in the output.

Cheers

ChrisM


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1410027
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse