Author |
Topic |
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-11-16 : 11:00:16
|
Having a little trouble with the syntax here.I have a table:CREATE TABLE [dbo].[tblProducts] ( id int IDENTITY(1,1) CONSTRAINT tblProducts_PK PRIMARY KEY NOT NULL , vwr INT NOT NULL, revision VARCHAR(10) NOT NULL, reqType VARCHAR(50), leg VARCHAR(3), img1 IMAGE ) ON [PRIMARY] I need 3 fields: id, vwr, and reqTypeBut, I need the dataset to be distinct by unique vwr's.I've tried something like this but I'm getting a syntax error:select t.id, t.vwr, t.reqType( select distinct vwr from tblVWR) as d inner join tblVWR ton d.vwr = t.vwr I'm not a tsql guru and only deal with the db on occasion.I've searched around for a solution but just can't seem to get the syntax down.Suggestions?Thanks,Zath |
|
X002548
Not Just a Number
15586 Posts |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-16 : 11:03:11
|
[code]select t.id, t.vwr, t.reqTypefrom( select distinct vwr from tblVWR) as d inner join tblVWR ton d.vwr = t.vwr[/code] KH |
 |
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-11-16 : 11:04:15
|
THANKS!But with a little adjustment, I got it working:select distinct d.vwr from tblVWR AS dinner join tblVWR ton d.vwr = t.vwrZath |
 |
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-11-16 : 11:11:21
|
Ok, I take that back. As soon as I added the other fields, I got duplicates again.select t.id, t.vwr, t.reqTypefrom( select distinct vwr from tblVWR) as d inner join tblVWR ton d.vwr = t.vwrAND this one:select distinct d.id, d.vwr, d.reqType from tblVWR AS dinner join tblVWR ton d.vwr = t.vwrAre both giving me duplicate vwr's (the 1000):1000 MEC 71000 MEC 81018 TEST 61024 EE 11234 NULL 33333 NULL 45555 NULL 212345 NULL 599999 NULL 9 |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-16 : 11:18:30
|
quote:
1000 MEC 71000 MEC 8
so which record do you want ? KH |
 |
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-11-16 : 11:22:11
|
Either one is fine. Doesn't matter.An easy fix would be to leave the id out, but I need that as well.Especially since, for instance, 1000 will ALWAYS have the MEC.Zath |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-11-16 : 11:32:17
|
The third column, the id, is the primary key and I use that for another reason in the gridview.MIN or MAX?Never used those before. Have a link for a sample?Zath |
 |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-11-16 : 12:02:21
|
F1 is your best friend. Pull up Query Analyzer. Push F1. Tadah! The help file, which will explain MIN and MAX quite well.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2006-11-16 : 12:56:04
|
quote: Originally posted by Zath The third column, the id, is the primary key and I use that for another reason in the gridview.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@That's a red glag btwBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
samuelclay
Yak Posting Veteran
71 Posts |
Posted - 2006-11-16 : 16:45:32
|
Min/Max as Brett suggested:SELECT min(id), vwr, min(reqType)FROM tblVWR GROUP BY vwrbut is the data meaningful? in your example, you had a reqtype of both 7 & 8 for vwr= 'MEC' but do don't care which shows up? |
 |
|
|