SQL : To get one of the duplicates by using a case statement - sql

I have a table in which there are two columns with duplicates.
id name classname description
-----------------------------
1 a aa aa:abcd
2 a Unknown Unknown
3 b bb unknown
4 c cc abcd
Now I have a select query where in I have to filter out all the duplicates and my description is shown as identifier, my result should be like this,
id name identifier
-----------------
1 a aa
2 b NULL
3 c NULL
where all the description having either without ':' as its char index should display as NULL or Unknown as Null.
I'm using the below select query to filter the duplicates in the 'name' column , but I'm unable to use the same query for description as I'm using case for obtaining result in order to trim my description 'aa: abcd' to aa
select distinct
id,
(select top 1 name
from table1 t
where t.name = t1.name
order by case t1.classname
when 'Unknown Tag Class' then 0
else 1
end
) name,
(case when charindex(':',Description)> 0
then substring(Description,1,(charindex(':',Description)-1))
end
) as Identifier
from table1 t1
In the above query I want to modify the case statement of description so that i can filter duplicates and also trim the values like "aa:abcd" to "aa" and put them in identifier column.
Need help on this.
this is the query i am using
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EXEC_REP_TransposedTagAttributes]')
AND type in (N'U'))
BEGIN
select distinct
[Att : 42674] as TagID
,Tagname
,isnull([Att : 14591],'-') as OriginatingContractor
,isnull([Att : 14594],'-') as System
,(case when charindex(':',TargetName)> 0 then
substring(TargetName,(charindex(':',TargetName)+1),len(TargetName))
end) as SystemDescription
,(case when charindex(':',TagClassDescription)> 0 then
substring(TagClassDescription,1,(charindex(':',TagClassDescription)-1))
end) as TagIdentifier
from EXEC_REP_TransposedTagAttributes t1
LEFT JOIN (SELECT SourceName, TargetName FROM EXEC_REP_Associations WHERE AssociationType = '3' and TargetClassName = 'SUB SYSTEM') b ON TagName = b.SourceName
where tagname='ZIH-210053' Order by [Att : 42674]
END
ELSE
select 'Reporting Database is being refreshed, please wait.' as errMsg
and the result i am geting is
TagID Tagname OriginatingContractor System SystemDescription TagIdentifier
2609005 ZIH-210053 Hyundai Heavy Industries (Topsides) 210 Slugcatcher NULL
2609005 ZIH-210053 Hyundai Heavy Industries (Topsides) 210 Slugcatcher ZIH
there are also rows which have tag identifier as null and donot have duplicates

select id,name,
CASE WHEN charindex(':',description)>0
THEN LEFT(description,charindex(':',description)-1)
ELSE NULL
END as identifier
from t as t1
where description like '%:%'
or NOT EXISTS (select * from t where t.id<>t1.id and t.name=t1.name);
SQLFiddle demo

Related

Compare two columns in SQL

I'm new to SQL and have very basic queries in GCP.
Let's consider this table below:
Name
B
C
Arun
1234-5678
1234
Tara
6789 - 7654
6789
Arun
4567
4324
Here, I want to compare column B and C and if they match then give 1 else 0 in column same and else different (which we have to create).
So here the catch:
if column B has 1234-5678 and column C has 1234, then the column should match considering only the number before the "-" in the value.
The output should be :
Name
B
C
same
different
Arun
1234-5678
1234
1
0
Tara
6789 - 7654
6789
1
0
Arun
4567
4324
0
1
Also, I want to count the values of 1 for each values in Name for same and different columns.
So far I've tried this:
SELECT
name,
b,
c ,
if(b = c, 1, 0) as same,
if (b!=c,1,0) as different,
count(same),
count(different)
From Table
using "MySQL" (will work almost same with SQL server as well) here's the possible solution.
Step 1) Setup table
CREATE TABLE Users (
Name varchar(50),
B varchar(50),
C varchar(50)
);
INSERT INTO Users
VALUES
('Arun', '1234-5678', '1234'),
('Tara', '6789-7654', '6789'),
('Arun', '4567', '4324');
Step 2) same & different columns
SELECT
Name, B, C,
CASE WHEN SUBSTRING_INDEX(B, "-", 1) = C THEN 1 ELSE 0 END as same,
CASE WHEN SUBSTRING_INDEX(B, "-", 1) <> C THEN 1 ELSE 0 END as different
FROM
Users
Step 3) Join both results to get total_same & total_different for each user
SELECT
Name,
SUM(CASE WHEN SUBSTRING_INDEX(B, "-", 1) = C THEN 1 ELSE 0 END) as total_same,
SUM(CASE WHEN SUBSTRING_INDEX(B, "-", 1) <> C THEN 1 ELSE 0 END) as total_different
FROM
Users
GROUP BY Name
Reference: SQL Fiddle
For the first step, you will need to SUBSTR the column b.
We start at position 1 and we want 4 characters (only works if there's only 4 characters before the '-').
With table2 as (
select name, b,c, same, different from (select name, b, c, case when (SUBSTR(b,1,4) = c)
then '1' else '0' end as same, case when(SUBSTR(b,1,4)!= c) then '1' else '0' end as different
from Table1
group by name, b,c))
The WITH clause can be used when you have complex query, and if you want to create a temporary table in order to use it after.
The Table2 give you this :
After the WITH clause, you will have the second step, the count of same / different per name :
Select table1.name,count(table2.same+table2.different) as total from table1
join table2 on (table2.name = table1.name and table2.b = table1.b)
group by table1.name;
The output give you the total per name (the name are group by, so in your example you will only have 2 rows, one for Arun with a total of 2 (same + different) and the other one with a total of 1)
So here's the entire code :
with table2 as (
select name, b,c, same, different from (select name, b, c, case when (SUBSTR(b,1,4) = c) then '1' else '0' end as same, case when(SUBSTR(b,1,4)!= c) then '1' else '0' end as different
From Table1
group by name, b,c))
select table1.name, table1.b, table1.c, count(table2.same+table2.different) as total from table1
join table2 on (table2.name = table1.name and table2.b = table1.b)
group by table1.name;

How do you select all rows when data occurs in one of the rows?

I have a table with data
|FormID|Name|
1 A
1 B
2 A
2 C
3 B
3 C
I am trying to query all rows where Name 'A' appears, however i also want to get all rows with the same FormID when the name occurs
For example
Select * from table where name = 'A'
resultset
|FormID|Name|
1 A
2 A
1 B
2 C
Right now i am just querying for the FormID values where the name occurs and then doing another query with the FormID number (Select * from table where formID in (1,2) ) but there must be a way to do this in one sql statement
You can use exists:
select t.*
from t
where t.name = 'A' or
exists (select 1
from t t2
where t2.formid = t.formid and t2.name = 'A'
);
Actually, the first condition is not necessary, so this suffices:
select t.*
from t
where exists (select 1
from t t2
where t2.formid = t.formid and t2.name = 'A'
);
Another approach:
SELECT formid, name
FROM forms
WHERE formid IN (SELECT formid FROM forms WHERE name = 'A')
ORDER BY name;
gives
formid name
---------- ----------
1 A
2 A
1 B
2 C
Because the subquery in the IN doesn't depend on the current row being looked at, it only has to be evaluated once, making it more potentially more efficient for large tables.

Querying a subset

I want to write an SQL query to find records which contain a particular column and from that subset want to find records which doesn't contain a some other value. How do you write a query for that?
cid id2 attribute
--------------------------------
1 100 delete
1 100 payment
1 100 void
2 100 delete
2 102 payment
2 102 void
3 102 delete
3 103 payment
In above example, I want to list cid for which payment and delete attributes exist but void attribute doesn't exist. So it should list out 3 from above example because it doesn't have void attribute.
Forgot to mention that there could be more attributes. However, I need to list out records for which delete and payment exist regardless of other attributes but void doesn’t.
I call this a "set-within-sets" query, because you are looking for particular sets of attributes within each cid.
I would express this with group by and conditions in the having:
select cid
from t
group by cid
having sum(case when attribute = 'payment' then 1 else 0 end) > 0 and
sum(case when attribute = 'delete' then 1 else 0 end) > 0 and
sum(case when attribute = 'void' then 1 else 0 end) = 0 ;
In some databases, you can simplify this with string aggregation -- assuming there are no duplicate attributes for cids. For instance, using the MySQL function:
select cid
from t
where attribute in ('payment', 'delete' 'void')
group by cid
having group_concat(attribute order by attribute) = 'delete,payment';
You can use conditional aggregation:
select cid
from tablename
where attribute in ('delete', 'payment', 'void')
group by cid
having
count(distinct attribute) = 2
and
sum(
case attribute
when 'void' then 1
else 0
end
) = 0
If there are not more attributes than these 3, then you can omit the WHERE clause.
See the demo.
Results:
| cid |
| --- |
| 3 |
I'm assuming that there are only three attributes, so the logic behind this query is:
First COUNT the number of attributes GROUP BY cid, and then LEFT JOIN the original table ON attribute is void. You should grab cid that has exactly 2 attributes and no void.
The original table is named as temp:
SELECT
subq2.result_cid
FROM (
SELECT
*
FROM (
SELECT
T.cid AS result_cid,
COUNT(T.attribute) AS count
FROM
temp AS T
GROUP BY
T.cid
) AS subq
LEFT OUTER JOIN temp AS T2 ON subq.result_cid = T2.cid AND T2.attribute = 'void'
) AS subq2
WHERE subq2.count = 2 AND subq2.id2 IS NULL
use corelated subquery by using not exists
select t1.* from tablename t1
where not exists( select 1 from tablename t2
where t1.cid=t2.cid and attribute='void'
)
and exists ( select 1 from tablename t2
where t1.cid=t2.cid
having count(distinct attribute)=2
)
and attribute in ('payment','delete')
demo online

Get Value from column depending on another column value in SQL

I have a table like this
id code text
100 1 short description 100
100 2 long descr 100
101 1 short description 101
Now I want to fetch data from text field depending on value in code field. if the code is 1 its short description and if code is 2 its long description.
My desire output is like this
id shortdescription longdescription
100 short description 100 long descr 100
101 short description 101 null
Try this:
SELECT id,
MAX(CASE WHEN code=1 THEN text END) as shortdescription,
MAX(CASE WHEN code=2 THEN text END) as longdescription
FROM TableName
GROUP BY id
Result in SQL Fiddle.
This query is same as:
SELECT id,MAX(shortdescription) as shortdescription,MAX(longdescription) as longdescription
FROM
(
SELECT id,
CASE WHEN code=1 THEN text END as shortdescription,
CASE WHEN code=2 THEN text END as longdescription
FROM TableName
) T
GROUP BY id
NB: To understand the working of this query, please execute the inner query first.
Result:
ID SHORTDESCRIPTION LONGDESCRIPTION
100 short description 100 long descr 100
101 short description 101 (null)
See result in SQL Fiddle.
I think the simplest approach is to treat this as conditional aggregation . . . put the case statement as an argument to the max() function:
select id,
max(case when code = 1 then text end) as ShortDescription,
max(case when code = 2 then text end) as LongDescription
from table t
group by id;
Create two subqueries with the requested where condition (code = 1|2), then join them together (full outer join if any of them could missing).
SELECT
COALESCE(SHORTDESC.id, LONGDESC.id) AS id
, SHORTDESC.[text] AS shortdesc
, LONGDESC.[text] AS longdesc
FROM (
SELECT
id, [text]
FROM
yourTable
WHERE
code = 1
) SHORTDESC
FULL OUTER JOIN (
SELECT
id, [text]
FROM
yourTable
WHERE
code = 2
) LONGDESC
ON SHORTDESC.id = LONGDESC.id

SQL CASE SUBQUERY COUNT

I have table with 2 col :
UID NAME
-----------
111 AAA
222 BBB
Customer will enter the name and I have to retrieve UID with respective value. If name won't present in the rows, it has to retrieve 000, not like no rows.
I am trying to write query like this:
SELECT
CASE UID
WHEN Count(*) = 0 THEN '000'
ELSE UID
END
FROM table1
WHERE NAME ='XXX'
Please help me in this regard. Thanks in advance...
If UID is an integer, then you need to take casts into account:
select coalesce(cast(max(uid) as char(3)), '000')
from table1
where name = 'XXX'
The cast is intended to be to the type of UID, which seems to be char(3) in your example.
When there are no matching rows, then the max() function returns NULL. The coalesce() turns this into the value you are looking for.
try this
select case
when max(id) is null then
0
else
max(id)
end
from table1
where name = 'b'
You have error in case
SELECT case when count(UID) = 0 THEN '000' ELSE UID end FROM table1 where name = 'XXX'
sqlfiddle :http://www.sqlfiddle.com/#!2/257ea/1