Forum

How to write a quer...
 
Notifications
Clear all

How to write a query to count the number of invoices?


kumar BI
Posts: 18
 kumar BI
Topic starter
(@kumar BI)
Joined: 1 year ago

Can you give any example how to write a query to count the number of invoices? 

1 Reply
VK
Posts: 17
 VK
(@vinodkrsetty5895)
Active Member
Joined: 1 year ago

Let us see how to write a query to count the number of invoices with practical examples.

 

Invoice ID is a unique ID or a table column that exists in the Products table.

 

It is associated with the product information.

 

One invoice may have multiple products.

 

See the example below I have used the Current_Products table.

 

Select * from Current_Products

 

Invoice_ID ProductID            ProductName                   Sales
1                      A                          Mobile                          10000
2                      B                          Desktop                        20000
3                      C                          Printer                         15000
4                      D                          Headset                       2500
5                      E                          Speakers                      1300

 

Using this table I want to count the number of invoices.

 

For that, I can write a query like this

 

Select Count(Invoice_ID) from Current_Products

 

Results would be

 

Count(Invoice_ID)
5

 

Remember one thing here Invoice will be generated only when the product is sold.

 

Let us take the sales invoice detail table here.

 

Table: Sales_Invoice_Detail

 

Invoice_Date_ID Invoice_ID  Product_ID     Quantity    Unit_Discuount
1                           1                             A                     1              0
2                           1                             A                     1              0
3                           2                             B                     1              0

 

Let us say I want to find out the list of all products that have not sold.

 

For that, I can write this SQL Query. 

 

Select * from Current_Products Where
Product_ID NOT IN (Select distinct Product_ID from Sales_Invoice_Details)

 

Here output would be

 

Invoice_ID                      ProductID                 ProductName              Sales
4                                       D                                Headset                      2500
5                                       E                                 Speakers                    1300

I hope it is useful.

 

 

 

 

Reply
Share: