How to write a sql query for below requirement? - sql

AgencyId
SourceType
SourceCode
PropertyState
Code
a1002
NULL
Xyz1
NULL
test1
a1002
NULL
Xyz2
NULL
test2
a1002
NULL
Xyz3
NULL
test3
a1002
NULL
Xyz4
NULL
test4
a1002
NULL
Xyz5
NULL
test5
a1002
NULL
Xyz6
NULL
test6
a1002
NULL
Xyz7
NULL
test7
a1002
NULL
Xyz8
NULL
test8
a1002
NULL
Xyz9
NULL
test9
a1002
NULL
Xyz10
NULL
test10
a1002
NULL
Xyz11
NULL
test11
a1002
NULL
Xyz12
NULL
test12
a1002
NULL
Xyz13
NULL
test13
a1002
NULL
Xyz14
NULL
test14
a1002
NULL
Xyz15
NULL
test15
a1002
NULL
Xyz16
NULL
test16
a1002
NULL
Xyz17
NULL
test17
a1002
NULL
Xyz18
NULL
test18
a1002
NULL
Xyz19
NULL
test19
a1002
NULL
Xyz20
NULL
test20
a1002
NULL
Xyz21
NULL
test21
a1002
NULL
Xyz22
NULL
test22
a1002
NULL
Xyz23
NULL
test23
a1002
NULL
Xyz24
NULL
test24
a1002
NULL
Xyz25
NULL
test25
a1002
NULL
Xyz26
NULL
test26
a1003
NULL
Xyz27
FL
test27
a1003
NULL
Xyz28
NULL
test28
a1004
NULL
NULL
NULL
test29
a1005
NULL
NULL
NULL
test30
a1006
NULL
NULL
FL
test31
a1006
NULL
NULL
NULL
test32
a1007
NULL
NUL**L
NULL
test33
a1008
B
NULL
NULL
test34
a1008
O
NULL
NULL
test35
I have a table name test,in that there are 5 columns AgencyId,SourceType,SourceCode,PropertyStae,Code
Want to write sql query which will give Code as output base on following drilldown logic
First match by AgencyId,SourceCode,PropertyState if not then AgencyId,SourceCode if not then AgencyId,SourceType if not then AgencyId PropertyStae if not then SourceType PropertyState if not then only by AgencyId.
How to write sql query for this. Requirement is such that can not write sp nor functions. Kindly let me know about the solution

You could translate the requirement to a serie of OR & AND
For example
DECLARE
#AgencyId varchar(8) = 'a1002',
#SourceCode varchar(8) = 'Xyz1',
#SourceType char(1) = 'B',
#PropertyState char(2) = 'FL';
SELECT *
, CASE
WHEN AgencyId = #AgencyId
AND SourceCode = #SourceCode
AND PropertyState = #PropertyState
THEN 'match 1'
WHEN AgencyId = #AgencyId
AND SourceCode = #SourceCode
THEN 'match 2'
WHEN AgencyId = #AgencyId
AND SourceType = #SourceType
THEN 'match 3'
WHEN AgencyId = #AgencyId
AND PropertyState = #PropertyState
THEN 'match 4'
WHEN SourceType = #SourceType
AND PropertyState = #PropertyState
THEN 'match 5'
WHEN AgencyId = #AgencyId
THEN 'match 6'
ELSE 'no match'
END AS Match
FROM test
WHERE (AgencyId = #AgencyId AND SourceCode = #SourceCode AND PropertyState = #PropertyState)
OR (AgencyId = #AgencyId AND SourceCode = #SourceCode)
OR (AgencyId = #AgencyId AND SourceType = #SourceType)
OR (AgencyId = #AgencyId AND PropertyState = #PropertyState)
OR (SourceType = #SourceType AND PropertyState = #PropertyState)
OR (AgencyId = #AgencyId)
But those criteria can be simplified :
SELECT *
FROM test
WHERE (AgencyId = #AgencyId
OR (SourceType = #SourceType AND PropertyState = #PropertyState)
);
AgencyId
SourceType
SourceCode
PropertyState
Code
a1002
null
Xyz1
FL
test1
a1002
null
Xyz1
null
test2
a1002
B
null
null
test3
a1002
null
null
FL
test4
null
B
null
FL
test5
a1002
null
null
null
test6
Demo on db<>fiddle here

Related

how to concatenate multiple columns in oracle sql into single column only for matching condition

i have below 4 columns
empid | name | dept | ph_no
---------------------------------
123 | null | null | null
124 | mike | science | null
125 | null | physics | 789
126 | null | null | 463
127 | john | null | null
and i need to merge all 4 columns into single columns only for null values.
And i need something like below--
empid
------------
123 is missing name,dept,ph_no
124 is missing ph_no
125 is missing name
126 is missing name,dept
127 is missing dept,ph_no
This can be done with case expressions.
select empid,empid||' is missing '||
trim(',' from
(case when name is null then 'name,' else '' end||
case when dept is null then 'dept,' else '' end||
case when ph_no is null then 'ph_no' else '' end
)
)
from tbl
I agree with Vamsi and would like just to add a where clause so the "complete" ones won't be returned.
select empid,empid||' is missing '||
case when name is null then 'name,' else '' end||
case when dept is null then 'dept,' else '' end||
case when ph_no is null then 'ph_no' else '' end
from tbl
where (name is null or dept is null or ph_no is null);
You can also use the NVL2 function.
SELECT empid||' is missing '||NVL2(name, NULL, 'name, ') ||NVL2(dept, NULL, 'dept, ')||NVL2(ph_no, NULL, 'ph_no') empid
FROM table_

Loading XML File data into Hive tables

I want to load the XML file into hive columns, but I am getting NULL when triggering select query on hive table.
Can anyone help?
Create table statement
CREATE TABLE `clobtest_h`(
`id` double,
`subject` string,
`body` string,
`purge_id` double,
`purge_date` timestamp,
`s_retention_applied` string,
`d_primary_column` double)
ROW FORMAT SERDE
'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
STORED AS INPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
'hdfs://nameservice1/user/hive/warehouse/support.db/clobtest_h'
TBLPROPERTIES (
'COLUMN_STATS_ACCURATE'='false',
'last_modified_by'='root',
'last_modified_time'='1488897940',
'numFiles'='0',
'numRows'='-1',
'rawDataSize'='-1',
'totalSize'='0',
'transient_lastDdlTime'='1488897940')
Insert query
insert into clobtest_h values(2,'Testing issue','<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Mens">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Womens">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>',1234.0,'2017-03-07 20:15:04','N',6.0)
Select query on table, getting NULLs after first line fetching
"select * from support.clobtest_h"
Logging initialized using configuration in jar:file:/opt/cloudera/parcels/CDH-5.6.0-1.cdh5.6.0.p0.45/jars/hive-common-1.1.0-cdh5.6.0.jar!/hive-log4j.properties
OK
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-5.6.0-1.cdh5.6.0.p0.45/jars/parquet-pig-bundle-1.5.0-cdh5.6.0.jar!/shaded/parquet/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-5.6.0-1.cdh5.6.0.p0.45/jars/parquet-format-2.1.0-cdh5.6.0.jar!/shaded/parquet/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-5.6.0-1.cdh5.6.0.p0.45/jars/parquet-hadoop-bundle-1.5.0-cdh5.6.0.jar!/shaded/parquet/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-5.6.0-1.cdh5.6.0.p0.45/jars/hive-exec-1.1.0-cdh5.6.0.jar!/shaded/parquet/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/cloudera/parcels/CDH-5.6.0-1.cdh5.6.0.p0.45/jars/hive-jdbc-1.1.0-cdh5.6.0-standalone.jar!/shaded/parquet/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [shaded.parquet.org.slf4j.helpers.NOPLoggerFactory]
2.0 Testing issue <?xml version="1.0"?> NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL
NULL 1234.0 2017-03-07 20:15:04 NULL NULL NULL NULL
Time taken: 1.878 seconds, Fetched: 42 row(s)
Mar 8, 2017 1:37:37 PM WARNING: parquet.hadoop.ParquetRecordReader: Can not initialize counter due to context is not a instance of TaskInputOutputContext, but is org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl
Mar 8, 2017 1:37:37 PM INFO: parquet.hadoop.InternalParquetRecordReader: RecordReader initialized will read a total of 42 records.
Mar 8, 2017 1:37:37 PM INFO: parquet.hadoop.InternalParquetRecordReader: at row 0. reading next block
Mar 8, 2017 1:37:37 PM INFO: parquet.hadoop.InternalParquetRecordReader: block read in memory in 21 ms. row count = 42
I have set the below property in hive-site.xml, which resolved the issue.
<property>
<name>hive.query.result.fileformat</name>
<value>SequenceFile</value>
</property>

Eliminate NULL values

I have the following data:
A B C D E F
NULL 1122111 NULL 0 NULL XBK
9226978 NULL 0 NULL XGI NULL
NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL
Now I need to collapse that to a single row with the below results:
A B C D E F
9226978 1122111 0 0 XGI XBK
I have no idea where to get started. Please help.
SELECT MAX(A) AS A,MAX(B) AS B
FROM Table_Name
Try this:-
SELECT COALESCE(A,0), COALESCE(B,0), COALESCE(C,0), COALESCE(D,0), COALESCE(E,0), COALESCE(F,0)
FROM YOUR_TABLE;

How to fix this pivot query...?

I have this query:
with cte1 as (
select id,
row_number() over (partition by [Id] order by id) as row,
first_name + ' ' + last_name as [Contact Name]
from contacts
where is_company = 0 and is_active = 1
),
companyContacts as (
select * from cte1 where row < 6
)
select company.company_name,
c.[1] as contact_1,
c.[2] as contact_2,
c.[3] as contact_3,
c.[4] as contact_4,
c.[5] as contact_5
from contacts company
left join contact_company_relation_additional_information relation
on company.id = relation.company_id and relation.ContactCompanyRelation_IsActive = 1
left outer join
(select *
from companyContacts
pivot (min([Contact Name]) for row in ([1],[2],[3],[4],[5])) x
) c on c.id = relation.contact_id
where is_company = 1 and is_active = 1
order by company.company_name
That brings me the data this way:
company_name contact_1 contact_2 contact_3 contact_4 contact_5
Analist Ori Reshef NULL NULL NULL NULL
Analist Ben Gurion NULL NULL NULL NULL
Analist Ofer Jerus NULL NULL NULL NULL
Bar Net Maya Leshe NULL NULL NULL NULL
Bar Net Yossi Farc NULL NULL NULL NULL
Bar Net Dima Brods NULL NULL NULL NULL
Here for some reason the contacts are in different rows and only in column: "contact_1".
But I need to have only one row ro each company name, and have the contacts in 5 diferent columns. Like this:
company_name contact_1 contact_2 contact_3 contact_4 contact_5
Analist Ori Reshef Ben Gurion Ofer Jerus NULL NULL
Bar Net Maya Leshe Yossi Farc Dima Brods NULL NULL
Can someone tell me how can I fix this query to bring me the data as I need it...?
My table struct is:
table Contacts: [id], [is_company], [first_name], [last_name], [company_name]
table contact_company_relation: [id], [company_id], [contact_id]
Sample Data:
table contacts:
id is_company first_name last_name company_name
1 True NULL NULL Analist
2 True NULL NULL Bar Net
3 False Ori Reshef NULL
4 False Ben Gurion NULL
5 False Ofer Jerus NULL
6 False Maya Leshe NULL
7 False Yossi Farc NULL
8 False Dima Brods NULL
table contact_company_relation:
id company_id contact_id
1 1 3
2 1 4
3 1 5
4 2 6
5 2 7
6 2 8
The problem that you are having is with the following line:
row_number() over (partition by [Id] order by id) as row,
This is creating a unique number for each row in your contacts table but you are partitioning the table by the id column which appears to be a unique value already for each row.
You should be partitioning the data based on the number of rows that exist in the contact_company_relation table instead.
I also would alter the code to something like the following:
select company_name,
Contact1, Contact2, Contact3,
Contact4, Contact5
from
(
select c.first_name + ' ' + c.last_name as contact_name,
comp.company_name,
'Contact'+
cast(row_number() over(partition by ccr.company_id
order by ccr.contact_id) as varchar(1)) row
from contacts c
inner join contact_company_relation ccr
on c.id = ccr.contact_id
inner join contacts comp
on ccr.company_id = comp.id
) d
pivot
(
max(contact_name)
for row in (Contact1, Contact2, Contact3,
Contact4, Contact5)
) p;
See SQL Fiddle with Demo. This gives a result:
| COMPANY_NAME | CONTACT1 | CONTACT2 | CONTACT3 | CONTACT4 | CONTACT5 |
|--------------|------------|------------|------------|----------|----------|
| Analist | Ori Reshef | Ben Gurion | Ofer Jerus | (null) | (null) |
| Bar Net | Maya Leshe | Yossi Farc | Dima Brods | (null) | (null) |

Selecting a specific value from 3 different colums under different rules minimum or middle in SQL server

I have a (hopefully) little problem in SQL where I need to query back a single value from 3 columns in a new column.
Here is what my table looks like :
Name of the issuer #ofratings Agency1 Agency2 Agency3
AAA 3 22 22 24
BBB 3 22 24 28
CCC 2 16 12 NULL
DDD 2 16 16 NULL
EEE 1 NULL 3 NULL
Now my problem... I'll try to be has clear as possible feel free to tell me if I an unclear.
I need to create a new column where I would bring back the resulting agency value following these rules
IF I have 3 agency quotes and none are identical, then I want the middle one (Issuer BBB in the example).
If I have 3 agency quotes and 2 are identical then I want that one in the new column (Issuer AAA in the example.
If I have 2 agency quotes and they are identical, then I want either to be brought back in my new column (Issuer DDD in the example).
If I have 2 agency quotes and both are different then I want the minimum between the 2(Issuer CCC in the example).
If I have only 1 agency quote then I want that one brought back (Issuer EEE in the example).
Mind you I can just has easily have agency 1 and 3 be identical in the Issuer AAA and so forth.
I did a search on getting the min values from 3 columns and got fou
What's the best way to select the minimum value from several columns?
The problem is I tried the CASE WHEN THEN example given but SQL allows a maximum of 10 levels on nesting in a case argument and my rule makes me bust that :S
Any help would be greatly appreciated.
Thank you
EDIT for #Fabien the Solution
HEre is the Select function in question
Mind you that S&P is Agency 1 in my example, Moodys is agency2 and DBRS is agency3
Select
no.issuer_cd,
count(distinct(no.Rater_CD)) as 'norating',
SP.rating_rank as 'S&P',
MO.rating_rank as 'Moodys',
DB.rating_rank as 'DBRS'
from csm_issuer_rating r
left join
(select
no.issuer_cd,
no.rater_cd
from csm_issuer_rating no
where no.rater_cd in ('M_SP_BOND','M_DBRS_BOND','M_MOODY_BOND')) as no
on r.issuer_cd = no.issuer_cd
left join
csm_issuer i on i.issuer_cd = no.issuer_cd
Left join
csm_rater_rating rr on rr.rater_cd = no.rater_cd
left join
( select
r.issuer_cd,
r.rating_cd,
rr.rating_rank
from csm_issuer_rating r
left join csm_rater_rating rr on rr.rating_cd = r.rating_cd
where rr.rater_cd = 'M_SP_BOND' and r.rater_cd = 'M_SP_BOND'
) as SP
on i.issuer_cd = sp.issuer_cd
left join
( select
r.issuer_cd,
r.rating_cd,
rr.rating_rank
from csm_issuer_rating r
left join csm_rater_rating rr on rr.rating_cd = r.rating_cd
where rr.rater_cd = 'M_MOODY_BOND' and r.rater_cd = 'M_MOODY_BOND'
) as MO
on i.issuer_cd = mo.issuer_cd
left join
( select
r.issuer_cd,
r.rating_cd,
rr.rating_rank
from csm_issuer_rating r
left join csm_rater_rating rr on rr.rating_cd = r.rating_cd
where rr.rater_cd = 'M_DBRS_BOND' and r.rater_cd = 'M_DBRS_BOND'
) as DB
on i.issuer_cd = db.issuer_cd
group by
no.issuer_cd,
SP.rating_rank,
MO.rating_rank,
DB.rating_rank)
SQL Fiddle
MS SQL Server 2008 Schema Setup:
CREATE TABLE Table1
(issuer_cd varchar(3), norating int,
"S&P" varchar(4), Moodys varchar(4), DBRS varchar(4))
;
INSERT INTO Table1
(issuer_cd, norating, "S&P", Moodys, DBRS)
VALUES
('AAA', 3, '22', '22', '24'),
('BBB', 3, '22', '24', '28'),
('111', 3, '26', '24', '28'),
('CCC', 2, '16', '12', NULL),
('DDD', 2, '16', '16', NULL),
('EEE', 1, NULL, '3', NULL)
;
Query 1:
SELECT issuer_cd,
CASE when ("S&P" is not null and
Moodys is not null) or
("S&P" is not null and
DBRS is not null) or
(Moodys is not null and
DBRS is not null) then (
CASE when "S&P" between isnull(Moodys,-1) and
DBRS then "S&P"
when Moodys between isnull("S&P",-1) and
DBRS then Moodys
when DBRS between isnull("S&P",-1) and
Moodys then DBRS
when "S&P" between isnull(DBRS,-1) and
Moodys then "S&P"
when Moodys between isnull(DBRS,-1) and
"S&P" then Moodys
when DBRS between isnull(Moodys,-1) and
"S&P" then DBRS
when "S&P" = Moodys or
Moodys = DBRS then Moodys
when "S&P" = DBRS then DBRS
END
)
when ("S&P" is null and
Moodys is null) then DBRS
when (Moodys is null and
DBRS is null) then "S&P"
when ("S&P" is null and
DBRS is null) then Moodys
END
FROM Table1
Results:
| ISSUER_CD | COLUMN_1 |
|-----------|----------|
| AAA | 22 |
| BBB | 24 |
| 111 | 26 |
| CCC | 12 |
| DDD | 16 |
| EEE | 3 |
EDIT :
May be you can try something like this :
IF OBJECT_ID(N'tempdb..#mytemp', N'U') IS NOT NULL
DROP TABLE #mytemp;
/* Your SELECT */
Select
no.issuer_cd,
count(distinct(no.Rater_CD)) as 'norating',
SP.rating_rank as 'S&P',
MO.rating_rank as 'Moodys',
DB.rating_rank as 'DBRS'
INTO #mytemp
FROM ...
/* My SELECT */
SELECT ....
FROM #mytemp;
Replace, tablename with your table name :
select *, (case when agency1<>agency2 and agency1<>agency3 then agency2
when agency1=agency2 and agency1<>agency3 then agency3
when agency1<>agency2 and agency3=agency2 then agency1
when agency3<>agency2 and agency3=agency1 then agency2
when agency1 is null and agency2=agency3 then agency2
when agency2 is null and agency1=agency3 then agency1
when agency3 is null and agency1=agency2 then agency1
when agency1 is null and agency2<agency3 then agency2
when agency1 is null and agency2>agency3 then agency3
when agency2 is null and agency1<agency3 then agency1
when agency2 is null and agency1>agency3 then agency3
when agency3 is null and agency1<agency2 then agency1
when agency3 is null and agency1>agency2 then agency2
when agency1 is null and agency2 is null then agency3
when agency2 is null and agency3 is null then agency1
when agency1 is null and agency3 is null then agency2
end)columnname
from tablename