Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

group by ins MS SQL query

$
0
0

I am searching a table that has customer contracts in it.

item#,cust#,CustItem#

So, each item could have several customers or none. I also join to the item table to check for a status of A for active item.

I want to search the table and only return item records that do not have 2 specific cust#'s

select 'Cust Does Not Exist' as status,
item.item as item,
item.description as description,
LTRIM(Rtrim(itemcust.cust_num)),
itemcust.cust_item

From item
inner join itemcust on item.item = itemcust.item
Where
(item.stat = 'A') and
(LTRIM(Rtrim(itemcust.cust_num))<> '106066' or LTRIM(Rtrim(itemcust.cust_num))<> '118691')
group by item.item,item.description,LTRIM(Rtrim(itemcust.cust_num)),itemcust.cust_item

The problem is I get back multiple entries for each item. I just want 1 line per item.



Viewing all articles
Browse latest Browse all 4871

Trending Articles



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