473,387 Members | 1,455 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,387 software developers and data experts.

Use TSQL to create a data matrix

I am developing a SQL database to cover operations that were previously
handled in a spreadsheet, and need to create a view or procedure that
presents data into a matrix format similar to what the users are currently
working with. There must be a way I can create this using Transact SQL but
I cant figure it at this point. What the users want is for data to be
presented in 7 continuous columns where each column shows records for 1 day
of a week and each record is presented as a 'block'. Any tips or hints from
people who have achieved something similar would be gratefully accepted.

A sample of the format appears below. (Set with tabs. Hope this
translates)

1/2/2004 2/2/2004 3/2/2004 4/2/2004 5/2/2004
6/2/2004 7/2/2004
Person Smith Jones Green
Room 1A 2B 3c
Start 9:00AM 8:00AM 8:00AM
End 5:00 PM 5:00 PM 5:00 PM

Person Brown White
Room 1D 1D
Start 9:00AM 9:00AM
End 5:00 PM 5:00 PM

Person Green
Room 1M
Start 9:00AM
End 5:00 PM

Notes

1. As the diagram shows, there is likely to be a different number of records
for each day.
2. It is not necessary to have the field headings appear on the left of the
view.


Jul 20 '05 #1
4 7589
What client application are the users using to view this data? The client
application is a much more logical place to do this kind of presentational
stuff. What you have asked for is clearly something formatted for display on
screen or in a report. TSQL just isn't designed for report-writing. Sure,
you could write this in TSQL if you are masochistic enough to want to, but
Access and Visual Studio for example come with much better tools to do it.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
David, thanks for your reply,

Im actually using an Access ADP as the client interface and any output
created would be enhanced in Access for presentation to users. But I cant
think of how to get the base data easily in Access and I am something of an
Access expert. The closest Access has to what I want is a crosstab query,
but this falls well short of the solution I seek. I also experimented with
lining up 7 subforms, each containing records for a seperate day of the
week. This method kind of worked but had significant flaws. My impression
was that Transact SQL had some fancy commands not available in Access to
present data? I could write some ADO code in VBA to assemble the data at
runtime but if I was going to this trouble anyway I would be better off
biting the bullet and writing a procedure. But if you have some ideas for
MS Access I'm all ears!!!

"David Portas" <RE****************************@acm.org> wrote in message
news:45********************@giganews.com...
What client application are the users using to view this data? The client
application is a much more logical place to do this kind of presentational
stuff. What you have asked for is clearly something formatted for display on screen or in a report. TSQL just isn't designed for report-writing. Sure,
you could write this in TSQL if you are masochistic enough to want to, but
Access and Visual Studio for example come with much better tools to do it.

--
David Portas
SQL Server MVP
--

Jul 20 '05 #3
It is a crosstab. You didn't specify any table structure but assuming
you have something that approximates this:

CREATE TABLE YourTable (room INTEGER NOT NULL, person VARCHAR(10) NOT
NULL, starttime DATETIME NOT NULL, endtime DATETIME NOT NULL, PRIMARY
KEY (room,person,starttime))

You could do use the following query. @dt is the starting date of the
week.

SELECT room, person,
MIN(CASE WHEN DATEDIFF(DAY,@dt,starttime)=0 THEN starttime END) AS
start1,
MIN(CASE WHEN DATEDIFF(DAY,@dt,endtime)=0 THEN endtime END) AS end1,
MIN(CASE WHEN DATEDIFF(DAY,@dt,starttime)=1 THEN starttime END) AS
start2,
MIN(CASE WHEN DATEDIFF(DAY,@dt,endtime)=1 THEN endtime END) AS end2,
MIN(CASE WHEN DATEDIFF(DAY,@dt,starttime)=2 THEN starttime END) AS
start3,
MIN(CASE WHEN DATEDIFF(DAY,@dt,endtime)=2 THEN endtime END) AS end3,
.... etc
FROM YourTable
WHERE starttime >= @dt
AND starttime < DATEADD(DAY,7,@dt)
GROUP BY room, person

The rest is just formatting you can do in an Access report.
--
David Portas
SQL Server MVP
--

Jul 20 '05 #4
Many thanks David, I will give this a try.
"David Portas" <RE****************************@acm.org> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
It is a crosstab. You didn't specify any table structure but assuming
you have something that approximates this:

CREATE TABLE YourTable (room INTEGER NOT NULL, person VARCHAR(10) NOT
NULL, starttime DATETIME NOT NULL, endtime DATETIME NOT NULL, PRIMARY
KEY (room,person,starttime))

You could do use the following query. @dt is the starting date of the
week.

SELECT room, person,
MIN(CASE WHEN DATEDIFF(DAY,@dt,starttime)=0 THEN starttime END) AS
start1,
MIN(CASE WHEN DATEDIFF(DAY,@dt,endtime)=0 THEN endtime END) AS end1,
MIN(CASE WHEN DATEDIFF(DAY,@dt,starttime)=1 THEN starttime END) AS
start2,
MIN(CASE WHEN DATEDIFF(DAY,@dt,endtime)=1 THEN endtime END) AS end2,
MIN(CASE WHEN DATEDIFF(DAY,@dt,starttime)=2 THEN starttime END) AS
start3,
MIN(CASE WHEN DATEDIFF(DAY,@dt,endtime)=2 THEN endtime END) AS end3,
... etc
FROM YourTable
WHERE starttime >= @dt
AND starttime < DATEADD(DAY,7,@dt)
GROUP BY room, person

The rest is just formatting you can do in an Access report.
--
David Portas
SQL Server MVP
--

Jul 20 '05 #5

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

Similar topics

18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
6
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class names) that a manager class can walk through in...
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
8
by: David Lozzi | last post by:
I'm fairly new to ASP.Net 2.0 SQLDatasource objects. It defaults using TSQL statments for the SELECT, INSERT, UPDATE, DELETE commands, which is great and it works. However, I've always been taught...
4
by: eeb4u | last post by:
I am connecting to MS SQL 2000 from Red Hat EL4 using freetds and currently running queries to get counts on tables etc. When running SELECT queries I notice that the data returns and I have to...
1
by: Sagaert Johan | last post by:
Hi How can i create an sql server login (sql 2005 express) ? I have my TSQL code ready but how can i run it from c#? How can i execute TSQL code from Csharp, or are there better ways using some...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.