views:

70

answers:

2
+1  Q: 

Group by problem

I need to group by ,even if is there a difference in values for a column by converting to a text value and needs that text value to be included in select list

How is this possibe?

  SELECT col1,col2
   FROM
       (
         SELECT col1,col2 FROM table1
         UNION ALL
     SELECT col1,col2 FROM table2
         UNION ALL
         SELECT col1,col2 FROM table3

       )tbl
  GROUP BY  tbl.col1
           ,tbl.col2

here col2 may or may not be a text value or int, if it is text value i need to convert all other col2 ints to text and do a group by

See this example :

Table 1
-------
A B  3   C 
A B  var C


Table 2
-------
UNION ALL

B B  3   C 
B B  var C


Table 3
-------
UNION ALL

B B  3   C 
B B  3   C

Result of each table should be 
-------

Table 1
-------
A B  var C (since there is a var in the any of the row in that column in that table)

Table 2
-------
B B  var C (since there is a var in the any of the row in that column in that table)

Table 3
-------
B B  3   C  (here it remains 3 since there is not value call var in any of the row in that column)

Result 
------
A B  var C
B B  var C  (since there is a var in the any of the row in that column in that table)
A: 

This seems a little to easy, maybe i am missing something? Are you just looking to do a CAST on col2?

SELECT  col1
       ,CAST(col2 AS nvarchar(100)) as Col2
       ...etc
slugster
i need to eliminate that coulumn from group by and need to be in select list ...what to do?
@SmartestVEGA: I still don't understand. What means "eliminate that column from the group by" and what means "need to be in select list"? Show us an example with values and the expected result.
Stefan Steinegger
what i need is ...suppose if i have several columnsand in that one column will always display different values ...(always)and i need to convert all those different value to a common text ....not a numberand i want to group by that ...how is it possible?
A: 

tAKE THIS:

 SELECT col1,col2
   FROM
       (
         SELECT col1,col2 FROM table1
         UNION ALL
     SELECT col1,col2 FROM table2
         UNION ALL
         SELECT col1,col2 FROM table3

       )tbl
where col2 not like '3%'
  GROUP BY  tbl.col1

The Result look like your question:

A B var C

B B var C

Ice