views:

44

answers:

2

Hello Experts,

I have a flat text file data which I import into a SQL Server table.

It creates and table with specified name along with multiple columns as per data file.

Now I need a query which will return the data and its count. e.g.

data file :

BREAD,MILK
BREAD,DIAPER,BEER,EGGS
MILK,DIAPER,BEER,COKE
BREAD,MILK,DIAPER,BEER
BREAD,MILK,DIAPER,COKE 
BREAD,ICE,MANGO
JUICE,BURGER

Result should be

BREAD | 5
MILK  | 4
DIAPER| 4

and so on.

A: 

This is a wild guess based on incomplete information:

select Item, count(*)
from Items
group by Item
RedFilter
Do you think I can achieve this using cursor or similar, because I don't know how much columns would be for different data files?
ANIL MANE
+1  A: 

At a guess at the requirement as would need to see your scheme, but, maybe something like this?

SELECT
    ItemValue,
    COUNT(*)
FROM
(
    SELECT
        Column1 ItemValue
    FROM
        DataTable
    UNION ALL
    SELECT
        Column2 ItemValue
    FROM
        DataTable
    UNION ALL
    SELECT
        Column3 ItemValue
    FROM
        DataTable
    UNION ALL
    SELECT
        Column4 ItemValue
    FROM
        DataTable
) UnionDataTable
Robin Day
what is UnionDataTable ?
ANIL MANE
It works with "DataTable group by ItemValue "
ANIL MANE
UnionDataTable is an Alias for the previous SELECT.
Claudia