This is a sql server question although I am using it within Visual BasicOne of the Visual Basic apps that I developed tracks number of service calls within a 60 day period by date. I create a table containing 60 rows with a date and service call count field in each row. I run a query that counts the number of service calls by date and order by count number in descending and date in ascending order so the date with the most calls is the first set of numbers followed in order by count and date. In order to show this in one screen, i have created a single table that has 3 sets of two columns with 20 rows each, the first set of columns is the date with the most counts, count 1 through 20, then the second set of numbers show the 21st date through 40, count,then 41st date through date 60 with count. I do this so I can show the data on one screen without scrolling as shown below. Date Count Date Count Date Count02/05/16 20 02/01/16 12 01/08/16 601/31/16 18 02/22/16 12 02/10/16 601/13/16 17 01/04/16 11 02/18/16 602/03/16 17 01/12/16 11 02/29/16 601/14/16 16 02/26/16 11 01/25/16 501/15/16 16 01/16/16 10 02/02/16 501/26/16 16 01/21/16 10 02/09/16 501/06/16 15 02/04/16 10 02/16/16 501/10/16 15 02/20/16 10 01/07/16 401/27/16 15 02/24/16 10 01/18/16 402/17/16 15 01/22/16 9 02/08/16 402/06/16 14 01/28/16 9 02/28/16 401/03/16 13 02/23/16 9 01/05/16 301/09/16 13 02/25/16 9 02/07/16 301/20/16 13 02/15/16 8 02/27/16 302/14/16 13 01/11/16 7 01/01/16 202/21/16 13 01/29/16 7 01/02/16 201/19/16 12 02/11/16 7 01/24/16 201/23/16 12 02/13/16 7 02/12/16 201/30/16 12 02/19/16 7 01/17/16 1This works, and is extremely fast, but still it is a sledge hammer approach that I use to do this. I create a table with the dates and counts in the correct order. Then I create a table for each set of numbers 1-20, 21-40, and 41 -60. Then I do a join on each of the tables to put the numbers back in one table. I know this is crude and un-wieldy. How can I do this with one table. I tried doing it with unions and joins in the same table, but can't get it to work. I tried to google the solution, but don't have much luck. Is there a simple solution that I am missing? Below is the code:<Code> Dim Con5 As New SqlConnection Dim Cmd5 As New SqlCommand Sql_String(1) = "Drop Table Last_Service_Count;" Sql_String(2) = "Drop Table Last_Service_Count_temp2;" Sql_String(3) = "Drop Table Last_Service_Count_set1;" Sql_String(4) = "Drop Table Last_Service_Count_set2;" Sql_String(5) = "Drop Table Last_Service_Count_set3;" Sql_String(6) = "Select ROW_NUMBER() OVER(ORDER BY Last_Service_Count_draw Desc,Last_Service_Count_Date) As 'Last_Daily_index', Last_Service_Count_Date,Last_Service_Count_Draw into Last_Service_Count_temp2 from Last_Service_Count_temp" Sql_String(7) = "Select ROW_NUMBER() OVER(ORDER BY Last_Service_Count_draw Desc,Last_Service_Count_Date) As 'Last_Daily_index', Last_Service_Count_Date,Last_Service_Count_Draw into Last_Service_Count_Set1 from Last_Service_Count_Temp2 where Last_Daily_index between 1 and 20 " Sql_String(8) = "Select ROW_NUMBER() OVER(ORDER BY Last_Service_Count_draw Desc,Last_Service_Count_Date) As 'Last_Daily_index', Last_Service_Count_Date,Last_Service_Count_Draw into Last_Service_Count_Set2 from Last_Service_Count_Temp2 where Last_Daily_index between 21 and 40 " Sql_String(9) = "Select ROW_NUMBER() OVER(ORDER BY Last_Service_Count_draw Desc,Last_Service_Count_Date) As 'Last_Daily_index', Last_Service_Count_Date,Last_Service_Count_Draw into Last_Service_Count_Set3 from Last_Service_Count_Temp2 where Last_Daily_index between 41 and 60 " Sql_String(10) = "Select a.Last_Daily_index, a.Last_Service_Count_Date As Last_Service_Count_Date1, a.Last_Service_Count_Draw as Last_Service_Count_Draw1, b.Last_Service_Count_Date As Last_Service_Count_Date2, b.Last_Service_Count_Draw as Last_Service_Count_Draw2, c.Last_Service_Count_Date As Last_Service_Count_Date3, c.Last_Service_Count_Draw as Last_Service_Count_Draw3, into Last_Service_Count From Last_Service_Count_Set1 a Left Join Last_Service_Count_Set2 b On a.Last_Daily_index = b.Last_Daily_index Left Join Last_Service_Count_Set3 c On a.Last_Daily_index = c.Last_Daily_index For x = 1 To 110 Try Con5.ConnectionString = "Server=Jocarlap\SQLEXPRESS;Database = Keno;Integrated Security=SSPI;" Con5.Open() Cmd5.Connection = Con5 Cmd5.CommandText = Sql_String(x) Cmd5.ExecuteNonQuery() Catch ex As Exception Finally Con5.Close() End Try Next x <Code/>Thanks for looking at it.
↧