Mel...
Can you show a sample of your table with some data (use data that best illustrates the case you are trying to accomplish) and a sample of what you want your result set to be like?
Main Topics
Browse All TopicsHi- I have this:
SELECT Distinct name, description
FROM products
But I don't want the column 'description' to be distinct. (In fact, it won't let me make it distinct, because it is text. name is varchar).
Is there a way to do that, select one column distinctly and another..ummm... indistinctly? :)
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
This is a little hard to explain, because I'm pretty inexperienced. I am pulling from two tables.
In the second table, the item appears twice. That is why I want it to be distinct.
So in products, I have:
2060|Bada|“Bada- Bing” Cherry Bread Pudding|This luscious pudding is to die for. We start with fresh French bread cubes and B& B soaked dark Bing Cherries. We put them together with the finest sugar and cream and bake; all you have to do is heat 1 minute and serve with our Crème Anglaise.
And in the table sizes, I have:
125567|size4|2060|4||15|0|
125510|size2|2060|2||7.5|0
I want the results to be:
|“Bada- Bing” Cherry Bread Pudding|This luscious pudding is to die for. We start with fresh French bread cubes and B& B soaked dark Bing Cherries. We put them together with the finest sugar and cream and bake; all you have to do is heat 1 minute and serve with our Crème Anglaise.|size2|size4
Not:
|“Bada- Bing” Cherry Bread Pudding|This luscious pudding is to die for. We start with fresh French bread cubes and B& B soaked dark Bing Cherries. We put them together with the finest sugar and cream and bake; all you have to do is heat 1 minute and serve with our Crème Anglaise.|size2|
|“Bada- Bing” Cherry Bread Pudding|This luscious pudding is to die for. We start with fresh French bread cubes and B& B soaked dark Bing Cherries. We put them together with the finest sugar and cream and bake; all you have to do is heat 1 minute and serve with our Crème Anglaise.|size4|
Does that make sense? And as I said, it won't let description be distinct. Thanks!
OK.. I don't have access to SQL right now but try this(I apologize for syntax, normally I check queries but I am not connected to the network so no server to test on)
SELECT prod.description, size1.Price [Size 2 Price], size2.Price [Size 4 Price]
FROM PRODUCTS prod
INNER JOIN TableSize size1
ON prod.PrimaryKey = size1.PrimaryKey
INNER JOIN TableSize size2
ON prod.PrimaryKey = size2.PrimaryKey
You will need to change the columns and tables appropriately.
Sorry... In a haze today (was out doing tilling in the heat..)
My solution won't actually do anything for you there.. You'd have to add something like
SELECT prod.description, size1.Price [Size 2 Price], size2.Price [Size 4 Price]
FROM PRODUCTS prod
INNER JOIN TableSize size1
ON prod.PrimaryKey = size1.PrimaryKey
AND size1.SizeType = 'Size2'
INNER JOIN TableSize size2
ON prod.PrimaryKey = size2.PrimaryKey
AND size2.SizeType = 'Size4'
This is not the best solution the more I think about it.. It will give you results you desire when you have two prices, but if you can have more or less we need to do something a little different.
ok.. but you will need to replace everything with your tables and columns.. Not sure how much you know about SQL.. You said you didn't know a lot, so sorry if this is insulting just want to give you all of the info you need..
So where I said "PrimaryKey" you will need to replace with the actual column names from each table, need to change your table names, and need to change the "sizetype" to actually be the name of the column..
If you want any other information returned, you can put it in the select list, just prefix it with the table it is coming from's alias.
Hi- Sorry, I'll have to check this with you to be sure I understand..
So the tables are:
products (columns I am using are are id, name, description)
sizes (columns I am using are product_id, which links to id, size2_price, size4_price- but each id from the product table appears twice in the sizes table).
So if the product table has an id of 2060, the sizes table will have:
Id=23774, product_id=2060, size2_price=8
Id =23775, product_id=2060, size4_price=16
And I want the outcome to say:
Name|Description|Size2_pri
What I am seeing now is:
Name|Description|Size2_pri
Name|Description|Size4_pri
Is that a little better explanation? Sorry I was a little obtuse earlier. Will your solution above still work, do you think?
Thanks again!
Assume that table sizes have 2 fields, 1 store type of size and other store value, I add sub query to avoid select text filed in group clause
try this!
select sizes.id,name, select top 1 description from products as p where p.id=sizes.product_id) as description,
sum(case when sizetype='size2_price' then sizevalue else 0 end) as size2_price,
sum(case when sizetype='size4_price' then sizevalue else 0 end) as size4_price
from sizes
inner join products on sizes.product_id=products.
group by sizes.id,name
Hi, HuyBD,
Wow- that looks complicated to me! I tried a straight copy and paste, and the browser didn't like it.. If I understood more I might be able to get it to work, I apologize for not being more adept. It looks like you have all the fields right as far as my column names go, but I get an error:
Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]Incorrec
I forget to open the parentheses
select sizes.id,name, (select top 1 description from products as p where p.id=sizes.product_id) as description,
sum(case when sizetype='size2_price' then sizevalue else 0 end) as size2_price,
sum(case when sizetype='size4_price' then sizevalue else 0 end) as size4_price
from sizes
inner join products on sizes.product_id=products.
group by sizes.id,name
>>Hey- maybe that should be group by sizes.id, products.name?
yes, you can add all prefix in select
select sizes.id,products.name, (select top 1 description from products as p where p.id=sizes.product_id) as description,
sum(case when sizes.sizetype='size2_pric
sum(case when sizes.sizetype='size4_pric
from sizes
inner join products on sizes.product_id=products.
group by sizes.id, products.name
/*create this function */
create function dbo.fn_getsize(@product_id
returns varchar(1000) as
begin
declare @sizes varchar(1000), @size varchar(100)
set @sizes =''
declare c cursor for select size2orsize4_ColumnName from sizes where product_id = @product_id for read only
open c
fetch next from c into @size
while @@fetch_Status = 0
begin
set @sizes = ',' + @size
fetch next from c into @size
end
close c
deallocate c
return @sizes
end
go
/*use this query to fetch the data
==========================
select [name],
[description],
dbo.fn_getsize(product_id)
from products
Mel -
I thought in your original post to me you had a column in the sizes table that indicated if it was a 2 or a 4 type size.. There are no other columns in that table? If there are my second query will work without the function or anything else..
You wrote:
125567|size4|2060|4||15|0|
125510|size2|2060|2||7.5|0
for what was in the sizes table... If that |size4| is a column or the |4| is a column that indicates size, you can use my second query:
SELECT prod.description, size1.Price [Size 2 Price], size2.Price [Size 4 Price]
FROM PRODUCTS prod
INNER JOIN TableSize size1
ON prod.PrimaryKey = size1.PrimaryKey
AND size1.SizeType = 'Size2'
INNER JOIN TableSize size2
ON prod.PrimaryKey = size2.PrimaryKey
AND size2.SizeType = 'Size4'
"SizeType" is whatever column has the |Size4| or |Size2| in it. If there is such a column.
PrimaryKey is the key name in the Products table and the corresponding key in the sizes table/
TableSize is the table that has sizes in it
Everything else you can leave as is (assuming there is in fact a column with "size2" or "size4" in it.
I'm sorry to all- I haven't been giving the right information, and that has wasted some of your time.
Here are the exact column headers for table size, with two rows of data. It is confusing, because the way the former developers built it, there is a ROW that specifies size 2 or size 4, and a price in each row. But then I added COLUMNS size2_price and size4_price, becuase I wanted to call them for this project. And I copied over the price column to the size2_price and size_four price columns:
id |code| product_id |name| color| price| sale_price| cost| stock| sort_order| product_code| active |weight |oversize| size2_price |size4_price
125503 size2 1333 2 13.33 0 0 0 0 SoyTend 0 0 0 13.33
125534 size4 1333 4 26.66 0 0 0 0 SoyTend 0 0 0 26.26
Ok...So now you ahve the price in two places. That is redundant and not the best form.. Where is the price always going to be accurate? in the "Size2_Price" and "Size4_Price" columns or in the "price" column? Looking at the above, it actually makes more sense NOT having the extra columns at the end and having two rows for each..
But anyway this should work:
SELECT prod.description, size2.Price [Size 2 Price], size4.Price [Size 4 Price]
FROM PRODUCTS prod
INNER JOIN TableSize size2
ON prod.Product_ID = size2.Product_ID
AND size2.Code = 'Size2'
INNER JOIN TableSize size4
ON prod.Product_ID = size4.Product_ID
AND size4.Code = 'Size4'
You may still need to change some columns and table names around.. Not sure exactly what the size table is called.
If size2_price and size4_price field is integer, try this
select sizes.id,products.name, (select top 1 description from products as p where p.id=sizes.product_id) as description,
sum(case when sizes.size2_price is not null then sizes.size2_price else 0 end) as size2_price,
sum(case when sizes.size4_price is not null then sizes.size4_price else 0 end) as size4_price
from sizes
inner join products on sizes.product_id=products.
group by sizes.id, products.name
Hi, HuyBD,
Sorry, I get "The text, ntext, and image data types are invalid in this subquery or aggregate expression." Unfortunately desription column is text. I have thought about changing that.
You know, maybe I'm going at this the wrong way. I need to think harder about this and see if I can simplify. Thanks so much for everyone's help, let's put this on hold for a bit while I unscramble my brain. :)
Depend on you purpose, try convert text to varchar if possible
select sizes.id,products.name, (select top 1 cast(description as varchar) from products as p where p.id=sizes.product_id) as description,
sum(case when sizes.size2_price is not null then sizes.size2_price else 0 end) as size2_price,
sum(case when sizes.size4_price is not null then sizes.size4_price else 0 end) as size4_price
from sizes
inner join products on sizes.product_id=products.
group by sizes.id, products.name
SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
FROM [CHANGE THESE WORDS TO TABLE NAME WITH PRODUCTS KEEP BRACKETS] prod
INNER JOIN [CHANGE THESE WORDS TO TABLE NAME WITH SIZES KEEP BRACKETS] size2
ON prod.Product_ID = size2.Product_ID
AND size2.Code = 'Size2'
INNER JOIN [CHANGE THESE WORDS TO TABLE NAME WITH SIZES KEEP BRACKETS] size4
ON prod.Product_ID = size4.Product_ID
AND size4.Code = 'Size4'
This should work for you if you change the table names in the brackets above. Your products table that lists the "description" is called description above. if it is named differently in your environment change the prod.description to prod.COLUMNNAME (change columnname to proper column name)..
The codes for sizes are also assuming that the column name that has the text "Size4" or "Size2" is actually called "code" if it is not you need to change that in the "Size2.Code" and "Size4.code" change the word code to the actual column.
Lastly, it is also assuming that the Product_ID column is the same in each table.
hi again; you know what? I think I am confusing the issue, making it sound more complex than it really is; you guys have been phenomenal in your help so far; let's try one more thing. Here's the thing- this code below does exactly what I want it to do EXCEPT it doesn't relate the two tables:
<CFQUERY DATASOURCE="#request.maind
SELECT *
FROM products
WHERE active='1'
ORDER BY name
</CFQUERY>
<CFQUERY DATASOURCE="#request.maind
SELECT *
FROM sizes
</CFQUERY>
<tr><td><b>Menu Item</b></td><td><b>2 servings</b></td><td><b>4 servings</b></td></tr>
<tr><td><cfoutput query="get_products" startrow=1 maxrows=1><b>#name#:</b>
</cfoutput>
<cfoutput query="get_products" startrow=1 maxrows=1>#description#</c
That gives me this result, but as I said, the prices aren't right becasue the two tables aren't linked in the query:
Menu Item 2 servings 4 servings
Name and description of item one $4.50 $8.75
Name and description of item two $15 $29
etc...
Thanks, Mike, I ran this code:
<CFQUERY DATASOURCE="#request.maind
SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
FROM [products] prod
INNER JOIN [sizes] size2_price
ON prod.ID = sizes.Product_ID
AND sizes.size2_price = 'Size2'
INNER JOIN [sizes] size4_price
ON prod.ID = sizes.Product_ID
AND sizes.size4_price = 'Size4'
</CFQUERY>
And got these errors:
Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]The column prefix 'size2' does not match with a table name or alias name used in the query.
The error occurred in F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
7 : <link rel="stylesheet" href="../stylesheet.css">
8 : </head>
9 : <CFQUERY DATASOURCE="#request.maind
10 : SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
11 : FROM [products] prod
--------------------------
SQL SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price] FROM [products] prod INNER JOIN [sizes] size2_price ON prod.ID = sizes.Product_ID AND sizes.size2_price = 'Size2' INNER JOIN [sizes] size4_price ON prod.ID = sizes.Product_ID AND sizes.size4_price = 'Size4'
DATASOURCE magicwww
VENDORERRORCODE 107
SQLSTATE HY000
you need to change it to this:
SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
FROM [products] prod
INNER JOIN [sizes] size2
ON prod.ID = sizes.Product_ID
AND sizes.size2_price = 'Size2'
INNER JOIN [sizes] size4
ON prod.ID = sizes.Product_ID
AND sizes.size4_price = 'Size4'
hmmm. now I get:
Error Occurred While Processing Request
Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]The column prefix 'sizes' does not match with a table name or alias name used in the query.
The error occurred in F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
7 : <link rel="stylesheet" href="../stylesheet.css">
8 : </head>
9 : <CFQUERY DATASOURCE="#request.maind
10 : SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
11 : FROM [products] prod
--------------------------
SQL SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price] FROM [products] prod INNER JOIN [sizes] size2 ON prod.ID = sizes.Product_ID AND sizes.size2_price = 'Size2' INNER JOIN [sizes] size4 ON prod.ID = sizes.Product_ID AND sizes.size4_price = 'Size4'
DATASOURCE magicwww
VENDORERRORCODE 107
SQLSTATE HY000
But my table name is sizes.
grr.. sorry my bad I didn't realize this was changed to... try this now:
SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
FROM [products] prod
INNER JOIN [sizes] size2
ON prod.ID = size2.Product_ID
AND size2.size2_price = 'Size2'
INNER JOIN [sizes] size4
ON prod.ID = size4.Product_ID
AND size4.size4_price = 'Size4'
Mike, I'm sorry- what a pain this is turning out to be for you! Now I get:
Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]Error converting data type varchar to float.
The error occurred in F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
Called from F:\Inetpub\wwwroot\magicki
7 : <link rel="stylesheet" href="../stylesheet.css">
8 : </head>
9 : <CFQUERY DATASOURCE="#request.maind
10 : SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
11 : FROM [products] prod
query analyzer is a SQL Server tool.. It should definitely be on whatever machine your database is on.. Depending on if you are using MSDE or a full release..
Start --> Programs --> Microsoft SQL Server --> SQL Query Analyzer
or Start --> Run --> type "isqlw" and hit enter.
My guess is your Product ID and ID columns are different data types in the Products and Sizes table.. Can you find that information easier than query analyzer?
ok.. Code is what we wnat to use .. what is stored in "code" is it "size2" or "2" or "size2_price"?
whatever it is you need to use it here:
SELECT prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
FROM [products] prod
INNER JOIN [sizes] size2
ON prod.ID = size2.Product_ID
AND size2.code = 'Size2' -- replace size2 with whatever is in the code field
INNER JOIN [sizes] size4
ON prod.ID = size4.Product_ID
AND size4.code = 'Size4' -- replace size4 with whatever is in the code field
Here is another mistake I have been making, sorry, Mike:
I should have had this:
<tr><td><b>Menu Item</b></td><td><b>2 servings</b></td><td><b>4 servings</b></td></tr>
<tr><td><cfoutput query="get_products" startrow=1 maxrows=1><b>#name#:</b>
</cfoutput>
<cfoutput query="get_products" startrow=1 maxrows=1>#description#</c
Instead of this:
<tr><td><b>Menu Item</b></td><td><b>2 servings</b></td><td><b>4 servings</b></td></tr>
<tr><td><cfoutput query="get_products" startrow=1 maxrows=1><b>#name#:</b>
</cfoutput>
<cfoutput query="get_products" startrow=1 maxrows=1>#description#</c
So I used your query above, now I get Variable NAME is undefined. If I do this, adding prod.name:
SELECT prod.name, prod.description, size2.Price AS [Size 2 Price], size4.Price AS [Size 4 Price]
FROM [products] prod
INNER JOIN [sizes] size2
ON prod.ID = size2.Product_ID
AND size2.code = 'Size2' -- replace size2 with whatever is in the code field
INNER JOIN [sizes] size4
ON prod.ID = size4.Product_ID
AND size4.code = 'Size4' -- replace size4 with whatever is in the code field
Then I get the name and description, but this error:
Variable SIZE2_PRICE is undefined.
Business Accounts
Answer for Membership
by: matthewspatrickPosted on 2006-05-29 at 13:16:12ID: 16785780
Hi mel150,
Your question does not really make sense. If there are three records with the same name but the description columns
all have different entries, wouldn't you want to return all three records?
Regards,
Patrick