Author |
Topic |
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 10:52:46
|
I am trying to write a query that will only return the non distinct recordswhat i have is a bunch of line items that have an order id and a ratethere may be 10 line items with the same orderid, I need to get the orderid and rates for the orderids that have more than one ratehow would i do this? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-01 : 10:53:29
|
[code]select t.*from yourtable tinner join ( select order_id from yourtable group by order_id having count(*) > 1) d on t.order_id = d.order_id[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-01 : 10:58:03
|
[code]SELECT OrderID, RateFROM ( SELECT OrderID, Rate, COUNT(*) OVER (PARTITION BY OrderID) AS Items FROM Table1 ) AS dWHERE Items > 1[/code] E 12°55'05.25"N 56°04'39.16" |
|
|
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 11:07:04
|
thanks for the fast replyPeso -- i get over construct is not supportedkhtan -- yours doesnt address the distinct rates |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-01 : 11:22:30
|
Peter's solution is for SQL Server 2005 only.try thisselect t.*from yourtable tinner join ( select order_id from yourtable group by order_id having count(*) = 1) d on t.order_id = d.order_id KH[spoiler]Time is always against us[/spoiler] |
|
|
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 11:26:43
|
adding distinct got it working thanks |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-01 : 11:28:51
|
"thats the same query that you posted before"It looks to me that khtan very carefully put the part that is different in red for you.Kristen |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-01 : 11:30:35
|
But if I have understood your question I think you need:select t.*from yourtable tinner join ( select DISTINCT order_id from yourtable group by order_id, rate having count(*) > 1) d on t.order_id = d.order_id Kristen |
|
|
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 11:39:36
|
that returns everything -- its not getting just the records with multiple rates |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-01 : 11:43:14
|
post your table DDL, sample data and result that you want KH[spoiler]Time is always against us[/spoiler] |
|
|
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 11:47:55
|
sample dataorderid RATE3DF4-966312330 13DF4-966312330 13DF4-966312330 13E3D-966312331 13E3D-966312331 13E3D-966312331 2returns3E3D-966312331 13E3D-966312331 2 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-01 : 12:09:40
|
Why would3E3D-966312331 2show, there's only one of those and you said " for the orderids that have more than one rate"Similarly, why doesn't 3DF4-966312330 1show?Kristen |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-01 : 12:25:30
|
declare @tbl table (col1 varchar(25),col2 int)INSERT INTO @tblselect '3DF4-966312330', 1 union allselect '3DF4-966312330', 1 union allselect '3DF4-966312330', 1 union allselect '3E3D-966312331', 1 union allselect '3E3D-966312331', 1 union allselect '3E3D-966312331', 2 select distinct b.* from @tbl ainner join @tbl bon a.col1 = b.col1and a.col2 <> b.col2 |
|
|
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 12:41:41
|
Why wouldmore than one rate in that 1 and 2 are different so their are two rates on that one. the frist doesnt show because there is only one so i dont need it |
|
|
tpiazza55
Posting Yak Master
162 Posts |
Posted - 2007-10-01 : 13:20:56
|
jimf what you did got it thanks |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-01 : 13:24:32
|
OK, NOW I understand what your requirements are at last.select DISTINCT t.*from yourtable tinner join ( select order_id from yourtable group by order_id having count(DISTINCT rate) > 1) d on t.order_id = d.order_id Kristen |
|
|
|