Question

New to SQL and I am struggling with a query to have multiple values from a column counted and added together. Here is the query

select count (*) as NEWCOL
from table1
where COL1 = 'val1' and COL2 = 'val1' and COL3='val1' or 'val2'
Was it helpful?

Solution

your where condition looks flawed:

select count (*) as NEWCOL
  from table1
  where COL1 = 'val1'
    and COL2 = 'val1'
    and (COL3 = 'val1' or COL3 = 'val2')
      ;

OTHER TIPS

Try this:

SELECT 
  COUNT(y.*) as NewCol
  ,x.[allfields] as WhatyouWantitToBe
FROM 
  table1 y
OUTER APPLY
  (SELECT 
     x.col1 + ' ' + x.col2 + ' ' + x.col3 AS [allFields]
   FROM
     table1 x
   WHERE
     x.col1 = y.col1
     AND
       x.col2 = y.col2
     AND
       x.col3 = y.col3) AS x
WHERE 
  y.COL1 = 'val1'
  AND y.COL2 = 'val1'
  AND (y.COL3 = 'val1' 
       OR 
       y.COL3 = 'val2')

And then again, that's what can be done with what you mentioned. A concatenation of varchar field as well as a count.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top