I have two tables the first table have regular price and discounted price. I'm trying to count how many regular price and discounted price and put the values in second table.
            Asked
            
        
        
            Active
            
        
            Viewed 87 times
        
    2 Answers
0
            
            
        Table_1 ID, someFields, Price, DiscountedPrice
Assumptions :
- DiscountedPrice is 0 when no discount is given
- Table 2 holds the number of elements with/without discount;
Table_2 Kind, CountValue, CountedOn
insert into Table_2 (Kind, CountValue, CountedOn)
Select 'Price', count(Id), GetDate()
from table1
Where DiscountedPrice=0
Union
Select 'Discount', count(Id), GetDate()
from table1
Where DiscountedPrice>0
==> Table_2
| Kind | CountValue | CountedOn | 
|---|---|---|
| 'Price' | 7 | 31-05-201 | 
| 'Discount' | 3 | 31-05-201 | 
Assumptions :
- DiscountedPrice is 0 when no discount is given
- Table 2 holds the number of elements with/without discount on given Date;
Table_2 Price, Discounted, CountedOn
insert into Table_2 (Price , Discounted, CountedOn)
Select ( select count(Id) from table1 Where DiscountedPrice=0 ) as Price , 
       ( select count(Id) from table1 Where DiscountedPrice>0 ) as Discounted, 
      getdate()
==> Table_2
| Price | Discounted | CountedOn | 
|---|---|---|
| 9 | 1 | 25-05-201 | 
| 7 | 3 | 31-05-201 | 
 
    
    
        Poor Beggar
        
- 51
- 9
0
            
            
        you can use count query to count the regular prices and discount price from the particular table like this
      SELECT COUNT(*)
      FROM   employees
      ) AS regular_prices,
      (SELECT COUNT(*)
      FROM   departments
      ) AS discount_price
FROM table1
note: you ca also add where clause for particular condition base filtering.
and that count you can put using insert query to another table which is table2.
 
    
    
        Ihtisham Tanveer
        
- 338
- 4
- 15
