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'
your where condition looks flawed:
select count (*) as NEWCOL
from table1
where COL1 = 'val1'
and COL2 = 'val1'
and (COL3 = 'val1' or COL3 = 'val2')
;
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.
Related
I have a col1 in tbl1 which has data as under:
C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\
now I want to insert a '\' after C:\ABC\1245_PQR\125\xyz\ROW and before '20MAOSAD12\'
and the data format is same but it changes in all row, for example -
C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\
C:\ABC\3456_ADR\515\xpo\ROWadMAOSAD23\
C:\ABC\1547_DFR\255\RDS\ROW14SDFS15\
Can someone please help.
Thanks
You can use replace function
SELECT REPLACE('C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\', '\ROW', '\ROW\')
To be more precise
SELECT REPLACE(col1, '\ROW', '\ROW\') from tbl1
Alternatively you could consider the STUFF operator:
STUFF ( character_expression , start , length , replaceWith_expression )
SELECT STUFF('C:\ABC\1245_PQR\125\xyz\ROW20MAOSAD12\',28,0, '\')
SELECT STUFF('SomeColumn',28,0, '\') FROM SomeTable WHERE SomeColumn=SomeValue
Also, I made this one - a bit lengthy
-- Step 1
Select all rows that have ...xyz.... in col1 and col2 is xyz
select * from tbl where col2 = 'row' and col1 like '%xyz%'
-- Step 2
-- From the rows above, calculate the start index of ...xyz....... in col1
select charindex('xyz', col1) from (select col1 from tbl where col2 = 'xyz' and col1 like '%xyz%') tmp
-- Step 3
-- Split the col1 from CharIndex + 3 (size of xyz)
select left(col1, charindex('xyz', col1) + 2 ), substring(col1, charindex('xyz', col1) + 3, LEN(col1) ), ( left(col1, charindex('xyz', col1) + 2 ) + '\' + substring(col1, charindex('xyz', col1) + 3, LEN(col1) ))
from (select col1 from tbl where name = 'xyz' and col1 like '%xyz%') tmp
-- Step 4
-- Update !!
update tbl
SET col1 = ( left(col1, charindex('xyz', col1) + 2 ) + '\' + substring(col1, charindex('xyz', col1) + 3, LEN(col1) ))
where col2 = 'xyz' and col1 like '%xyz%' and col1 not like '%xyz\%'
I would like to take cells in every row and make them into a string of names... My method already deals with casing.
For example, the table;
'john' | | 'smith' | 'smith'
'john' | 'paul' | | 'smith'
'john' | 'john' | 'john' |
returns:
'john smith'
'john paul smith'
'john'
This would need to run postgreSQL 8.2.15 of postgres so I can't make use of potentially useful functions like CONCAT, and data is in a greenplum db.
Alternatively, a method to directly delete duplicate tokens in a list of strings would let me achieve the larger objective. For example:
'john smith john smith'
'john john smith'
'smith john smith'
returns
'john smith'
'john smith'
'smith john'
The order of the tokens is not important, as long as all the unique values are returned, once only.
Thanks
Normalize your table structure, select distinct name values from that table, create a function to aggregate strings (see, e.g., How to concatenate strings of a string field in a PostgreSQL 'group by' query?), and apply that function. Except for the aggregate function creation, this could all be done in a single statement or view.
I've come up with a solution for you! :)
The following query returns the four columns (which I named col_1,2,3and 4) and removes the duplicates by joining the test_table with itself.
Here is the code:
SELECT t1.col_1, t2.col_2, t3.col_3, t4.col_4
FROM (
SELECT id, col_1
FROM test_table
) AS t1
LEFT JOIN (
SELECT id, col_2
FROM test_table
) as t2
ON (t2.id = t1.id and t2.col_2 <> t1.col_1)
LEFT JOIN (
SELECT id, col_3
FROM test_table
) as t3
ON (t3.id = t1.id and t3.col_3 <> t1.col_1 and t3.col_3 <> t2.col_2)
LEFT JOIN (
SELECT id, col_4
FROM test_table
) as t4
ON (t4.id = t1.id and t4.col_4 <> t1.col_1 and t4.col_4 <> t2.col_2 and t4.col_4 <> t3.col_3);
If you want to obtain the final string, you just substitute the "SELECT" row with this one:
SELECT trim(both ' ' FROM (COALESCE(t1.col_1, '') || ' ' || COALESCE(t2.col_2, '') || ' ' || COALESCE(t3.col_3, '') || ' ' || COALESCE(t4.col_4, '')))
this should work with your version of postgres, according with the docs:
[for the trim and concatenation functions]
https://www.postgresql.org/docs/8.2/static/functions-string.html
//***************************************************
[for the coalesce function]
https://www.postgresql.org/docs/8.2/static/functions-conditional.html
Please let me know if I've been of help :)
P.S. Your question sounds like a bad database design: I would have those columns moved on a table in which you could do this operation by using a group by or something similar. Moreover I would do the string concatenation on a separate script.
But that's my way of doing :)
I would do this by unpivoting the data and then reaggregation:
select id, string_agg(distinct col)
from (select id, col1 from t union all
select id, col2 from t union all
select id, col3 from t union all
select id, col4 from t
) t
where col is not null
group by id;
This assumes that each row has an unique id.
You can also use a giant case:
select concat_ws(',',
col1,
(case when col2 <> col1 then col2 end),
(case when col3 <> col2 and col3 <> col1 then col3 end),
(case when col4 <> col3 and col4 <> col2 and col4 <> col1 then col4 end)
) as newcol
from t;
In ancient versions of Postgres, you can phrase this as:
select trim(leading ',' from
(coalesce(',' || col1, '') ||
(case when col2 <> col1 then ',' || col2 else '' end) ||
(case when col3 <> col2 and col3 <> col1 then ',' || col3 else '' end),
(case when col4 <> col3 and col4 <> col2 and col4 <> col1 then ',' || col4 else '' end)
)
) as newcol
from t;
Select * from TABLENAME WHERE "CLAUSE"
It will print the result in a single row.
Col 1 Col 2 ...... Col N
Val 1 Val 2 ...... Val N
I need
Col 1 Val 1
Col 2 Val 2
.
.
.
Col N Val N
A little time consuming to do on a regular basis, but:
select COL_NAME, COL_DATA
from (SELECT * FROM table_name
WHERE clause)
unpivot ( COL_NAME FOR COL_DATA IN ( COL1 as 'COL1'
,COL2 as 'COL2'
,COL3 as 'COL3'
,COL4 AS 'COL4')
)
Bear in mind that you also need to cast all of the values to the same data-type as Oracle won't mix datatypes in the same column, so if COL1-3 are number, but COL4 is varchar, then you would
select COL_NAME, COL_DATA
from (SELECT * FROM table_name
WHERE clause)
unpivot ( COL_NAME FOR COL_DATA IN ( TO_CHAR(COL1) as 'COL1'
,TO_CHAR(COL2) as 'COL2'
,TO_CHAR(COL3) as 'COL3'
,COL4 AS 'COL4')
)
select 'col 1', col1 from TABLENAME WHERE "CLAUSE"
UNION ALL
select 'col 2', col2 from TABLENAME WHERE "CLAUSE"
UNION ALL
...
select 'col n', coln from TABLENAME WHERE "CLAUSE"
order by 1
I need to do a SELECT like :
Select
col1, col2, (expression) as colA
from tablex ,
but the expression depends on an external variable #per so, the select would be something like :
SELECT
col1, col2,
case
#per = 1 then (col00 + col01) as colA
#per = 2 then (col00 + col01 + col02) as colA
#per = 3 then (col00 + col01 + col02 + col03) as colA
end
FROM tableX
How do I do this?
Thanks
This is the code assuming you are using T-SQL:
SELECT col1,col2,
case
WHEN #per =1 then (col00+col01)
WHEN #per =2 then (col00+col01+col02)
WHEN #per =3 then (col00+col01+col02+col03)
end as colA
FROM tableX
SELECT col1,col2, colA =
CASE #per
WHEN 1 THEN (col00+col01)
WHEN 2 THEN (col00+col01+col02)
WHEN 3 THEN (col00+col01+col02+col03)
ELSE 0
END
FROM tableX
I have a form that is giving me information like INPUT1, INPUT2. I need to select from COL1 and COL2 where COL1 = INPUT1, COL2 = INPUT2 or the other way around, COL1 = INPUT2, COL2 = INPUT1.
SELECT * FROM table WHERE
(COL1 = INPUT1 AND COL2 = INPUT2)
OR (COL1 = INPUT2 AND COL2 = INPUT1);
SELECT * FROM table
WHERE ( COL1=INPUT1 AND COL2=INPUT2 )
OR ( COL1=INPUT2 AND COL2=INPUT1 )
select * from table_name
where ( COL1=INPUT1 AND COL2=INPUT2 ) OR ( COL1=INPUT2 AND COL2=INPUT1 ) ;