Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 pulling specific entries in a query

Author  Topic 

SlowlyLearning
Starting Member

3 Posts

Posted - 2009-01-10 : 23:49:28
Query 1
A A1
B A1
B A1
A B1
C B1
B B1
D C1
E C1
A C1

Query 2
A 3 A1 C1
B 2 A1 B1
C 1 B1 B1
D 1 C1 C1
E 1 C1 C1

Query 2 SQL
SELECT [query1].Col1,
Count([query1].col2) AS Count,
First([query1].[code1]) AS [FirstOfCode1],
Last([query1].[code1]) AS [LastOfCode1]
FROM [query 1]
GROUP BY [query 1].Col1;


What I want to do is list each of the column two entries of query 1 that make up the second column of query 2. For A I want to know that it is listed with A1, B1, and C1. I can use the first and last command to get the first and last entry but is there also a second, third, fourth, etc. command that I can use?

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-11 : 00:18:50
hi try this

declare @temp table ( col1 varchar(30), col2 varchar(30))
insert into @temp
select 'A', 'A1' union all
select 'B', 'A1' union all
select 'B', 'A1' union all
select 'A', 'B1' union all
select 'C', 'B1' union all
select 'B', 'B1' union all
select 'D', 'C1' union all
select 'E', 'C1' union all
select 'A', 'C1'


select t.col1,count( distinct t.col2 ) as 'count' , tmin.col2 as firstofcode1,tmax.col2 as lastofcode1
from @temp t
inner join ( select row_number() over ( partition by col1 order by col2 ) as sno ,col1,col2 from @temp ) tmin on tmin.col1= t.col1 and tmin.sno = 1
inner join ( select row_number() over ( partition by col1 order by col2 desc ) as sno ,col1,col2 from @temp ) tmax on tmax.col1= t.col1 and tmax.sno = 1
group by t.col1,tmin.col2,tmax.col2
Go to Top of Page

SlowlyLearning
Starting Member

3 Posts

Posted - 2009-01-11 : 01:37:41
I didn't explain my first post very well and I think there were a few errors, this one is correct.

Table:
Category----Type
---A---------A1
---B---------A1
---B---------A1
---A---------B1
---C---------B1
---B---------B1
---D---------C1
---E---------C1
---A---------C1

Query 1:
Category---Count of Type-----First of Type----Last of Type
---A------------3-----------------A1--------------C1
---B------------3-----------------A1--------------B1
---C------------1-----------------B1--------------B1
---D------------1-----------------C1--------------C1
---E------------1-----------------C1--------------C1

SQL:
SELECT Table1.Category, Count(Table1.Type) AS CountOfType, First(Table1.Type) AS FirstOfType, Last(Table1.Type) AS LastOfType
FROM Table1
GROUP BY Table1.Category;

Everything above is what I currently have set up. However, this is the table that I am trying to create:

Query 1:
Category---Count of Type--First of Type-Second of Type-Last of Type
A--------------3---------------A1-------------B1------------C1
B--------------3---------------A1-------------A1------------B1
C--------------1---------------B1--------------o------------B1
D--------------1---------------C1--------------o------------C1
E--------------1---------------C1--------------o------------C1


Notice the 'second of type' column. In MS Access there is a 'First'(First(Table1.Type) AS FirstOfType) and 'Last' (Last(Table1.Type) AS LastOfType) command but I am looking for a way to create a column with second, third, fourth, etc probably all the way up to 'fifteenth of type' in the actual database I am manipulating. This is just a hypothetical situation, I have a database with 300,000 lines that I am working with so I cannot go through and specificly declare each type and category in a seperate table as Raky suggested in the previous post although I assume this solution would work.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-11 : 01:42:32
quote:
Originally posted by SlowlyLearning

Query 1
A A1
B A1
B A1
A B1
C B1
B B1
D C1
E C1
A C1

Query 2
A 3 A1 C1
B 2 A1 B1
C 1 B1 B1
D 1 C1 C1
E 1 C1 C1

Query 2 SQL
SELECT [query1].Col1,
Count([query1].col2) AS Count,
First([query1].[code1]) AS [FirstOfCode1],
Last([query1].[code1]) AS [LastOfCode1]
FROM [query 1]
GROUP BY [query 1].Col1;


What I want to do is list each of the column two entries of query 1 that make up the second column of query 2. For A I want to know that it is listed with A1, B1, and C1. I can use the first and last command to get the first and last entry but is there also a second, third, fourth, etc. command that I can use?




SELECT Col1,[Count],
MAX(CASE WHEN Seq=1 THEN Col2 ELSE NULL END) AS [FirstOfCode1],
MAX(CASE WHEN BSeq=1 THEN Col2 ELSE NULL END) AS [LastOfCode1]
FROM
(
SELECT [query1].Col1,
[query1].Col2,
COUNT([query1].col2) OVER (PARTITION BY [query1].Col1) AS [Count],
ROW_NUMBER() OVER(PARTITION BY [query1].Col1 ORDER BY [query1].col2) AS Seq,
ROW_NUMBER() OVER(PARTITION BY [query1].Col1 ORDER BY [query1].col2 DESC) AS BSeq
FROM YourTable
)t
GROUP BY Col1,[Count]


this doesnt however gurantee you first and last in terms of order of occurance of table, for that you need a unique valued column to determine order of occurance (either id column or audit column)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-11 : 01:44:38
there's not a concept of first or last in case of sql table. for dtermining order you need another column by which you detertmine what order you're looking at. thats what i told in previous post about presence of id column or audit column like dateadded
Go to Top of Page

SlowlyLearning
Starting Member

3 Posts

Posted - 2009-01-11 : 02:20:24
The actual order doesn't matter to me I just want to know which values in the 'Type' category from table one make up the 'count of type' category from table two. In this case A1 B1 and C1 are the Type entries that make up the Category A. I am using MS Access and in the program you can use the commands first and last to pull up the first and last entries A1 and C1 respectively but I want the ability to pull up each entry in a seperate column as shown in my second post.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-11 : 02:59:51
quote:
Originally posted by SlowlyLearning

The actual order doesn't matter to me I just want to know which values in the 'Type' category from table one make up the 'count of type' category from table two. In this case A1 B1 and C1 are the Type entries that make up the Category A. I am using MS Access and in the program you can use the commands first and last to pull up the first and last entries A1 and C1 respectively but I want the ability to pull up each entry in a seperate column as shown in my second post.


ok. in that case what you can do is to use row_number() to generate sequence and then use sequence number to get the vbarious values. something like

SELECT Col1,[Count],
MAX(CASE WHEN Seq=1 THEN Col2 ELSE NULL END) AS [FirstOfCode1],
MAX(CASE WHEN Seq=2 THEN Col2 ELSE NULL END) AS [SecondOfCode1],
MAX(CASE WHEN Seq=3 THEN Col2 ELSE NULL END) AS [ThirdOfCode1],
MAX(CASE WHEN Seq=4 THEN Col2 ELSE NULL END) AS [FourthOfCode1],
MAX(CASE WHEN Seq=5 THEN Col2 ELSE NULL END) AS [FifthOfCode1],
MAX(CASE WHEN Seq=6 THEN Col2 ELSE NULL END) AS [SixthOfCode1],
....
FROM
(
SELECT [query1].Col1,
[query1].Col2,
COUNT([query1].col2) OVER (PARTITION BY [query1].Col1) AS [Count],
ROW_NUMBER() OVER(PARTITION BY [query1].Col1 ORDER BY [query1].col2) AS Seq
FROM YourTable
)t
GROUP BY Col1,[Count]


the only problem with above code is that you need to be sure of maximum records that can come for a code value. However, if you want to make it dynamic then you need to use dynamic sql like this

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx
Go to Top of Page
   

- Advertisement -