Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL Query help please - ordering based off a different table

$
0
0

Hello,

I'm not an experienced SQL coder so please excuse me if this is not possible or simple. I have 2 tables. The first with a bunch of product names, prices, codes, descriptions, etc. The second table has the product inventory counts. What I want to do in the sample below is sort the results based off of the product name first but only if there is inventory of it then the out of stock items both alphabetical.

Thanks for any help its much appreciated...

select * from Product where (select count(where brand and name match the * from the parent select)) from inventory>0) order by Brand, Name union select * from Product where (select count(where brand and name match the * from the parent select)) from inventory=0) order by Brand, Name

TableName: Product

Brand | Name | Price | Image

Brand1 | Item1 | 9.99 | Image1

Brand1 | Item2 | 9.99 | Image2

Brand2 | Item3 | 9.99 | Image3

Brand4 | Item2 | 9.99 | Image 4

TableName: Inventory

Brand | Name | Status (1 is in stock)

Brand1 | Item1 | 1

Brand1 | Item1 | 1

Brand1 | Item1 | 1

Brand1 | Item1 | 1

Brand2 | Item2 | 1

Brand2 | Item2 | 1


Life moves pretty fast. If you don't stop and look around once in a while, you could miss it.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>