Hi I have a table name "T1' with a column name "Con". I need to find Unique and Repeat through additional columns as Unique and Repeat comparing consicutive rows. If row match with immediate row then repeat column will show "1" or else "0" and if row doesn't match with immediate row then Unique column will show "1" or else "0".
Given your sample data, you don't need to look at the "next" row. This logic does what you want:
select t1.con,
iif(cnt = 1, 1, 0) as is_unique,
iif(cnt > 1, 1, 0) as is_repeat
from t1 inner join
(select t1.con, count(*) as cnt
from t1
group by t1.con
) as tt1
on t1.con = tt1.con;
Related
I've already found so many answers here but now I can't seem to find any to my specific problem.
I can't figure out how to select a value from a row depending on the value in different columns
with the below table, I want to achieve the following results.
in case the value in column stdvpuni = 1 then return values / contents from this row for the article (column art).
in case the value in column stdvpuni = 0 then return values / contents from the row where STDUNIABG = 1 for this article (column art).
You seem to want one row part art, based on the content of other rows. That suggests using row_number():
select t.*
from (select t.*,
row_number() over (partition by art order by stdvpuni desc, STDUNIABG desc) as seqnum
from t
) t
where seqnum = 1;
You don't specify what to do if neither column is 1. You might want a where clause (where 1 in (stdvpuni, STDUNIABG)) or another condition in the order by.
I do not know what values / contents is, but I suppose that's easy for you to figure out. So, I will focus on the way to select this:
SELECT
CASE
WHEN current.stdvpuni = 1 THEN 'values / contents of current row'
ELSE 'values / contents of other row'
END
FROM yourtable current
JOIN yourtable other
ON other.stdvpuni = 1;
Use your conditions with NOT EXISTS in the WHERE clause:
SELECT t1.*
FROM tablename t1
WHERE t1.STDVPUNI = 1
OR (
t1.STDVPUNI = 0 AND t1.STDUNIABG = 1
AND NOT EXISTS (SELECT 1 FROM tablename t2 WHERE t2.ART = t1.ART AND t2.STDVPUNI = 1)
);
I have a scenario where I have to search value of column 1 in first table to see whether it matches some value in another table.
This should continue in a loop until the last row on first table has been compared.
No loops needed. You can do this easily as a set based operation using exists()
select *
from FirstTable
where exists (
select 1
from SecondTable
where FirstTable.Column1 = SecondTable.Column1
);
To find the opposite, where the row in the first table does not have a match based on Column1, you can use not exists()
select *
from FirstTable
where not exists (
select 1
from SecondTable
where FirstTable.Column1 = SecondTable.Column1
);
If you want to identify which rows have a match and don't you can use:
select FirstTable.*
, MatchFound = case when x.Column1 is null then 'No' else 'Yes' end
, x.Column1
from FirstTable
outer apply (
select top 1
*
from SecondTable
where FirstTable.Column1 = SecondTable.Column1
) as x
I want to update a column. When the value is not in the list or in table or doesn't match, it will return "Not Account Related" and when the value is in the list, the row will become blank. So this is my query.
SELECT
CASE Project_Names
WHEN "List_Value_Name".[LK_ProjectNames] = "Project_Names".[1]
THEN ''
ELSE "Not Account Related"
END
FROM
[master].[dbo].[1]
Table is LK_ProjectNames with columns List_Value_Name & table 1 with column Project_Names.
If value in table 1 column Project_Names got in the table LK_ProjectNames column List_Value_Name, it will return blank. if not it will return Not Account Related
If you are storing project name in table1 in one row one project basis.
SELECT *,
CASE WHEN EXISTS(SELECT 1 FROM LK_ProjectNames l WHERE LTRIM(RTRIM(t.Project_Names)) =LTRIM(RTRIM(l.List_Value_Name)) ) THEN '' ELSE 'Not Account Related' END as RESULT
FROM table1 t
If you are storing project names as comma seperated in table1 .
SELECT *,
CASE WHEN EXISTS(SELECT 1 FROM LK_ProjectNames l WHERE LTRIM(RTRIM(t.Project_Names)) like '%'+LTRIM(RTRIM(l.List_Value_Name))+'%' ) THEN '' ELSE 'Not Account Related' END as RESULT
FROM table1 t
I have a table with column uuid and type. I want all the uuid's 'xxxxx' such that no rows have uuid = 'xxxxx' AND type = 'buy'.
This ends up the same as if you took all uuid's in the table, and then removed all uuid's that match SELECT uuid FROM table WHERE type = 'buy'.
I approach these problems using aggregation and a having clause:
select a_uuid
from table t
group by a_uuid
having sum(case when type = 'Purchase' then 1 else 0 end) = 0;
EDIT:
If you have a table with one row per a_uuid, then the fastest is likely to be:
select a_uuid
from adtbs a
where not exists (select 1 from table t where t.a_uuid = a.a_uuid and t.type = 'Purchase');
For this query, you want an index on table(a_uuid, type).
select a_uuid
from t
group by a_uuid
having not bool_or(type = 'Purchase')
http://www.postgresql.org/docs/current/static/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE
Suppose there is a Table with 2 columns and the following data:
ID : 1,2,3,4,5,6,7,8
DATA:a,a,a,a,a,a,a,b
There can be only one 1 row with DATA=b and multiple rows with DATA=a.
I need a query that will select only one row.If there is any row with DATA=b then we will select that and if there is no row with DATA=b then it will select the first row with DATA=a.
Please Help!!!!
SELECT *
FROM table
WHERE DATA in ('A', 'B')
ORDER BY CASE WHEN DATA= 'B' THEN 0 ELSE 1 END, ID
FETCH FIRST 1 ROWS ONLY