SQL: How to apply distinct only for one column out of many? -


i have 2 tables orders , customers. orders contains following columns: orderid, customerid. customers contains columns: customerid, customername. have few records customers not have orders. want unique customers (even not have orders) , orders.

i want using query:

select distinct(customername) orderid, orders.customerid, customername customers left join orders on orders.customerid = customers.customerid order customername; 

but replace orderid column values customername values. why should use following query desired result.

select orderid, customerid, customername  (     select distinct(customername) orderid, orders.customerid, customername     customers     left join orders on orders.customerid = customers.customerid     order customername ); 

is there errors first query? possible fix it?

orderid return more 1 record when customer has more 1 order.
distinct works on whole records, not individual fields if there's more 1 order customer show both records - customer same, order id different both records distinct.

if count orderid , group customername you'll unique customers , how many orders they've made:

select customername, count(orderid) count_of_orders customers left join orders on orders.customerid = customers.customerid group customername order customername   

enter image description here


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -