Author |
Topic |
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 07:08:02
|
HiI have this query below that is meant to produce unique data but some of the data a replicate. Please take a look at it and let me know where the mistake isSELECT DISTINCT TOP ($limit) * From Revenue Where 1=1 AND BillIndex NOT IN ( SELECT TOP $offset BillIndex From Revenue ORDER BY BillIndex ASC) AND BillCreateDate >='$term' AND BillCreateDate <='$term1' AND UserIndex <> 135 AND UserIndex <> 53 AND UserIndex <> 51 ORDER BY BillIndex, UserID |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-03-06 : 07:23:37
|
As you use *, there is change some column values may get duplicatedPost some sample data with expected resultMadhivananFailing to plan is Planning to fail |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 07:28:50
|
Expected results should be: PPCB Jan 15 2009 12:00AM to Jan 15 2009 12:00AM 1 MEG Uncapped 2849.88 21889.05 12476.93ArunaKulasinghe Jan 15 2009 12:00AM to Jan 15 2009 12:00AM LiveBox256K-12 -220.00 478.99 258.99DHPConsultingEnginee Jan 15 2009 12:00AM to Jan 15 2009 12:00AM LiveBox256K-12 363.63 449.89 49.89 But it is showing:PPCB Jan 15 2009 12:00AM to Jan 15 2009 12:00AM 1 MEG Uncapped 2849.88 21889.05 12476.93PPCB Jan 15 2009 12:00AM to Jan 15 2009 12:00AM 1 MEG Uncapped 2849.88 21889.05 12476.93ArunaKulasinghe Jan 15 2009 12:00AM to Jan 15 2009 12:00AM LiveBox256K-12 -220.00 478.99 258.99DHPConsultingEnginee Jan 15 2009 12:00AM to Jan 15 2009 12:00AM LiveBox256K-12 363.63 449.89 49.89 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 07:45:50
|
Not sure, But may be you have a special character there. Can you put names of all the rows in your select. Also replace the column containing "Jan 15 2009 12:00AM to Jan 15 2009 12:00AM " column with replace(rtrim(ltrim(<columnname>)),char(9),'') |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 08:03:49
|
quote: Originally posted by sakets_2000 Not sure, But may be you have a special character there. Can you put names of all the rows in your select. Also replace the column containing "Jan 15 2009 12:00AM to Jan 15 2009 12:00AM " column with replace(rtrim(ltrim(<columnname>)),char(9),'')
It did not work |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 08:06:47
|
post the query that you ran. |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 08:10:53
|
quote: Originally posted by sakets_2000 post the query that you ran.
Here's the query:SELECT TOP ($limit) UserID, BillStartDate, BillEndDate, Package, Sums, Amount_Due, Previous_Amount From Revenue Where 1=1 AND BillIndex NOT IN ( SELECT TOP $offset BillIndex From Revenue ORDER BY BillIndex ASC) AND BillCreateDate >='$term' AND BillCreateDate <='$term1' AND UserIndex <> 135 AND UserIndex <> 53 AND UserIndex <> 51 ORDER BY BillIndex, UserID |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 08:17:28
|
quote: Originally posted by tkotey
quote: Originally posted by sakets_2000 post the query that you ran.
Here's the query:SELECT TOP ($limit) UserID, BillStartDate, BillEndDate, Package, Sums, Amount_Due, Previous_Amount From Revenue Where 1=1 AND BillIndex NOT IN ( SELECT TOP $offset BillIndex From Revenue ORDER BY BillIndex ASC) AND BillCreateDate >='$term' AND BillCreateDate <='$term1' AND UserIndex <> 135 AND UserIndex <> 53 AND UserIndex <> 51 ORDER BY BillIndex, UserID
Why did you remove "distinct"? |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-06 : 08:22:41
|
u first posted query has distinct keywordnow ur running query without distinct so that duplicates r getting in ur output |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 08:23:44
|
quote: Originally posted by sakets_2000
quote: Originally posted by tkotey
quote: Originally posted by sakets_2000 post the query that you ran.
Here's the query:SELECT TOP ($limit) UserID, BillStartDate, BillEndDate, Package, Sums, Amount_Due, Previous_Amount From Revenue Where 1=1 AND BillIndex NOT IN ( SELECT TOP $offset BillIndex From Revenue ORDER BY BillIndex ASC) AND BillCreateDate >='$term' AND BillCreateDate <='$term1' AND UserIndex <> 135 AND UserIndex <> 53 AND UserIndex <> 51 ORDER BY BillIndex, UserID
Why did you remove "distinct"?
Because it did not make a difference. I thought it was going to prevent the data duplication |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-06 : 08:28:35
|
Whats the data type of BillStartDate and BillEndDate |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 08:30:57
|
quote: Originally posted by sakets_2000 Whats the data type of BillStartDate and BillEndDate
datetime |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 08:33:43
|
When I add the DISTINCT I get the following error msgWarning: mssql_query() [function.mssql-query]: message: ORDER BY items must appear in the select list if SELECT DISTINCT is specified. (severity 15) |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2009-03-06 : 09:04:10
|
I solved the problem. I got help from <a href="http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx">here</a> |
|
|
|