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.
Author |
Topic |
vgunas1
Starting Member
18 Posts |
Posted - 2003-05-23 : 11:23:01
|
The following are the fields that I have in my table1.ProjectNo. ProjectName ProjectLeadIT02 IT transfer Louise AlcottEN82 Marketing Design Chris WebberCS46 HR Online Jane AustenEN56 Marketing Services Emily BronteIT53 IT website Elizabeth RyanIT847 Intranet Zenith MayCS01 New Hires Meg HaydenI would like to know of a query or a macro that arranges the records in the following manner.ProjectNo. ProjectName ProjectLeadIT02 IT transfer Louise AlcottIT53 IT website Elizabeth RyanCS01 New Hires Meg HaydenCS46 HR Online Jane AustenEN56 Marketing Services Emily BronteEN82 Marketing Design Chris Webberi.e I want a two empty records after each category. Can this be done in Access? If not i would like to know if there is any way to this using a macro and read it in the above format into an excel spreadsheet.Please do let me know!Thanks! |
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2003-05-23 : 14:01:58
|
Why would you want empty rows in a database????If you are trying to just get them to display I suggest that you use the Group by function of Access Report. Note: you may need to add an extra column and update it with the proper part of the string to get your groups.JimUsers <> Logic |
 |
|
vgunas1
Starting Member
18 Posts |
Posted - 2003-05-23 : 17:11:04
|
I want the empty rows because that is the specification. I already bunched up the same type of project numbers together. All I want is empty rows between each type.Thanks! |
 |
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-05-23 : 18:20:31
|
If you're outputting this to excel via vba you can do something like the following:You'll have to read up on create an excel object and creating a recordset in ado.Dim i,j As IntegerDim strType As Stringi=0strType = ""While Not rs.EOF If (i> 0 and strType <> rs.Fields.Item(1) ) Then i = i + 1 Else For j=0 To rs.Fields.Count-1 With objExcel .Cells(i,j).FormulaR1C1 = rs.Fields.Item(j) End With Next strType = rs.Fields.Item(1) rs.MoveNext i=i+1WendAlternatively you can try to create a set of records in a table or query that have the following data.Group OrderIT 1CS 2EN 3Then you could have the following SQLDECLARE @Table1 TABLE (ProjectNo NVARCHAR(10), ProjectName NVARCHAR(25), ProjectLead NVARCHAR(25))INSERT INTO @Table1 (ProjectNo, ProjectName, ProjectLead)SELECT 'IT02','IT transfer','Louise Alcott' UNION ALLSELECT 'EN82','Marketing Design','Chris Webber' UNION ALL SELECT 'CS46','HR Online Jane','Austen' UNION ALL SELECT 'EN56','Marketing Services','Emily Bronte' UNION ALL SELECT 'IT53','IT website','Elizabeth Ryan' UNION ALL SELECT 'IT847','Intranet','Zenith May' UNION ALL SELECT 'CS01','New Hires','Meg Hayden'DECLARE @GroupingTable1 TABLE (GroupingCode NVARCHAR(2), GroupingOrder INTEGER)INSERT INTO @GroupingTable1 (GroupingCode, GroupingOrder)SELECT 'IT', 1 UNION ALLSELECT 'CS', 2 UNION ALLSELECT 'EN', 3 SELECT AA.ProjectNo, AA.ProjectName, AA.ProjectLeadFROM(SELECT *FROM @Table1 A INNER JOIN @GroupingTable1 B ON LEFT(A.ProjectNo,2) = B.GroupingCodeUNION ALLSELECT NULL, NULL, NULL, C.GroupingCode, C.GroupingOrderFROM @GroupingTable1 CUNION ALLSELECT NULL, NULL, NULL, C.GroupingCode, C.GroupingOrderFROM @GroupingTable1 C) AAORDER BY GroupingOrder, CASE WHEN ProjectNo IS NULL THEN 1 ELSE 0 END, ProjectNo, ProjectName, ProjectLead Results:-------------------------------------IT02 IT transfer Louise AlcottIT53 IT website Elizabeth RyanIT847 Intranet Zenith MayNULL NULL NULLNULL NULL NULLCS01 New Hires Meg HaydenCS46 HR Online Jane AustenNULL NULL NULLNULL NULL NULLEN56 Marketing Services Emily BronteEN82 Marketing Design Chris WebberNULL NULL NULLNULL NULL NULLAlso I notice that you use names in the field projectleadit's a bad idea because think about if you ever had to havehierarchies or two people with same name or mispellings (bad,bad)Use the id from the employees table instead.If you have time you may want to post the design of your database and let us help your build a better design.Edited by - ValterBorges on 05/23/2003 18:21:43Edited by - ValterBorges on 05/23/2003 18:24:00 |
 |
|
vgunas1
Starting Member
18 Posts |
Posted - 2003-05-28 : 15:33:19
|
We have an excel sheet with the following fields:Project Number Project Name Project Status Overall Project Manager Project Phase Project Type Purpose of Project Customer Proposed Start Date Approved Start Date Proposed End Date Approved End Date Project Sponsor Executive Sponsor IT Project Manager Business Project Manager Project Champion Project Classification Program Status Comments Budget Review Category Description of PurposeWe export the fields from excel into an access database. We sort the various records based on certain criteria and then export the results back to excel and run macros to format the excel sheet. Since the records are all exported from excel into access we cannot change the design of the database.So inserting the space between the records can be done either in access or in excel.Based on the project number the records will have to be grouped and space has to be inserted between records belonging to different groups.Eg. for project number: AM123 KT121 GS419 AM897 KT589The records will have to be placed in the following manner:AM123AM897GS419KT121KT589Thanks!quote: If you're outputting this to excel via vba you can do something like the following:You'll have to read up on create an excel object and creating a recordset in ado.Dim i,j As IntegerDim strType As Stringi=0strType = ""While Not rs.EOF If (i> 0 and strType <> rs.Fields.Item(1) ) Then i = i + 1 Else For j=0 To rs.Fields.Count-1 With objExcel .Cells(i,j).FormulaR1C1 = rs.Fields.Item(j) End With Next strType = rs.Fields.Item(1) rs.MoveNext i=i+1WendAlternatively you can try to create a set of records in a table or query that have the following data.Group OrderIT 1CS 2EN 3Then you could have the following SQLDECLARE @Table1 TABLE (ProjectNo NVARCHAR(10), ProjectName NVARCHAR(25), ProjectLead NVARCHAR(25))INSERT INTO @Table1 (ProjectNo, ProjectName, ProjectLead)SELECT 'IT02','IT transfer','Louise Alcott' UNION ALLSELECT 'EN82','Marketing Design','Chris Webber' UNION ALL SELECT 'CS46','HR Online Jane','Austen' UNION ALL SELECT 'EN56','Marketing Services','Emily Bronte' UNION ALL SELECT 'IT53','IT website','Elizabeth Ryan' UNION ALL SELECT 'IT847','Intranet','Zenith May' UNION ALL SELECT 'CS01','New Hires','Meg Hayden'DECLARE @GroupingTable1 TABLE (GroupingCode NVARCHAR(2), GroupingOrder INTEGER)INSERT INTO @GroupingTable1 (GroupingCode, GroupingOrder)SELECT 'IT', 1 UNION ALLSELECT 'CS', 2 UNION ALLSELECT 'EN', 3 SELECT AA.ProjectNo, AA.ProjectName, AA.ProjectLeadFROM(SELECT *FROM @Table1 A INNER JOIN @GroupingTable1 B ON LEFT(A.ProjectNo,2) = B.GroupingCodeUNION ALLSELECT NULL, NULL, NULL, C.GroupingCode, C.GroupingOrderFROM @GroupingTable1 CUNION ALLSELECT NULL, NULL, NULL, C.GroupingCode, C.GroupingOrderFROM @GroupingTable1 C) AAORDER BY GroupingOrder, CASE WHEN ProjectNo IS NULL THEN 1 ELSE 0 END, ProjectNo, ProjectName, ProjectLead Results:-------------------------------------IT02 IT transfer Louise AlcottIT53 IT website Elizabeth RyanIT847 Intranet Zenith MayNULL NULL NULLNULL NULL NULLCS01 New Hires Meg HaydenCS46 HR Online Jane AustenNULL NULL NULLNULL NULL NULLEN56 Marketing Services Emily BronteEN82 Marketing Design Chris WebberNULL NULL NULLNULL NULL NULLAlso I notice that you use names in the field projectleadit's a bad idea because think about if you ever had to havehierarchies or two people with same name or mispellings (bad,bad)Use the id from the employees table instead.If you have time you may want to post the design of your database and let us help your build a better design.Edited by - ValterBorges on 05/23/2003 18:21:43Edited by - ValterBorges on 05/23/2003 18:24:00
|
 |
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-05-28 : 23:24:10
|
Both ideas are still ok, however since your using access if you decide to use the second method you might have to use a sub-query, not sure if access allows you to join based on a function like left(). I believe it doesn't.If you need more specifics post some code. |
 |
|
vgunas1
Starting Member
18 Posts |
Posted - 2003-05-29 : 10:05:21
|
I would like to use the query that you had posted. However,I need some clarifications about the query.1)I would like to know exactly how the query works.2) What do you mean by AA, A, B and C in the query?3) Could you please give us the query specific to the fields that we had posted, if possible?Thanks so much. Really appreciated.quote: Both ideas are still ok, however since your using access if you decide to use the second method you might have to use a sub-query, not sure if access allows you to join based on a function like left(). I believe it doesn't.If you need more specifics post some code.
|
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-05-29 : 22:21:02
|
quote: ... and run macros to format the excel sheet ....
Why not have the macro insert blank rows wherever you need them? You should not be forcing queries or databases to alter data returned when the object displaying the data is perfectly capable of formatting it as needed.For example:if the worksheet is sorted by col1, and between each new value in col1 you'd like there to be a blank row, just run something like this (or have your formatting macro call this):Sub AddBlankRows(W as worksheet)Dim i As IntegerDim lastval As Stringi = 1For i = 1 To W.UsedRange.Rows.Count If lastval <> W.Cells(i, 1) Then W.Cells(i, 1).EntireRow.Insert i = i + 1 End If lastval = W.Cells(i, 1)NextEnd SubIf you are already doing some work to format the data in Excel, finish up the job in Excel and add those blank lines. - Jeff |
 |
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-05-30 : 00:19:10
|
VBA is definitely the way to go but let me explain the query.SELECT *FROM @Table1 A INNER JOIN @GroupingTable1 B ON LEFT(A.ProjectNo,2) = B.GroupingCodeCreates rows like the following'IT02','IT transfer','Louise Alcott','IT', 1SELECT NULL, NULL, NULL, C.GroupingCode, C.GroupingOrderFROM @GroupingTable1 CCreates rows like the following notice there are two of these.NULL, NULL, NULL, 'IT', 1THE UNION combines these set to give something like'IT02','IT transfer','Louise Alcott','IT', 1NULL, NULL, NULL, 'IT', 1NULL, NULL, NULL, 'IT', 1So this is what the inner portion returns.The A, AA, BB, B those are aliases that are used to keep the table names short when you want to refer to them.Then there is the order byORDER BY GroupingOrder, CASE WHEN ProjectNo IS NULL THEN 1 ELSE 0 END, ProjectNo, ProjectName, ProjectLeadfirst I want to order the records based on their order in the GroupingTable, next by put the projects first and nulls second that's what the case statement does, then by projectno, projectname and project lead. |
 |
|
|
|
|
|
|