Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Get next 2 weeks Expand / Collapse
Author
Message
Posted Wednesday, July 23, 2014 2:25 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:50 AM
Points: 225, Visits: 676
I am trying to get the numbers for the next two week from now . Need help with querying this ..

The table looks like this
CREATE TABLE dbo.Planner(
[Name] [nvarchar](255) NULL,
[ProjectName] [nvarchar](255) NULL,
,[7-Jul-14][nvarchar](255) NULL
,[14-Jul-14][nvarchar](255) NULL
,[21-Jul-14][nvarchar](255) NULL
,[28-Jul-14][nvarchar](255) NULL
,[4-Aug-14][nvarchar](255) NULL
,[11-Aug-14][nvarchar](255) NULL
,[18-Aug-14][nvarchar](255) NULL
,[25-Aug-14][nvarchar](255) NULL
,[1-Sep-14][nvarchar](255) NULL
,[8-Sep-14][nvarchar](255) NULL
,[15-Sep-14][nvarchar](255) NULL)

insert into Planner (Name,ProjectName,,[7-Jul-14]
,[14-Jul-14]
,[21-Jul-14]
,[28-Jul-14]
,[4-Aug-14]
,[11-Aug-14]
,[18-Aug-14]
,[25-Aug-14]
,[1-Sep-14]
,[8-Sep-14]
,[15-Sep-14]) Values ('Mike','Audit','10','10','10','0','5','5','5','4','15','14','10')


So if today is 23rd July , next two weeks sum will be 28th July and 4th Aug ie 0+5 = 5 . Need help with querying this ..


Post #1595711
Posted Wednesday, July 23, 2014 2:57 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:58 PM
Points: 3,575, Visits: 8,020
You have 2 options:
- Normalize your table
- Use dynamic SQL

You decide.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1595720
Posted Wednesday, July 23, 2014 3:10 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:51 AM
Points: 1,912, Visits: 19,380
Luis Cazares (7/23/2014)
You have 2 options:
- Normalize your table
- Use dynamic SQL

You decide.


I agree with Luis...normalise your data...

what are you expecting the results to be from your post?
what other tables do you have that store this data.....is this just for reporting ?



______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1595726
Posted Wednesday, July 23, 2014 3:28 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:50 AM
Points: 225, Visits: 676
Yes , its for reporting
Post #1595733
Posted Wednesday, July 23, 2014 3:35 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:51 AM
Points: 1,912, Visits: 19,380
sharonsql2013 (7/23/2014)
Yes , its for reporting


what are you expecting the results to be from your post?
what other tables do you have that store this data.....?


______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1595736
Posted Wednesday, July 23, 2014 5:57 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 2:12 PM
Points: 773, Visits: 5,016
I vote for normalizing too... then this query becomes trivial. Been down the unnormalized path once before and it was not a place I want to go again. The time you take to normalize with make the rest of this super easy and fast.
Post #1595769
Posted Wednesday, July 23, 2014 10:31 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:42 PM
Points: 35,216, Visits: 31,672
@sharonsql2013,

Many folks have correctly identified that the table should be "normalized". While it may not be possible for you to change the table, do you know how you would normalize the table if you had to? I ask because THAT's the key to this problem.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1595790
Posted Wednesday, July 23, 2014 11:12 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 2:12 PM
Points: 773, Visits: 5,016
What do (Name, Project, Date) represent? Some event? If you explain that part, we can help you sort out the design of your table. (As Jeff mentioned, are you allowed to restructure this table? I hope so, because this is going to be awful to query... and what happens when you add another date?)

Apologies for all the questions, but once you answer, I think figuring out a more flexible structure for your data should be pretty easy.
Post #1595794
Posted Friday, July 25, 2014 8:34 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 19, 2014 9:50 AM
Points: 225, Visits: 676
True, I cannot change the table .
Name is the name of the person , Project is the projectname and all the weeks represent the hours worked on the project.
Post #1596244
Posted Friday, July 25, 2014 11:01 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:42 PM
Points: 35,216, Visits: 31,672
sharonsql2013 (7/25/2014)
True, I cannot change the table .
Name is the name of the person , Project is the projectname and all the weeks represent the hours worked on the project.


You haven't answered my previous question. I understand that you can't change the table. Do you understand the normalization process that you'd use if you could? THAT's the key to solving this problem even if it doesn't result in you changing the table.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1596324
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse