Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 18th, 2009, 06:39 AM
Authorized User
 
Join Date: Sep 2008
Location: , , Singapore.
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default result set

Hi,

I have a table X with 3 columns name ,date(YYYYMMDD),total

name--date--total


x---02Jan2009--1
y---06Jan2009--1
z---10Jan2009--1
x---01Oct2008--1
x---03Nov2008--1
y---22Dec2008--1
x---02Feb2009--1
y---22Feb2009--1
z---03Feb2009--1
z---04Feb2009--1

select Name,count(total) from X where
TO_CHAR(date,'YYYYMM') = (TO_CHAR(TO_DATE('20090201','YYYYMMDD'),'YYYYMM'))
Group BY Name

i will get the result as

Name--Month total
x--1
y--1
z--2

i need to write a query in such a way that i need to get the result for that year which is in between 200901 to 200902 (the count of the selectee month and the previous months of the year)

Name--month total--year total

x--1--2
y--1--2
z--2--3


Regards
Raj
Reply With Quote
  #2 (permalink)  
Old May 18th, 2009, 08:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi rajesh_css,

I am not sure which DBMS you use, and what you actually wanted in this case. Just giving a try to see if this helps.
Code:
select Name,
SUM( (SELECT count(total) from X T1 where TO_CHAR(T1.date,'YYYYMM') = (TO_CHAR(TO_DATE('20090201','YYYYMMDD'),'YYYYMM')) AND T2.Name = X.Name ) )AS MonthTotal,
SUM( (SELECT count(total) from X T2 where TO_CHAR(T2.date,'YYYY') = (TO_CHAR(TO_DATE('20090201','YYYYMMDD'),'YYYY')) AND T2.Name = X.Name ) ) AS YearTotal
FROM X
Group BY Name
Thanks,
__________________
- Vijay G
Reply With Quote
  #3 (permalink)  
Old May 18th, 2009, 03:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You should almost always avoid changing dates into strings! Try to work with dates *AS DATES* and if you can't do that then work with them as numbers.

What DB is this?? Probably Oracle?

So see if Oracle doesn't have a better way to do this.

For example:
Code:
SELECT ...
WHERE T1.date >= TO_DATE('20090101') AND T1.date < TO_DATE('20090301') 
...
See that? >= January 1st but < March 1st. Now it works even if T1.date includes both date and time.
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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help - EXSLT func cannot set more than one result! anboss XSLT 1 October 28th, 2008 02:17 PM
getting a result set to a vector cakalanka Java Databases 1 March 24th, 2008 07:33 AM
write result set bmains SQL Server 2000 11 March 9th, 2004 09:31 AM
Result set jemacc SQL Server 2000 0 December 19th, 2003 07:26 PM
Using a SP result set in a FROM clause Bernard Omiple SQL Server 2000 1 October 3rd, 2003 05:38 AM



All times are GMT -4. The time now is 10:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.