Solved

Sql help

Posted on 2011-02-16
5
178 Views
Last Modified: 2012-05-11
Hi,
I am trying to write sql to find the info based on a particular field, grouped by date (quarter)
The sql works fine but I have two more points,
1. Be able to add the DatePart(year, date) to the date columns - so that we know which year quarter it is
As you can see in below code it would be adding 2010 q1 + 2011 q1
Need to get over that
First is most important
2. Is there any way,
That I could change the above sql to have count(MAN) for multiple models?
Displayed as this model had count = this - in particular quarter
select DatePart(quarter, date) as date1,  count(MAN)
from table
where DatePart(year, date) >= '2010'
and model = 'Particular'
group by DatePart(quarter, date)

Open in new window

0
Comment
Question by:SameerMirza
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34905781
please try this:
select model, DatePart(year, date), DatePart(quarter, date) as date1,  count(MAN)
from table
where DatePart(year, date) >= 2010
group by model, DatePart(year, date), DatePart(quarter, date) 

Open in new window

0
 

Author Comment

by:SameerMirza
ID: 34905922
thanks angel. But issue is to have done somthing like
DatePart(quarter, date) + '/' + DatePart(year, date)
Problem rises when it comes to the data conversion related issue
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34905932
you mean:
select model
, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date) as varchar(10)) as date1,  count(MAN)
from table
where DatePart(year, date) >= 2010
group by model, cast(DatePart(year, date) as varchar(10)) + '/' + cast( DatePart(quarter, date) as varchar(10))

Open in new window

0
 

Author Comment

by:SameerMirza
ID: 34907604
yes. Its perfact
0
 

Author Closing Comment

by:SameerMirza
ID: 34907605
thx
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure service broker on all databases 2 88
INSERT DATE FROM STRING COLUMN 18 59
kill process lock Sql server 9 65
SQL Server 2005 - Comparing Fields' Contents 6 44
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…

820 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