I have tried using the documentation but can not figure this one out
so I am requesting some expert advice:
First I will explain what I want, then show you how I got it using MS
Access and then someone, hopefully, can steer in the right
direction...that is building a functional SQL statement
I have a table called tblTally structured as follows:
Explanation:
ta_tr, ta_pt, ta_st with ta_tr unique
here is how a few sample records are stored:
1,1, 909
2,16,121
3,16,121
4,4,909
5,8,121
6,8,121
7,12,607
8,7,607
9,11,121
For the results column 1 is a non-factor.
What I need is an SQL statement that returns the following results:
Count of unique ta_pt where the ta_st is grouped
For this example I would want the following returned:
count of ta_pt = 3 where ta_st = 121
count of ta_pt = 2 where ta_st = 909
count of ta_pt = 2 where ta_st = 607
The query I came up with gives me repeated ta_pt thus my counts are
too high. It does not matter how many times ta_pt is repeated. I only
want it to count once for each unique instance. Further it is
impossible for the same ta_pt to have different ta_st.
For example this is not possible:
2,16,909
3,16,121
This is possible:
2,16,121
3,16,121
all cases of ta_pt = 16 will have ta_pt = 121
Please keep in mind that I need the whole table summarized at once. I
am not trying to extract an individual count. I want them all in my
recordset so from the above example I would get 3 records in my
recordset from the SQL query.
Now here is how I did it using MS Access:
Step one:
created a query as follows:
SELECT tblTally.ta_point, tblTally.ta_stand FROM tblTally GROUP BY
tblTally.ta_point, tblTally.ta_stand
Step Two:
saved it as qryTally
Step Three:
created a new query as follows:
SELECT Count(qryTally.ta_point) AS CountOfta_point, qryTally.ta_stand
FROM qryTally GROUP BY qryTally.ta_stand ORDER BY qryTally.ta_stand
Now with access it is easy for me to save a query and reference it. I
know that stuff inside out. I do not know how to do the same procedure
with MS SQL so any insight is appreciated.
The Mad Ape
www.tatumba.com
The Mad Ape - 25 Jan 2008 14:55 GMT
BTW I am programming in VB.Net from Visual Studio 2005 and this is for
a desktop app.
> I have tried using the documentation but can not figure this one out
> so I am requesting some expert advice:
[quoted text clipped - 76 lines]
>
> The Mad Apewww.tatumba.com
The Mad Ape - 25 Jan 2008 16:13 GMT
Okay here is what I did to solve this. What I did was put 1st round of
grouped data into a new table created on the fly and perform a count
and group on that new temp table. If anyone can offer this ole dog
some advice or critique of this code I would appreciate.
My end goal was to update some values in a listview.
There may be an easier and more elegant way but at least this works.
'need to update point count
Dim sqlTabExist As New SqlCeCommand("SELECT * FROM
INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'tblPtSum' AND TABLE_TYPE
= 'TABLE'", saveconn)
Dim readerTabExist As SqlCeDataReader =
sqlTabExist.ExecuteReader()
Dim boolTabExist As Boolean = False
While readerTabExist.Read
boolTabExist = True
End While
readerTabExist.Close()
readerTabExist.Dispose()
readerTabExist = Nothing
If boolTabExist = True Then
Dim sqlTabDelete As SqlCeCommand = saveconn.CreateCommand
sqlTabDelete.CommandText = "DELETE FROM tblPtSum"
sqlTabDelete.ExecuteNonQuery()
sqlTabDelete.Dispose()
sqlTabDelete = Nothing
End If
If boolTabExist = False Then
Dim sqlTabCreate As SqlCeCommand = saveconn.CreateCommand
sqlTabCreate.CommandText = "CREATE TABLE tblPtSum(PtTot
NVARCHAR(100) NULL, Stand NVARCHAR(100) NULL)"
sqlTabCreate.ExecuteNonQuery()
sqlTabCreate.Dispose()
sqlTabCreate = Nothing
End If
Dim sqlPtCt As New SqlCeCommand("SELECT ta_point, ta_stand
FROM tblTally GROUP BY ta_point, ta_stand ORDER BY ta_stand",
saveconn)
Dim readerPtCt As SqlCeDataReader = sqlPtCt.ExecuteReader()
While readerPtCt.Read
Dim sqlInsertRow As SqlCeCommand =
saveconn.CreateCommand()
Dim sqlPtCtAdd As String = "INSERT INTO
tblPtSum(PtTot,Stand) VALUES ('" &
readerPtCt.Item("ta_point").ToString & "', '" &
readerPtCt.Item("ta_stand").ToString & "')"
sqlInsertRow.CommandText = sqlPtCtAdd.ToString()
sqlInsertRow.ExecuteNonQuery()
sqlInsertRow.Dispose()
sqlInsertRow = Nothing
End While
readerPtCt.Close()
readerPtCt.Dispose()
readerPtCt = Nothing
sqlPtCt.Dispose()
sqlPtCt = Nothing
'now add grouped contents to listview
Dim sqlPtAdd As New SqlCeCommand("SELECT Count(PtTot) As
TotCt, Stand FROM tblPtSum GROUP BY Stand ORDER BY Stand", saveconn)
Dim readerPtAdd As SqlCeDataReader = sqlPtAdd.ExecuteReader()
While readerPtAdd.Read
Dim item As ListViewItem
For Each item In lvStands.Items
If item.Text = readerPtAdd.Item("Stand").ToString Then
item.SubItems(3).Text =
readerPtAdd.Item("TotCt").ToString
End If
Next
End While
readerPtAdd.Close()
readerPtAdd.Dispose()
readerPtAdd = Nothing
sqlPtAdd.Dispose()
sqlPtAdd = Nothing
> I have tried using the documentation but can not figure this one out
> so I am requesting some expert advice:
[quoted text clipped - 76 lines]
>
> The Mad Apewww.tatumba.com