SQL 2008 Query Help

Posted on 2010-11-25
Last Modified: 2012-05-10

I need to create a table which shows days of the week. Data is from Database.


Team        Sun        Mon         Tue     Wed        Thur        Fri      Sat

Headers are static.

Based on current date, (11/25/2010) which is a "Thurs" day, this data should come in Thurs field and all the before days should calculate automatically based on that respective date. Fri & Sat should be empty as no data will be there in the database.

If the current date is Sun, then rest of the data should be blank.

Pls guide me to write a query which will do this function.

Question by:davidi1
  • 2
  • 2
LVL 58

Expert Comment

ID: 34214193
Do you want to create a table or a query?
"this data should come in Thurs field"
Can you please explain what that means?  Data from some table that has a date column?
Does the query always show only 1 row (current week)?
Where does Team fit in?

Author Comment

ID: 34214259
sorry. my bad. let me clarify more.

SQL Table Name: RawData


TeamName, Date, Closecount

I already run a query to pullup dynamic table which will display last 7 days of data. however my org doesnt want that. they want to keep the headers static & want me to modify the query to satisfy the headers.

for every day ("Sun") ex:

5 rows will be generated as result.

I need to populate the same data with respect to days, Sun, Mon, Tue.., etc..,

lemme know if you have more questions.., sorry.., answering your question real fast as im jumping to a meeting. will be back to you in 15 mins.

LVL 51

Accepted Solution

HainKurt earned 500 total points
ID: 34214323
do you want something like this?

declare @aDate DateTime = getdate();

declare @dw int = datepart(dw, @aDate);

select @dw,

@aDate - @dw + 1 sun,

@aDate - @dw + 2 mon,

@aDate - @dw + 3 tue,

@aDate - @dw + 4 wed,

@aDate - @dw + 5 thu,

@aDate - @dw + 6 fri,

@aDate - @dw + 7 sat

Open in new window

LVL 58

Expert Comment

ID: 34214366
Sorry, but it is still not clear to me

for every day ("Sun") ex:
5 rows will be generated as result.

Why are there 5 rows for Sunday?
If you run it on a Thursday, I know you mentioned that Fri/Sat should be blank, but are the preceeding days also blank?
Do you want the closecount value from the table in the columns under the days, or the dates?

Author Comment

ID: 34214478
Nevermind cyberkiwi.  Hainkurt resolved my problem.

Hainkurt: Bingo. This is exactly what i was looking for. Thanks. Its works great.


Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
BULK LOGGED - log full 9 25
Help with SQL - TOP 10 by date and by group 13 32
Cant preview report change unless report is save first 5 29
Benefits of SMB Fileshare 3 62
Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now