Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
BigRetina
Posting Yak Master
144 Posts |
Posted - 2003-06-23 : 09:26:55
|
Salute...How can I compare two rows based on some field value??I have an employees payroll table. I want to compare between the current month transaction and the previous month transaction. I want to fetch every field that differed!.For example I have the following data :EmployeeNo BasicSalary Loan Treatment ... TransMonth---------- ----------- ----- ---------- -----------1 100 0 0 42 200 0 0 43 300 0 0 41 150 10 20 52 250 0 0 53 300 0 0 5 I want the output to be as follows EmployeeNo CurrentMonth PreviousMonth---------- ------------ -------------1 150 1001 10 01 20 02 250 0 p.s. Please note that I dont want to fix the coulmns.. I want to detect ANY column that changed and fetch the change!How can I do that??Thanks In Advance. |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2003-06-23 : 09:47:45
|
| there might be a more efficent way.select a.empno,b.basicsalary,a.basicsalaryfrom #emp ainner join #emp bon a.months=b.months-1and a.empno=b.empnoand a.basicsalary<>b.basicsalaryunionselect a.empno,b.loan,a.loanfrom #emp ainner join #emp bon a.months=b.months-1and a.empno=b.empnoand a.loan<>b.loanunionselect a.empno,b.treatment,a.treatmentfrom #emp ainner join #emp bon a.months=b.months-1and a.empno=b.empnoand a.treatment<>b.treatmentHTH-------------------------What lies behind you and what lies ahead of you are small matters compared to what lies within you.-Ralph Waldo Emerson |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-06-23 : 09:48:09
|
| You will have difficulty I think in returning randomly different columns per row. What could be done is that the entire row could be returned for any changed rows. That would beSELECT * from yourtable firstINNER JOIN yourtable second ON first.employeeno = second.employeeno and first.transmonth = second.transmonth - 1 and( first.basicsalary > second.basicsalary or first.basicsalary < second.basicsalary)The more not equal conditions that you add, the poorer this query will become....-------Moo. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-06-23 : 16:53:51
|
| the most efficient way to compare rows in a table is the GROUP BY operator... joins don't work that well -- they don't handle nulls, and you need to do funky FULL OUTER stuff to get all possibilities.Look at this:select EmployeeNo, BasicSalary, Loan, Treatment, MIN(TransMonth) as MonthFROMyourdataWHERE TransMonth in (@CurrentMonth, @PreviousMonth)GROUP BY EmployeeNo, BasicSalary, loan, treatmentHAVING COUNT(*) = 1that returns all rows that not exactly the same between @currentMonth and @PreviousMonth.If that format is not OK, let me know I can tell you how to get it into a cross tab-like format.note that you are NOT grouping by transMonth, and the MIN() aggregate function is arbitrary since we are only returning groups in which the COUNT(*) is one.- Jeff |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-06-24 : 04:19:21
|
quote: select EmployeeNo, BasicSalary, Loan, Treatment, MIN(TransMonth) as MonthFROMyourdataWHERE TransMonth in (@CurrentMonth, @PreviousMonth)GROUP BY EmployeeNo, BasicSalary, loan, treatmentHAVING COUNT(*) = 1that returns all rows that not exactly the same between @currentMonth and @PreviousMonth.
Shouldn't it be returning the rows where the count is > 1?-------Moo. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-06-24 : 07:46:10
|
| if all columns in 2 rows are the same, the group by will summarize them together in 1 row and the count(*) will be 2.if ANY columns are different, the group by will not be able to summarize them (since we are grouping on all columns we wish to compare) and the result will be two rows with no grouping being done -- each with a count(*) of 1.that is why we DON'T group by the TransMonth -- if we did, the count(*) would always be 1. and since we are only returning rows in which no summarization was done, the Min(transmonth) just returns the transmonth of each row that could not be GROUP'ed -- and thus is a unique row in the database.- Jeff |
 |
|
|
BigRetina
Posting Yak Master
144 Posts |
Posted - 2003-06-24 : 08:26:29
|
| First Of All Thanks To All Of U!...Nazim...Your method was Exactly the way I did it!.Thanks.jsmith8858...I like your way... Thanks...but I would like to know how to do the cross-tab view?Thanks Again |
 |
|
|
|
|
|
|
|