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»»

Need a Query on group by condition Expand / Collapse
Author
Message
Posted Friday, October 11, 2013 7:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 7:51 AM
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
Post #1504011
Posted Friday, October 11, 2013 7:42 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's 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)
Post #1504014
Posted Friday, October 11, 2013 7:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 7:51 AM
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
Post #1504022
Posted Friday, October 11, 2013 8:02 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's 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)
Post #1504023
Posted Friday, October 11, 2013 8:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 7:51 AM
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.............
Post #1504038
Posted Friday, October 11, 2013 8:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's 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)
Post #1504043
Posted Sunday, October 13, 2013 6:00 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, March 14, 2014 2:19 AM
Points: 2,820, Visits: 3,916
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
Post #1504254
Posted Sunday, October 13, 2013 10:07 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:00 PM
Points: 35,967, Visits: 30,258
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(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 #1504273
Posted Sunday, October 13, 2013 10:14 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:00 PM
Points: 35,967, Visits: 30,258
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(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 #1504274
Posted Monday, October 14, 2013 2:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 13, 2014 7:51 AM
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

Post #1504534
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse