SQL query for obtaining answers

20 pts.
Tags:
SQL
SQL Query
Table student containing 2 columns,Join date,Employee name. Under join date 4 rows r ter =1-jan-2008,2-feb-2008,3-mar-2008,4-feb-2008.Under Employee name 4 difeerent names jaison,robin,binoy,rahul Result set is, Table containing 4-column name=jan,feb,mar,april,,beneath these months count is given as 1,2,1,0 means these counts representing number of emplooyees joined in a month(january 1employee,february 2 employee,march 1 employee,april 0 employee) Can you give me the required sql query

Answer Wiki

Thanks. We'll let you know when a new response is added.

The answer could be different depending on the database you are using, which you didn’t specify.

If Oracle, you could use a query like this:

<pre>SELECT SUM(DECODE(TO_CHAR(join_date,’MM’),’01′,1,0)) JAN,
SUM(DECODE(TO_CHAR(join_date,’MM’),’02′,1,0)) FEB,
SUM(DECODE(TO_CHAR(join_date,’MM’),’03′,1,0)) MAR,
SUM(DECODE(TO_CHAR(join_date,’MM’),’04′,1,0)) APR
FROM YourTable;</pre>

In SQL Server, you could use a query like this:

<pre>SELECT
SUM(
CASE DATEPART(MM,join_date)
when 01 then 1
else 0
END) as Jan,
SUM(
CASE DATEPART(MM,join_date)
when 02 then 1
else 0
END) as Feb,
SUM(
CASE DATEPART(MM,join_date)
when 03 then 1
else 0
END) as Mar,
SUM(
CASE DATEPART(MM,join_date)
when 04 then 1
else 0
END) as Apr
FROM YourTable;</pre>

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    If you are not using Oracle, please provide more information (at least database and version).
    70,200 pointsBadges:
    report
  • Frs
    I am using sql2008
    20 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following