Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 18th, 2009, 06:39 AM
Authorized User
 
Join Date: Sep 2008
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
 
Old May 18th, 2009, 08:41 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old May 18th, 2009, 03:33 PM
Friend of Wrox
 
Join Date: Jun 2008
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.





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





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