SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need a Query on group by condition


Need a Query on group by condition

Author
Message
smharabbani40
smharabbani40
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 23
Hi ALL,

I have one table which contines 3 columns they are

1.emp
2.monthyear
3.amount

in that table i am having 6 rows like below.

emp monthyear amount
1 102013 1000
2 102013 1000
1 112013 1000
1 112013 1000
2 122013 1000
2 122013 0000

i want a total on group by condition on each employee. which will have the data from NOV to dec 2013 only.

out put should be like this
1 2000
2 1000
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64814 Visits: 17979
smharabbani40 (10/11/2013)

Hi ALL,

I have one table which contines 3 columns they are

1.emp
2.monthyear
3.amount

in that table i am having four rows like below.

emp monthyear amount
1 112013 1000
1 112013 1000
2 122013 1000
2 122013 0000

i want a total on group by condition on each employee.

out put should be like this
1 2000
2 1000


This looks a lot like homework. You will need to use SUM.

http://technet.microsoft.com/en-us/library/ms187810.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
smharabbani40
smharabbani40
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 23
Hi I need a Query from the month 112013 to 122013 sir......

i have used the below Query which is not satisfieing.


SELECT Emp,
SUM (AMOUNT),
Monthyear
FROM table WHERE MonthYear between 112013 and 122013
GROUP BY Emp

Sorry to say ,if it very easy thing i wont put in here.

anyhow thanks for your replay
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64814 Visits: 17979
smharabbani40 (10/11/2013)
Hi I need a Query from the month 112013 to 122013 sir......

i have used the below Query which is not satisfieing.


SELECT Emp,
SUM (AMOUNT),
Monthyear
FROM table WHERE MonthYear between 112013 and 122013
GROUP BY Emp

Sorry to say ,if it very easy thing i wont put in here.

anyhow thanks for your replay


That query won't run because you did not also group by Monthyear.

It seems like Monthyear is an int datatype? If at all possible I would highly recommend changing to a date or datetime datatype instead. It will provide instant validation that the data entered is valid and it will let you do datemath without being forced to cast/convert it first.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
smharabbani40
smharabbani40
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 23
Hi

MonthYear is the INT data type.that is only the issue ,becuse of that between is not working....i can able to change datatype....

if any suggestion/fixe rather than this please let me know.

Thanks in advanced.............:-)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64814 Visits: 17979
smharabbani40 (10/11/2013)

Hi

MonthYear is the INT data type.that is only the issue ,becuse of that between is not working....i can able to change datatype....

if any suggestion/fixe rather than this please let me know.

Thanks in advanced.............:-)


Yeah the between is likely going to return values you don't want. Consider 122012. That would be december 2012 according to your pattern. It in fact does belong between 112013 and 122013 but it is not what you want in your query. This type of datemath is exactly what I was referring to in my first post. I would probably change this to a date datatype and then you can this type of query quite easily.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13766 Visits: 4077
declare @tbl table 
(
emp int ,
monthyear int,
amount money
)




insert into @tbl
select 1, 102013 ,1000
union all select 2, 102013, 1000
union all select 1, 112013 ,1000
union all select 1, 112013, 1000
union all select 2, 122013 ,1000
union all select 2 ,122013,0000



select emp, SUM(amount) from @tbl
where LEFT(monthyear,2) = '11' or LEFT(monthyear,2) = '12'
group by emp



-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221926 Visits: 42003
Bhuvnesh (10/13/2013)
declare @tbl table 
(
emp int ,
monthyear int,
amount money
)

insert into @tbl
select 1, 102013 ,1000
union all select 2, 102013, 1000
union all select 1, 112013 ,1000
union all select 1, 112013, 1000
union all select 2, 122013 ,1000
union all select 2 ,122013,0000

select emp, SUM(amount) from @tbl
where LEFT(monthyear,2) = '11' or LEFT(monthyear,2) = '12'
group by emp



I'm thinking that there's a major problem with that solution. What is the absolute guarantee that data from another year won't be included in the table? The code also contains a non-SARGable WHERE clause that will guarantee a table scan rather than a seek and range scan.

The absolute best thing to do here would be to store the date data correctly. Using INT to store date data isn't the best way. It could certainly be improved by storing the date in the INT in a sortable fashion (yyyymm instead of mmyyyy) but that's still not the best way.

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221926 Visits: 42003
Sean Lange (10/11/2013)
smharabbani40 (10/11/2013)
Hi I need a Query from the month 112013 to 122013 sir......

i have used the below Query which is not satisfieing.


SELECT Emp,
SUM (AMOUNT),
Monthyear
FROM table WHERE MonthYear between 112013 and 122013
GROUP BY Emp

Sorry to say ,if it very easy thing i wont put in here.

anyhow thanks for your replay


That query won't run because you did not also group by Monthyear.

It seems like Monthyear is an int datatype? If at all possible I would highly recommend changing to a date or datetime datatype instead. It will provide instant validation that the data entered is valid and it will let you do datemath without being forced to cast/convert it first.


Plus 1 billion to that.

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
smharabbani40
smharabbani40
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 23
Hi Thanks all for your support on the Query posted by me.

i think i should give the exact data here.

the data in the table is mentioned below.

EMP MONTHY AMOUNT
1 12013 1000
1 22013 1000
1 32013 1000
2 12013 1000
2 32013 0000
2 42013 1000
1 102013 1000
1 112013 1000
1 12014 1000
1 22014 1000
2 12014 0000
2 22014 1000
1 102014 1000
2 102014 1000

The Query should be give the data between 112013 to 102014. and the monthY column is the integer data type.

i need a data as mentioned in the below format.

EMP AMOUNT
1 4000(THIS AMOUNT INCLUDES THE MONTHS FROM 112013 TO 102014)
2 3000(THIS AMOUNT INCLUDES THE MONTHS FROM 112013 TO 102014)

Please let me know if anybody have any fixes on the same.
Thanks in advanced:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search