SQL : combine 2 rows into 1 - sql

This is my database
I'm trying to display "hello" and "wo" in the same column
My SQL statement:
SELECT
d.CampaignId, d.ClientID,
citn.ImagePath AS Thumbnail, cidi.ImagePath AS DetailImage
FROM
MasterData.CampaignImage AS d
INNER JOIN
MasterData.CampaignImage AS citn ON d.CampaignId = citn.CampaignId
AND d.ClientID = citn.ClientID
AND citn.ImageTypeId = 1
INNER JOIN
MasterData.CampaignImage AS cidi ON d.CampaignId = cidi.CampaignId
AND d.ClientID = cidi.ClientID
AND cidi.ImageTypeId = 2
The output:
But now I have 2 rows in my output, how can I combine these into just one row?

Simply do SELECT DISTINCT to skip duplicate rows.

Related

SQL: If there are two rows that contain same record, want it to display one

based on my question above, below is the SQL
SELECT ets_tools.tools_id, ets_borrower.fullname, ets_team.team_name, ets_borrow.time_from,
ets_borrow.time_to, ets_borrow.borrow_id FROM ets_tools
INNER JOIN ets_tools_borrow ON ets_tools.tools_id = ets_tools_borrow.tools_id
INNER JOIN ets_borrow ON ets_borrow.borrow_id = ets_tools_borrow.borrow_id
INNER JOIN ets_borrower ON ets_borrower.badgeid = ets_borrow.badgeid
INNER JOIN ets_team ON ets_team.team_id = ets_borrower.team_id
WHERE ets_tools.borrow_id IS NOT NULL AND ets_borrow.status_id = 1 AND ets_borrow.time_to IS NULL
and the result display like this:
From the image above, we can see that the borrow_id with value 1 display two rows. Now, how to display only one borrow_id for value 1 since its duplicate the same things.
Anyone can help?
Assuming you want to retain the record having the smallest tools_id, you could aggregate by the other columns and take the MIN of tools_id:
SELECT
MIN(ets_tools.tools_id) AS tools_id,
ets_borrower.fullname,
ets_team.team_name,
ets_borrow.time_from,
ets_borrow.time_to,
ets_borrow.borrow_id
FROM ets_tools
INNER JOIN ets_tools_borrow ON ets_tools.tools_id = ets_tools_borrow.tools_id
INNER JOIN ets_borrow ON ets_borrow.borrow_id = ets_tools_borrow.borrow_id
INNER JOIN ets_borrower ON ets_borrower.badgeid = ets_borrow.badgeid
INNER JOIN ets_team ON ets_team.team_id = ets_borrower.team_id
WHERE
ets_tools.borrow_id IS NOT NULL AND
ets_borrow.status_id = 1 AND
ets_borrow.time_to IS NULL
GROUP BY
ets_borrower.fullname,
ets_team.team_name,
ets_borrow.time_from,
ets_borrow.time_to,
ets_borrow.borrow_id;
Try this:
Change the SELECT to SELECT TOP 1 WITH TIES
And at the end add ORDER BY ROW_NUMBER() OVER(PARTITION BY ets_borrow.borrow_id ORDER BY ets_tools.tools_id)

Joining 2 columns where 1 column has unique records and the other has duplicates

I have 2 column Risk_Geo_ID in one table which has duplicates and Geography_Identifier in other table which has unique records. Doing an inner join is giving me duplicate records and cannot process this query in cube as Geography_Identifier is a PK and its unique. Need a query to get unique records.
Here is the Code:
SELECT
s.Geography_Identifier
,s.State_Code
,s.State_Name
,s.County_Name
,s.City_Name
,s.ZIP_Code
,a.Risk_ID
,a.Risk_Address
,a.Latitude
,a.Longitude
,a.Distance_to_Coast
,a.Insurance_Score
FROM [Policy].[Dim_Risk] AS a
INNER JOIN [Policy].[Fact_Monthly_Policy_Snap] AS b
ON b.Risk_ID = a.Risk_ID
AND b.Insurance_score = a.Insurance_Score
INNER JOIN [Common].[Dim_Geography] AS s
ON b.Risk_Geo_ID = s.Geography_Identifier
Try using the distinct
SELECT
distinct s.Geography_Identifier
,s.State_Code
,s.State_Name
,s.County_Name
,s.City_Name
,s.ZIP_Code
,a.Risk_ID
,a.Risk_Address
,a.Latitude
,a.Longitude
,a.Distance_to_Coast
,a.Insurance_Score
FROM [Policy].[Dim_Risk] AS a
INNER JOIN [Policy].[Fact_Monthly_Policy_Snap] AS b
ON b.Risk_ID = a.Risk_ID
AND b.Insurance_score = a.Insurance_Score
INNER JOIN [Common].[Dim_Geography] AS s
ON b.Risk_Geo_ID = s.Geography_Identifier
Try to use CROSS APPLY with TOP 1 if you want to get just one row with such ID:
SELECT
q.Geography_Identifier
,s.State_Code
,s.State_Name
,s.County_Name
,s.City_Name
,s.ZIP_Code
,a.Risk_ID
,a.Risk_Address
,a.Latitude
,a.Longitude
,a.Distance_to_Coast
,a.Insurance_Score
FROM [Policy].[Dim_Risk] AS a
INNER JOIN [Policy].[Fact_Monthly_Policy_Snap] AS b
ON b.Risk_ID = a.Risk_ID
AND b.Insurance_score = a.Insurance_Score
CROSS APPLY
(
SELECT
TOP 1
*
FROM [Common].[Dim_Geography] AS s
WHERE s.Geography_Identifier = b.Risk_Geo_ID
)q

How to get fields from multiple tables

I want to get fields from 2 different tables . The last field candidate_score_id has a many to one relationship. So how should I join the below 2 queries
1) To get candidate_score_id from the candidate_score table.
select candidate_score_id from candidate_score a where
a.assessment_id = NEW.assessment_id and
a.candidate_id = NEW.candidate_id and
a.attempt_Count = NEW.attempt_count;
2) To insert different fields in to the candidate_score_details table. The field in this table should be obtained by query above.
insert into candidate_score_details(candidate_score_details_id, candidate_id, assessment_id, attempt_count, score_type, score_tag,correct, candidate_score_id)
select uuid();
select a.candidate_id, a.assessment_id,a.attempt_count,"BY-COMPLEXITY",
case c.complexity
when 1 then "HIGH"
when 2 then "MEDIUM"
when 3 then "LOW"
end, count(*) from candidate_answer a, answer_key b, question_meta_data c where a.candidate_id = NEW.candidate_id and
a.assessment_id = NEW.assessment_id and
a.attempt_count = NEW.attempt_count and
a.assessment_id = b.assessment_id and
a.question_id = b.question_number and
a.response = b.answer and
a.question_id = c.question_number
group by a.candidate_id, a.assessment_id, a.attempt_count, c.complexity;
Just looking at the SQL joining aspect of your question, you'll need to specify the table I THINK you're aliasing a 2nd table with the "NEW" reference. If that's the case, then the query would be (replacing "OTHER_TABLE_NAME" with the name of the 2nd table:
select a.candidate_score_id
from candidate_score a
left join OTHER_TABLE_NAME new on
and a.assessment_id = NEW.assessment_id
and a.candidate_id = NEW.candidate_id
and a.attempt_Count = NEW.attempt_count
Seems that Query 1 has the same 3 criteria on the "candidate_score" table as for the "candidate_answer" table in Query 2.
So how about adding a LEFT JOIN of "candidate_score" to "candidate_answer" on those 3 fields?
For example:
INSERT INTO candidate_score_details
(
candidate_score_details_id,
candidate_id,
assessment_id,
attempt_count,
score_type,
score_tag,
correct,
candidate_score_id
)
SELECT
uuid(),
answer.candidate_id,
answer.assessment_id,
answer.attempt_count,
'BY-COMPLEXITY' AS score_type,
(CASE meta.complexity
WHEN 1 THEN 'HIGH'
WHEN 2 THEN 'MEDIUM'
WHEN 3 THEN 'LOW'
END) AS score_tag,
COUNT(*) AS correct,
MAX(score.candidate_score_id) AS max_candidate_score_id
FROM candidate_answer AS answer
JOIN answer_key AS akey
ON (akey.assessment_id = answer.assessment_id AND akey.question_number = answer.question_id AND akey.answer = answer.response)
LEFT JOIN candidate_score AS score
ON (score.candidate_id = answer.candidate_id AND score.assessment_id = answer.assessment_id AND score.attempt_count = answer.attempt_count)
LEFT JOIN question_meta_data AS meta
ON meta.question_number = answer.question_id
WHERE answer.candidate_id = NEW.candidate_id
AND answer.assessment_id = NEW.assessment_id
AND answer.attempt_count = NEW.attempt_count
GROUP BY answer.candidate_id, answer.assessment_id, answer.attempt_count, meta.complexity;

Duplicate rows while multiplying two columns of two different tables using joins

I have these following tables
Question_Segment_Master
Question_Set_Details
While I am joining these two tables I am getting duplicate rows..
Please view fiddle,where I posted those twos schema and it's data..
http://sqlfiddle.com/#!18/4c84f/10
This is my expected OP
You need to get the question_marks of each segment. Also, you should JOIN using the branch_id, test_id, subject_code_id along with the segment_id.
Using JOIN and then take Distinct values
SELECT DISTINCT qsm.segment_id, segment_name, segment_description
, must_attend_question AS tot_attented_question, total_question AS tot_questions
, (qsd.question_marks * must_attend_question) AS tot_marks
, '' AS marks_obtain
FROM dbo.Question_Segment_Master AS qsm
INNER JOIN dbo.Question_Set_Details AS qsd
ON (qsd.branch_id = qsm.branch_id AND qsd.test_id = qsm.test_id
AND qsd.segment_id = qsm.segment_id AND qsd.subject_code_id = qsm.subject_code_id)
WHERE qsm.subject_code_id = 1 and qsm.test_id = 1 and qsm.branch_id = 15;
Demo
You were missing the segment_id in your join clause
SELECT DISTINCT
qsm.segment_id,
qsm.segment_name,
qsm.segment_description,
qsm.must_attend_question tot_attented_question,
qsm.total_question tot_questions,
(qsd.question_marks * must_attend_question) tot_marks,
'' AS marks_obtain
FROM dbo.question_segment_master qsm
INNER JOIN dbo.question_set_details qsd on
qsd.test_id = qsm.test_id
and qsd.segment_id = qsm.segment_id
WHERE qsm.test_id=1
AND qsm.branch_id = 15
AND qsm.subject_code_id =1 AND qsd.question_set_id = 1

Why different results to two different ways of writing query

I am trying to understand why the following two SQL statements give different results. The first works as expected, the second produces no records.
Select * from Jet.LayoutListItemEntity_Default itemDefault
Join Jet.LayoutListEntity b
on itemDefault.UiKey = b.UiKey
Left join (Select * from Jet.LayoutListItemEntity where DomainId =2) item
on itemDefault.BindingPath = item.BindingPath
where item.DomainId is null
and b.DomainId = 2
Select * from Jet.LayoutListItemEntity_Default itemDefault
Join Jet.LayoutListEntity b
on itemDefault.UiKey = b.UiKey
Left join Jet.LayoutListItemEntity item
on itemDefault.BindingPath = item.BindingPath
where item.DomainId is null
and item.DomainId = 2
and b.DomainId = 2
The primary difference is that one puts the item.DomainId = 2 at the end, instead of in it's own select. It seems to me they would produce the same results.
Greg
These conditions are mutually exclusive:
where item.DomainId is null
and item.DomainId = 2
item.DomainID can't simultaneously be NULL and 2
You can move the errant where criteria to the JOIN criteria:
Select * from Jet.LayoutListItemEntity_Default itemDefault
Join Jet.LayoutListEntity b
on itemDefault.UiKey = b.UiKey
Left join Jet.LayoutListItemEntity item
on itemDefault.BindingPath = item.BindingPath
and item.DomainId = 2
where item.DomainId is null
and b.DomainId = 2
Your WHERE criteria is messed up in the 2nd query -- to make those statements the same, move the and item_DomainId = 2 to the JOIN.
Select * from Jet.LayoutListItemEntity_Default itemDefault
Join Jet.LayoutListEntity b
on itemDefault.UiKey = b.UiKey
Left join Jet.LayoutListItemEntity item
on itemDefault.BindingPath = item.BindingPath and item.DomainId = 2
where item.DomainId is null
and b.DomainId = 2