MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 19th, 2012, 05:11 AM
Sazrazz771 Sazrazz771 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 Sazrazz771 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 23 sec
Reputation Power: 0
SQL query-table help urgent

Hi guys -I am new in here - Last time I worked on some sql queries was back in 2007. Now I need some help with this problem. Please let me know the solution of this problem.
skydrive.live.com/redir?resid=3F0B2DD98F3A474!286

Reply With Quote
  #2  
Old September 19th, 2012, 06:02 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,357 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 40 m 42 sec
Reputation Power: 4140
1. we don't solve homework assignments on this site (although we will help you debug your efforts)

2. the data is totally inconsistent
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old September 19th, 2012, 06:05 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
3. We dislike the word 'urgent'.

Reply With Quote
  #4  
Old September 19th, 2012, 06:15 AM
Sazrazz771 Sazrazz771 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 Sazrazz771 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 23 sec
Reputation Power: 0
re

Its not a homework assignment. I just need general sql statement for this type of work/table/output.

Thx

Reply With Quote
  #5  
Old September 19th, 2012, 06:20 AM
Sazrazz771 Sazrazz771 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 Sazrazz771 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 23 sec
Reputation Power: 0
Alrite guys I apologize for all the "don'ts" of this forum - my first post gimme a break! I just need help with the problem at hand if anybody can guide/help that will be much appreciated. thanks!!!

Reply With Quote
  #6  
Old September 19th, 2012, 06:55 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Welcome to the rough and tumble of devshed!

r937's point (which I thought he expressed rather eloquently) was that the data shown in the 'outputs' does not correspond to that provided in (the sample) data set.

This makes it hard to guess what you want.

Consider the following:

Code:
DROP TABLE IF EXISTS table1;

CREATE TABLE table1
(pid INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,productdesc VARCHAR(30) NOT NULL UNIQUE
,productcode VARCHAR(20) NOT NULL UNIQUE
);

DROP TABLE IF EXISTS table2;

CREATE TABLE table2
(date DATE NOT NULL 
,pid INT NOT NULL 
,PRIMARY KEY(date,pid)
);

INSERT INTO table1 VALUES
(1,'Packet-Eye','P001'),
(2,'Wiggy','W099'),
(3,'Wimax-Lite','W001'),
(4,'Wimax-Home','W002');

INSERT INTO table2 VALUES
('2009-01-01',1),
('2009-01-12',1),
('2009-02-14',2),
('2009-03-03',3);

SELECT t1.productdesc `Product Description`,t2.date `Registration Date` FROM table1 t1 JOIN table2 t2 ON t2.pid = t1.pid;
+---------------------+-------------------+
| Product Description | Registration Date |
+---------------------+-------------------+
| Packet-Eye          | 2009-01-01        |
| Packet-Eye          | 2009-01-12        |
| Wiggy               | 2009-02-14        |
| Wimax-Lite          | 2009-03-03        |
+---------------------+-------------------+


Personally, I prefer to handle date formatting at the application level. It's often simpler to let the user or the user's locale 'choose' the desired format with a bit of javascript.

Output2 appears to be the product of a PIVOT TABLE. Although this kind of thing can be done in MySQL, again it's often best to return an ORDERed result and handle the display logic at the application level (e.g. with a bit of PHP).

Reply With Quote
  #7  
Old September 19th, 2012, 07:20 AM
Sazrazz771 Sazrazz771 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 Sazrazz771 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 23 sec
Reputation Power: 0
Thanks a ton! I owe you a latte! Your just made me have a flash back from the past when I used to play around with these sql statements...
For the second output I am guessing a COUNT(Product_Description) has to be used on output1 and then probably a Group by Month ... if someone can just type the statement out that will be great.

Thanks again!!

Reply With Quote
  #8  
Old September 19th, 2012, 07:41 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Code:
SELECT t1.something Product
     , DATE_FORMAT(t2.something_else,'%Y-%m') dt
     , COUNT(*) total 
  FROM table2 t2 
  JOIN table1 t1 
    ON t1.another_thing = t2.another_thing 
GROUP 
    BY t1.another_thing
     , dt;


Strictly speaking, you should only GROUP BY unaggregated fields identified in the SELECT clause, but I'm OK here because I've previously established that certain fields are UNIQUE.

Also I've not included an ORDER BY clause because the ORDER is implicit within the GROUP BY clause.

Finally, note that 'product-months' for which there are no results will not appear in the output. For that you must OUTER JOIN to the products table and either include a calendar utility table (containing all possible months or dates) or once again (and my preference), handle the logic for missing results at the application level.

Last edited by cafelatte : September 19th, 2012 at 07:46 AM.

Reply With Quote
  #9  
Old September 19th, 2012, 07:54 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,357 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 40 m 42 sec
Reputation Power: 4140
Quote:
Originally Posted by Sazrazz771
Its not a homework assignment. I just need general sql statement for this type of work/table/output.
i call bs

first of all, it's written as an assignment ("using table1 and table2, construct 2 sql scripts...")

but more tellingly, why would you put "urgent" unless it was something you had to hand in soon

if you're just interested in learning, why not wait until next week

Reply With Quote
  #10  
Old September 19th, 2012, 08:11 AM
Sazrazz771 Sazrazz771 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 Sazrazz771 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 23 sec
Reputation Power: 0
Thx for all your help with this.

@R937 its not an assignment its a side task Im taking at work- looking at some historical data. I was pretty good at data mining pre-2007 but after that my managerial position didnt involve sql tasks. Hence I needed some help.

Reply With Quote
  #11  
Old September 19th, 2012, 09:07 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
Quote:
Originally Posted by Sazrazz771
Alrite guys I apologize for all the "don'ts" of this forum - my first post gimme a break!


As a new user to a site it is always advisable to read FAQs and forum guidelines. The faq link is at the top of the site and here is a link to specific forum rules:

Forum rules

And people are going to be suspicious of "homework" type questions from new forum users because it happens a lot.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > SQL query-table help urgent

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap