Got a question regarding SQL and ColdFusion: I can’t write SQL code properly, so that it won’t repeat the variables twice. So far I’ve got:
<cfquery name="get_partner_all" datasource="#dsn#">
SELECT
C.COMPANY_ID,
C.FULLNAME,
CP.MOBILTEL,
CP.MOBIL_CODE,
CP.IMCAT_ID,
CP.COMPANY_PARTNER_TEL,
CP.COMPANY_PARTNER_TELCODE,
CP.COMPANY_PARTNER_TEL_EXT,
CP.MISSION,
CP.DEPARTMENT,
CP.TITLE,
CP.COMPANY_PARTNER_SURNAME,
CP.COMPANY_PARTNER_NAME,
CP.PARTNER_ID,
CP.COMPANY_PARTNER_EMAIL,
CP.HOMEPAGE,
CP.COUNTY,
CP.COUNTRY,
CP.COMPANY_PARTNER_ADDRESS,
CP.COMPANY_PARTNER_FAX,
CC.COMPANYCAT,
CRM.BAKIYE,
CRM.BORC,
CRM.ALACAK
FROM
COMPANY_PARTNER CP,
COMPANY C,
COMPANY_CAT CC,
#DSN2_ALIAS#.COMPANY_REMAINDER_MONEY CRM
WHERE
C.COMPANY_ID = CP.COMPANY_ID
AND C.COMPANY_ID = CRM.COMPANY_ID
AND C.COMPANYCAT_ID = CC.COMPANYCAT_ID
As you can see definition C.COMPANY_ID is repeated twice, so the variable shown also twice, but I need this (CRM) definition to display some money issues.
Can anyone show me how I can define it in a different way so that the output of this code won’t repeat the variables?
I assume you mean that you get multiple columns in the result set, each with the name “COMPANY_ID”. The solution to this is to specify specific columns from all of the tables, instead of
SELECT *(not just for theCOMPANY_CATtable, aliasCC).If you’re getting “repeated” rows, then you need to examine the contents of these rows. What’s happening there is that one or more rows from another table is matching one row from the “COMPANY” table. Each matching pair of rows generates a row in the output. Now you’ve expanded your column list, compare a pair of rows which have the same
COMPANY_ID– in which columns do they differ? If it’s in, say, the last 3 columns, then there are multiple rows inCRMwhich match the sameCOMPANY_ID.Once you’ve identified the other table that is causing duplicates to occur, you need to decide how to limit them – should you be aggregating values from that table (e.g.
SUMorMAX), or is there a way to further refine which row from the other table you want to match to the row inCOMPANY.At a guess though, I’d speculate that one company could have multiple partners…