简体   繁体   中英

Concatenate Two Fields in SQL

I have Sales Invoice #, Invoice Date for each Product. I want to only show unique order per Product. I know Select distinct will pick only unique Invoices# and Invoice Date. Please show me how I can concatenate these two fields to show Unique Order Per Product.

eg:

Sales Invoice # | Invoice Date | Products                                       
  2122          | 9/24/2019    | A           
  2123          | 9/25/2019    | A                                           
  2122          | 9/24/2019    | A    
  2124          | 9/25/2019    | B 
  2125          | 9/25/2019    | B  
  2125          | 9/25/2019    | B 

I want output which shows two columns only,

# of Orders  | Products       
           2 |A
           2 |B 

I am new to SQL please tell me the query to merge Sales # and Invoice Date as Orders, as I want to visualize this in a chart.

You seem to be looking for count(distinct) :

select product, count(distinct sales_invoice_id)
from t
group by product;

In addition to Gordon's answer,

I suggest you do "alter table" and set unique key for Sales Invoice # ;

this way you won't have unnecessary duplicates in your table.

then

select * where product='B'

give you same number of rows as # of Orders of B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM