DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   MS SQL (http://www.daniweb.com/forums/forum127.html)
-   -   Crazy CrossTab problems (http://www.daniweb.com/forums/thread164857.html)

muek Dec 29th, 2008 10:09 pm
Crazy CrossTab problems
 
Hi there,
I'm completely lost.

I have 3 tables:
- work (workID, description)
- machine (machineID, name)
- work_machine (machineID, workID, hours)

What I want is to obtain a result similar to:

work, machine1, machine2
A , 1 ,
B , 3 , 5

The problem is that the number of works and the number of machines is unknown.

Can anyone help?

kanaku Dec 30th, 2008 1:33 am
Re: Crazy CrossTab problems
 
I think what you can do on your work_machine table is have references to the workid and machineid...

work_machineID | workID | machineID
1 | A | 1
2 | B | 3
3 | B | 5

Then just select from this table where workID = 'A';

Ramy Mahrous Dec 30th, 2008 5:31 am
Re: Crazy CrossTab problems
 
Muke, seems you didn't normalize your database design!!

muek Dec 30th, 2008 10:54 am
Re: Crazy CrossTab problems
 
Ok, I think that I haven't explain myself.

In table "work_machine" I have:
workID | machineID | hours
A | M1 | 1
B | M2 | 3
B | M1 | 5

Now I would like to know how to make the query to obtain the data in the format:

work | machine1 | machine2
A | 1 | 0
B | 3 | 5

Any idea?

Ramy Mahrous Dec 30th, 2008 11:13 am
Re: Crazy CrossTab problems
 
What's machine1 and machine2 ?? and what '0' in the first row in output?

muek Dec 30th, 2008 11:58 am
Re: Crazy CrossTab problems
 
Hi,

machine1 and machine2 are rows from machine table
The 0 represents that machine2 don't do any "A" work

Quote:

Originally Posted by RamyMahrous (Post 767279)
What's machine1 and machine2 ?? and what '0' in the first row in output?


Ramy Mahrous Dec 30th, 2008 12:16 pm
Re: Crazy CrossTab problems
 
As I said, you've lack in your design, I am sorry but your database design is wrong. It need some normalization

kanaku Dec 30th, 2008 12:41 pm
Re: Crazy CrossTab problems
 
Hi again! Maybe you can try reading this: some-thread-on-sums.

Or this one @ daniweb.

Or ask RamyMahrous to describe the proper table structure. =)

muek Dec 30th, 2008 2:36 pm
Re: Crazy CrossTab problems
 
Hi RamyMahrus,

my DB design is the following:
I have 3 tables:
- work (workID, description)
- machine (machineID, name)
- work_machine (machineID, workID, hours)

Can you tell me what's wrong?


Quote:

Originally Posted by RamyMahrous (Post 767317)
As I said, you've lack in your design, I am sorry but your database design is wrong. It need some normalization


Ramy Mahrous Dec 30th, 2008 2:45 pm
Re: Crazy CrossTab problems
 
That's correct give me real sample data to all these tables + output you need not table just in english text what you need to extract from these tables.


All times are GMT -4. The time now is 9:37 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC