-1

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.

shammeee
  • 1
  • 3

2 Answers2

0

Table_1 ID, someFields, Price, DiscountedPrice

Assumptions :

  1. DiscountedPrice is 0 when no discount is given
  2. 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 :

  1. DiscountedPrice is 0 when no discount is given
  2. 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
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