If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Restructuring query.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-23-05, 03:16
jmut jmut is offline
Registered User
 
Join Date: Mar 2004
Location: Bulgaria
Posts: 22
Restructuring query.

Hi,
I got a little trouble with my query. It was intendet to do one thing but now the data I should fetch is changed so I am thinking could I rewrite the query to fix the problem or should I use multiple queries and php after that.

MySQL 4.1

Code:
SELECT DAYNAME( date ) AS pd, count( * ) AS amount, produkt_id
                        FROM contract
                        WHERE ( 1 )
                            AND contract_status_id = 1
                            AND WEEK( date, 1 ) = 51
                            AND YEAR( date ) = 2005
                        GROUP BY produkt_id, pd
                        WITH ROLLUP
With this query I get data similar to this:
Code:
pd      amount   produkt_id
Monday 	2 	50037
Saturday 	1 	50037
Wednesday 	1 	50037
NULL 	4 	50037
Friday 	1 	50091
Monday 	1 	50091
Wednesday 	1 	50091
NULL 	3 	50091
Monday 	2 	50199
Tuesday 	1 	50199
This works perfectly if I am looking for data for the current week only.
But now I have to search for all data till that week.
So for example one may mistakanly do :

Code:
SELECT DAYNAME( date ) AS pd, count( * ) AS amount, produkt_id
                        FROM contract
                        WHERE ( 1 )
                            AND contract_status_id = 1
                            AND WEEK( date, 1 ) <= 51
                            AND YEAR( date ) <= 2005
                        GROUP BY produkt_id, pd
                        WITH ROLLUP
But this will gather data for all mondays,tuesdays etc etc. not getting it per day for all products of one type.

(and will work for current year only).

I hope I did explain the problem.
Thanks in advance for spending time.
Cheers,
JD
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On