Handling Duplicate Column Name Issue in MariaDB Count Query - sql

I am getting an error on the query I'm running to get a count in MariaDB. This is the error:
Error Code: 1060. Duplicate column name 'id_number'
And this is my SQL code:
SELECT COUNT(*) as count FROM (
SELECT * FROM ((cr.customers
INNER JOIN (progress_notes_details
INNER JOIN progress_notes ON progress_notes_details.progress_note_id = progress_notes.id_number)
ON customers.id_number = progress_notes.c_id)
INNER JOIN open_balances ON progress_notes_details.id_number = open_balances.progress_notes_detail_id)
INNER JOIN
customer_payer_xref ON customers.id_number = customer_payer_xref.c_id
WHERE
(((progress_notes_details.qb_isbillable) IS NULL
OR (progress_notes_details.qb_isbillable) <> 1)
AND ((progress_notes_details.date_of_visit) BETWEEN coverage_start AND coverage_end)
AND ((progress_notes_details.dynamics_status) = 3)
AND ((customer_payer_xref.payer_id) = 23)
AND ((customer_payer_xref.primary_secondary_account_type) = 1))
) AS qdat
Can this be resolved via aliases? If so, it's unclear to me where to add them. In the main query? In the subquery?
Also, to clarify, I just inherited this code - and yes, it's bracket-happy.

Alias customers table as c and use it as c.id_number at one of the places it will remove that duplicate error as this is the only table you are using multiple times with idnumber hence duplicate

Remove the outer query:
SELECT COUNT(*)
FROM ((cr.customers . . .
Clearly, you have tables with the same column name. This causes a problem with SELECT *.
All the parentheses are probably not needed for the JOINs as well.

Related

Outer join for Alias name and column name -Oracle

I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.

How to Select Sum and Multiple Columns

Problem: I have a piece of code that joins two tables on whether the amounts for the respective references from both add to zero. The issue is that I don't know how to include more columns in the resulting query.
SELECT *
FROM (SELECT PayRef, SUM(MerchAmount) AS FAmount
FROM qryPaymentReferenceConversion
GROUP BY PayRef) AS qryPaymentReferenceConversion
INNER JOIN (SELECT Reference, SUM(TransAmount) AS BAmount
FROM tblBankStatementRegisterMaster GROUP BY Reference) AS tblBankStatementRegisterMaster
ON tblBankStatementRegisterMaster.Reference =qryPaymentReferenceConversion.PayRef
WHERE qryPaymentReferenceConversion.FAmount + tblBankStatementRegisterMaster.BAmount = 0;
In addition, when I try this alternative, I get a syntax error. Problem: Whenever I compile the code below, I receive a syntax error in the JOIN operation. Is this because I am selecting all fields rather than specifying the Reference field from the bank table? If so, how do I select all tables while maintaining the relationship?
SELECT tblBankStatementRegisterMaster.*, TransAmt
FROM tblBankStatementRegisterMaster AS BAmount
INNER JOIN (SELECT PayRef, Sum(MerchAmt) AS FAmount
FROM qryPaymentReferenceConversion GROUP BY PayRef)
ON tblBankStatementRegisterMaster.Reference = qryPaymentReferenceConversion.PayRef
WHERE qryPaymentReferenceConversion.FAmount + tblBankStatementRegisterMaster.BAmount = 0;

Hive - Multiple sub-queries in where clause is failing

I am trying to create a table by checking two sub-query expressions within the where clause but my query fails with the below error :
Unsupported sub query expression. Only 1 sub query expression is
supported
Code snippet is as follows (Not the exact code. Just for better understanding) :
Create table winners row format delimited fields terminated by '|' as
select
games,
players
from olympics
where
exists (select 1 from dom_sports where dom_sports.players = olympics.players)
and not exists (select 1 from dom_sports where dom_sports.games = olympics.games)
If I execute same command with only one sub-query in where clause it is getting executed successfully. Having said that is there any alternative to achieve the same in a different way ?
Of course. You can use left join.
Inner join will act as exists. and left join + where clause will mimic the not exists.
There can be issue with granularity but that depends on your data.
select distinct
olympics.games,
olympics.players
from olympics
inner join dom_sports dom_sports on dom_sports.players = olympics.players
left join dom_sports dom_sports2 where dom_sports2.games = olympics.games
where dom_sports2.games is null

Parent Query does not see columns of subquery when returning using SELECT *

I am currently learning SQL using Oracle SQL developer.
While writing queries I came up with three different versions of the same query.
SELECT
sh.share_id
FROM shares sh
LEFT JOIN trades tr
ON sh.share_id = tr.share_id
WHERE trade_id is NULL;
SELECT
sr.share_id
FROM (SELECT sh.share_id, tr.trade_id
FROM shares sh
LEFT JOIN trades tr
ON sh.share_id = tr.share_id) sr
WHERE sr.trade_id is NULL;
SELECT
sr.share_id
FROM (SELECT *
FROM shares sh
LEFT JOIN trades tr
ON sh.share_id = tr.share_id) sr
WHERE sr.trade_id is NULL;
The first two queries compile, run and return the same result set but when I try to run the third query I get a error on the second line of the third query.
"SR"."SHARE_ID": invalid identifier.
I know that * in the SELECT statement selects all columns so why Am I getting this error?
From reading your comments, in your final query, the DBMS doesn't know which share_id to use for your SELECT sr.share_id. AKA the SELECT * of your subquery is grabbing two share_id columns. You have to do something like your 2nd query.
The problem is that select * is selecting all columns from both tables, even those with the same name. So, you are getting share_id twice. A simple fix is to use USING:
SELECT sr.share_id
FROM (SELECT *
FROM shares sh LEFT JOIN
trades tr
USING (share_id)
) sr
WHERE sr.trade_id is NULL;
Of course, tis only fixes the reference to share_id.

SQL subquery multiple times error

I am making a subquery but I am getting a strange error
The column 'RealEstateID' was specified multiple times for 'NotSold'.
here is my code
SELECT *
FROM
(SELECT *
FROM RealEstatesInfo AS REI
LEFT JOIN Purchases AS P
ON P.RealEstateID=REI.RealEstateID
WHERE DateBought IS NULL) AS NotSold
INNER JOIN OwnerEstate AS OE
ON OE.RealEstateID=NotSold.RealEstateID
It's on SQL server by the way.
That's because there will be 2 realestiteids in your subquery. You need to change it to explicitly list the columns from both table and only include 1 realestateid. It doesn't matter which as you use it for your join.
If you're very Lazy you can select rei.* and only name the p cols apart from realestateid.
Btw select * is probably never a good idea in sub queries or derived tables or ctes.