473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting database fields (rows) to appear as columns in datagrid

I'm looking at a quick way to get results that are displayed as rows to
display as columns.

I have three tables:-
- The Questions for the survey
- The Results of the survey (Columns are listed as question numbers)
- The Survey Extra Results (As additional questions can be listed into
the Questions table, has 3 columns (a link/id to the survey id, a
link/id to the question id, and the answer the user gave).

I need to list the results of the survey and the extra results for the
survey on one row in a datagrid for each survey.

Any ideas?

Aug 6 '06 #1
4 3052
I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it will
start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys("Attendee")
For j = 0 To dsSurveys.Tables(0).Rows.Count - 1

SelectQuery = ""
dsQuestions = GetAllQuestionNumbers("Attendees")
If dsQuestions.Tables.Count <0 Then
If dsQuestions.Tables(0).Rows.Count <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tables(0).Rows.Count - 1
If
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0).T oString) <"" Then
SelectQuery +=
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0).T oString) & " AS [" &
dsQuestions.Tables(0).Rows(i).Item(0).ToString & EndOfString(i,
CInt(dsQuestions.Tables(0).Rows.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.SurveyExtra_Answer FROM SurveyExtra WHERE SurveyExtra_Guid
= '{" &
dsSurveys.Tables(0).Rows(j).Item("Survey_Additiona lQS").ToString & "}'
AND SurveyExtra_QuestionGuid = '{" &
dsQuestions.Tables(0).Rows(i).Item("Question_GuID" ).ToString & "}') AS
[" & dsQuestions.Tables(0).Rows(i).Item(0).ToString & "] "
If Not i =
(CInt(dsQuestions.Tables(0).Rows.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If

Query += "SELECT Survey.Survey_Id AS [Survey_Id],
Survey.Survey_GuID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_Id = " & dsSurveys.Tables(0).Rows(j).Item(0) & "
AND Survey_Type = 'Attendee'"

If j = (dsSurveys.Tables(0).Rows.Count - 1) Then
Query += ";"
Else
Query += " UNION "
End If

Next

Aug 7 '06 #2
Hello dallasfreeman,

kinda sounds like what you want is a crosstab query.

-Boo
I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it will
start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys("Attendee")
For j = 0 To dsSurveys.Tables(0).Rows.Count - 1
SelectQuery = ""
dsQuestions = GetAllQuestionNumbers("Attendees")
If dsQuestions.Tables.Count <0 Then
If dsQuestions.Tables(0).Rows.Count <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tables(0).Rows.Count - 1
If
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0).T oString) <"" Then
SelectQuery +=
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0).T oString) & " AS [" &
dsQuestions.Tables(0).Rows(i).Item(0).ToString & EndOfString(i,
CInt(dsQuestions.Tables(0).Rows.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.SurveyExtra_Answer FROM SurveyExtra WHERE SurveyExtra_Guid
= '{" &
dsSurveys.Tables(0).Rows(j).Item("Survey_Additiona lQS").ToString & "}'
AND SurveyExtra_QuestionGuid = '{" &
dsQuestions.Tables(0).Rows(i).Item("Question_GuID" ).ToString & "}') AS
[" & dsQuestions.Tables(0).Rows(i).Item(0).ToString & "] "
If Not i =
(CInt(dsQuestions.Tables(0).Rows.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If
Query += "SELECT Survey.Survey_Id AS [Survey_Id],
Survey.Survey_GuID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_Id = " & dsSurveys.Tables(0).Rows(j).Item(0) & "
AND Survey_Type = 'Attendee'"

If j = (dsSurveys.Tables(0).Rows.Count - 1) Then
Query += ";"
Else
Query += " UNION "
End If
Next

Aug 7 '06 #3
Thanks for your help

That's a function within Access though, this query has to be a straight
SQL.

or does the Crosstab Query eventually show the SQL behind it

By the way, the number of columns can differ each time, so the query
has to be able to handle x number of additional questions


GhostInAK wrote:
Hello dallasfreeman,

kinda sounds like what you want is a crosstab query.

-Boo
I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it will
start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys("Attendee")
For j = 0 To dsSurveys.Tables(0).Rows.Count - 1
SelectQuery = ""
dsQuestions = GetAllQuestionNumbers("Attendees")
If dsQuestions.Tables.Count <0 Then
If dsQuestions.Tables(0).Rows.Count <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tables(0).Rows.Count - 1
If
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0).T oString) <"" Then
SelectQuery +=
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0).T oString) & " AS [" &
dsQuestions.Tables(0).Rows(i).Item(0).ToString & EndOfString(i,
CInt(dsQuestions.Tables(0).Rows.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.SurveyExtra_Answer FROM SurveyExtra WHERE SurveyExtra_Guid
= '{" &
dsSurveys.Tables(0).Rows(j).Item("Survey_Additiona lQS").ToString & "}'
AND SurveyExtra_QuestionGuid = '{" &
dsQuestions.Tables(0).Rows(i).Item("Question_GuID" ).ToString & "}') AS
[" & dsQuestions.Tables(0).Rows(i).Item(0).ToString & "] "
If Not i =
(CInt(dsQuestions.Tables(0).Rows.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If
Query += "SELECT Survey.Survey_Id AS [Survey_Id],
Survey.Survey_GuID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_Id = " & dsSurveys.Tables(0).Rows(j).Item(0) & "
AND Survey_Type = 'Attendee'"

If j = (dsSurveys.Tables(0).Rows.Count - 1) Then
Query += ";"
Else
Query += " UNION "
End If
Next
Aug 11 '06 #4
Hello dallasfreeman,

SQL Server 2005 supports the PIVOT and UNPIVOT keywords for crasstab queries.

-Boo
Thanks for your help

That's a function within Access though, this query has to be a
straight SQL.

or does the Crosstab Query eventually show the SQL behind it

By the way, the number of columns can differ each time, so the query
has to be able to handle x number of additional questions

GhostInAK wrote:
>Hello dallasfreeman,

kinda sounds like what you want is a crosstab query.

-Boo
>>I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it
will start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys("Attendee")
For j = 0 To dsSurveys.Tables(0).Rows.Count - 1
SelectQuery = ""
dsQuestions = GetAllQuestionNumbers("Attendees")
If dsQuestions.Tables.Count <0 Then
If dsQuestions.Tables(0).Rows.Count <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tables(0).Rows.Count - 1
If
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0) .ToString) <""
Then
SelectQuery +=
GetColumns(dsQuestions.Tables(0).Rows(i).Item(0) .ToString) & " AS ["
&
dsQuestions.Tables(0).Rows(i).Item(0).ToString & EndOfString(i,
CInt(dsQuestions.Tables(0).Rows.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.SurveyExtra_Answer FROM SurveyExtra WHERE
SurveyExtra_Guid
= '{" &
dsSurveys.Tables(0).Rows(j).Item("Survey_Additio nalQS").ToString &
"}'
AND SurveyExtra_QuestionGuid = '{" &
dsQuestions.Tables(0).Rows(i).Item("Question_GuI D").ToString & "}')
AS
[" & dsQuestions.Tables(0).Rows(i).Item(0).ToString & "] "
If Not i =
(CInt(dsQuestions.Tables(0).Rows.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If
Query += "SELECT Survey.Survey_Id AS [Survey_Id],
Survey.Survey_GuID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_Id = " & dsSurveys.Tables(0).Rows(j).Item(0) & "
AND Survey_Type = 'Attendee'"
If j = (dsSurveys.Tables(0).Rows.Count - 1) Then
Query += ";"
Else
Query += " UNION "
End If
Next

Aug 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added...
4
by: sivashankar | last post by:
hello experts.... -i'm having a datagrid with 10 columns and 4 rows and one submit button. -the fields are eno,ename,eid,and 4 check boxes -after entering values when i clik the submit button...
0
by: John Shum | last post by:
I prepare a UserControl that act as a lookup control, once the code is enter in the TextBox, the description will appear in another TextBox outside the UserControl of the same row in a DataGrid via...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
3
by: Frustrated Developer via DotNetMonster.com | last post by:
I have posted a couple times on here already and found the user community to be very helpful. I took on a project before I realized how difficult a time I'm having working with a database....
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
1
by: dallasfreeman | last post by:
I'm looking at a quick way to get results that are displayed as rows to display as columns. I have three tables:- - The Questions for the survey - The Results of the survey (Columns are listed...
3
by: =?Utf-8?B?SmliZXkgSmFjb2I=?= | last post by:
Hi: I have an interesting problem that I don't know how to solve. I need to display rows of one of these grid controls on two columns of the grid. So, row1 would appear on row1, column1, and...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
1
by: tucson | last post by:
I have a gridview that has a blank row with 2 input fields: file to upload, and a description of the file. They click Add and a new row is added, Remove and the row is removed. The problem is:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.