user3588635
user3588635

Reputation: 21

Trying to add a count to distinct statement

I have two columns:

A column that will contain data.

So Column 1 has number of records. These records in column 1 i have to narrow it down to a distinct. I then want to know how many of the records in column 2 contain the data from coulmn 1.

So let's say:

Column 1 - AABB 
Column 2 - 111, 112.

How many of these in column 2 contain AABB just as a example and a count possibly on column 2. I also need to distinct column 1.

EDIT: (originally in an answer)

Sorry my question was phrased a bit silly.

So I want distinct column 1.

Then in column 2, i want to know the amount of records that are in column 2 contained under each distinct record stated in column 1. So that be a count.

Upvotes: 0

Views: 49

Answers (2)

Claudio
Claudio

Reputation: 250

This might do it, assuming your table is like this and the data is like below:

CREATE TABLE dbo.DataTbl (
Product VARCHAR(100),
Qty SMALLINT
);

GO

INSERT INTO dbo.DataTbl
VALUES ('Chocolate bars', 3),
   ('Chocolate bars', 5),
   ('Milk', 2),
   ('Milk', 4),
   ('Crisps', 6),
   ('Fizzy Drinks', 10);

---Your desired query
SELECT DISTINCT (Product) AS 'Product', SUM(Qty) AS 'TotalQty'
         FROM dbo.DataTbl
          GROUP BY Product

Upvotes: 0

Ganesh Jadhav
Ganesh Jadhav

Reputation: 2848

As far as I understand your question, I think you need this:

SELECT col1, COUNT(*)
FROM tbl
GROUP BY col1

Upvotes: 1

Related Questions