Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The following snippet is a join on multiple tables. I need to display all order number, customer name, product name, price and quantity ordered from anyone from Australia. I'm getting table headings but no rows. Is there something wrong with this?
SELECT
"order".orderno AS ord,
customer.cname,
product.prodname,
customer.country_code,
orderdetl.price,
orderdetl.qty,
country.country_code
FROM
prema01.country,
prema01.customer,
prema01."order",
prema01.orderdetl,
prema01.product
WHERE
customer.country_code = 'AUS'
I've changed the code, verified there is data in the tables and it still comes out blank.
I am completely stumped.
SELECT O.ORDERNO, C.CNAME, PN.PRODNAME, ODT.PRICE, ODT.QTY, ODT.QTY * PN.PRODSELL AS TOTAL
FROM prema01.ORDER O, prema01.CUSTOMER C, prema01.ORDERDETL ODT, prema01.PRODUCT PN, prema01.COUNTRY CT
WHERE CT.COUNTRY_NAME = 'Australia'
AND C.COUNTRY_CODE = CT.COUNTRY_CODE
AND C.CUSTNO = O.CUSTNO
AND O.ORDERNO = ODT.ORDERNO
AND PN.PRODNO = ODT.PRODNO AND O.ORDERNO <= 60
ORDER BY TOTAL DESC;
forgot to add the change. the tables have data in them, i've physically verified that.
For starters, you need some join conditions e.g.
Select
r.country_code,
c.cname
From
prema01.country r
inner join
prema01.customer c
on r.country_code = c.country_code
You need to build up the relationships with the other tables in a similar way.
Also, are you sure your tables have any data in them. Does
Select
Count(*)
From
prema01.country
return anything? How about
Select
Count(*)
From
prema01.customer
Where
country_code = 'AUS'
?
Fixed, I was looking for 'Australia' while I have 'AUSTRALIA' in my country table. thanks for the help
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Tried to change my code many times, need help with these errors, please!
Result: ambiguous column name: main.Guides.Guide_ID
SELECT *
FROM Guides
INNER JOIN Guides ON Guides_Countries.Guide_ID = Guides_Countries.Guide_ID
INNER JOIN Countries ON Countries.Country_ID = Guides_Countries.Country_ID
INNER JOIN Guides_Languages ON Guides.Guide_ID = Guides_Languages.Guide_ID
INNER JOIN Languages ON Languages.Language_ID = Guides_Languages.Language_ID
WHERE Countries.Name="Kazakhstan" AND (Languages.Name="German" OR Languages.Name="English") AND Guides.Guide_ID NOT IN
(SELECT Guide_ID
FROM GuidesUnavailableDates
INNER JOIN GuidesUnavailableDates ON GuidesUnavailableDates.UnDate_G_ID=Guides_UnDate.UnDate_G_ID
WHERE (Start_date<="21/06/2020" and End_date>="21/06/2020")
OR (Start_date<="30/06/2020" and End_date>="30/06/2020")
OR (Start_date>="21/06/2020" and End_date<="30/06/2020")
)
;
You have multiple table names appearing multiple times in your FROM clauses. That is causing your problem. I think you want:
SELECT *
FROM Guides g JOIN
Guides_Countries gc
ON gc.Guide_ID = g.Guide_ID JOIN
Countries c
ON c.Country_ID = gc.Country_ID JOIN
Guides_Languages gl
ON g.Guide_ID = gl.Guide_ID JOIN
Languages l
ON l.Language_ID = gl.Language_ID
WHERE c.Name = 'Kazakhstan' AND
l.Name IN ('German', 'English') AND
g.Guide_ID NOT IN (SELECT gud.Guide_ID
FROM GuidesUnavailableDates gud
WHERE gud.Start_date <= '2020-06-30' AND
gud.End_date >= '2020-06-21'
);
Notes:
Guide_Countries is not in your FROM list although Guides is there twice.
IN is much simpler than mutiple ORs.
All columns are qualified so it is clear what table they are coming from.
All tables have simple table aliases which are abbreviations for the table names.
There is no need for a JOIN in the subquery.
Use well formatted dates.
I am guessing that the weird date logic is to find an overlap with the time period mentioned so I simplified the logic. (You haven't explained the logic, so this is a guess and your original code doesn't make sense.)
I strongly recommend using NOT EXISTS with subqueries rather than NOT IN. However, I did not change the code here (mostly because I can't really tell what the subquery is supposed to be doing).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i am trying to join these two tables, but I can only do so if I change values of columns while joining like "substr(to_char(p.personnummer),3)" and "substr(k.ip_id,1,10)" . Can anyone tell me what is the order I am supposed to do it?
select x.ip_id, x.organisationsnummer
from (select
substr(to_char(p.personnummer),3) as ip_id_jnr,
substr(k.ip_id,1,10) as ipo_id
from customer k
inner join customer_VIEW p on ipo_id = ip_id_jnr) x
;
Your query doesn't show which table contains organisationsnummer; I used the k alias (as if it belongs to customer); fix it if necessary.
So, inline view:
select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10);
Now use it:
select x.ip_id,
x.organisationsnummer
from (select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10)
) x;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed, gen.qty_seed, gen.price
FROM (SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod FROM gen GROUP BY hhid, country) AS temp, gen
WHERE (((temp.hhid)=gen.hhid) And ((temp.country)=gen.country) And ((temp.max_prod)=gen.total_prod) And ((temp.max_area)=gen.area))
ORDER BY temp.hhid;
Why do some of the results were not seen?
I have atleast 100 hhid , each one has 3 area , 3 productions , quantity of seed , price etc ...
all of the hhid was shown in the ouput query except for 1 hhid ,
what might be wrong ?
The problem is that the maximum of prod and the maximum of area are rarely in the same row.
You should also learn to use explicit join syntax. A simple rule: never use a comma in the from clause.
This may be what you want:
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed,
gen.qty_seed, gen.price
FROM gen INNER JOIN
(SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod
FROM gen
GROUP BY hhid, country
) AS temp
ON temp.hhid = gen.hhid AND temp.country = gen.country
WHERE (temp.max_prod = gen.total_prod or temp.max_area = gen.area)
ORDER BY temp.hhid;
I left the WHERE clause in, because MS Access has strange restrictions on joins. Logically it should go in the ON clause, but I'm not 100% sure that Access accepts that syntax.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this simple query, I want to retrieve all the data that select statement is selecting now (rightly), plus I want to get total rows number using the same query how can I do this. How do I add total rows query with below query?
SELECT
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
Last item of this result has a row number that equals the number of records.
SELECT
ROW_NUMBER() over(tblTaxingSchemeDetails.TaxSchemeDetailsId)as SeqNo,
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
SELECT * COUNT(*) OVER() AS [Total_Rows]
FROM table 1 innerjoin table 2
You can get the desired result through this Query... Also would remind this query would be take more resource while compare to the normal query...
SELECT
COUNT(tblTaxingSchemeDetails.TaxSchemeDetailsId) over(PARTITION BY tblTaxingScheme.TaxSchemeId) as h,
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this query which should give this kind of output -
NAME DATE_JOINED TOTAL_GROUPS_FORMED LOANS_ADVANCES TOTAL_ISSUED_AMOUNT TOTAL_LOAN_AMT_REPAID TOTAL_ACCOUNT TOTAL_CLIENTS_RECRUITED TOTAL_AMT_DEPOSITS TOTAL_AMT_WITHDRAWALS NUMBER_DROPOUTS REPORT_DATE
This is my query.
SELECT
D.NAME,
0.0 AS DATE_JOINED,
0 AS TOTAL_GROUPS_FORMED,
COUNT(LOAN_NUMBER) AS LOANS_ADVANCES,
SUM(ISSUED_AMOUNT) AS TOTAL_ISSUED_AMOUNT,
0.0 AS TOTAL_LOAN_AMT_REPAID,
COUNT(A.ACCOUNT_NUMBER) AS TOTAL_ACCOUNT,
COUNT(C.CUSTOMER_CODE) AS TOTAL_CLIENTS_RECRUITED,
0.0 AS TOTAL_AMT_DEPOSITS,
0.0 AS TOTAL_AMT_WITHDRAWALS,
COUNT(CH.ACCOUNT_NUMBER) AS NUMBER_DROPOUTS,
D.REPORT_DATE FROM SALES_OFFICER D
LEFT JOIN LOANS L ON D.SALES_OFFICER_CODE=L.OFFICER_CODE
LEFT JOIN OPEN_ACCOUNT A ON D.SALES_OFFICER_CODE=A.OFFICER_CODE
LEFT JOIN CUSTOMERS C ON D.SALES_OFFICER_CODE=C.OFFICER_CODE
LEFT JOIN CLOSED_ACCOUNT CH ON D.SALES_OFFICER_CODE=CH.OFFICER_CODE
GROUP BY D.NAME,D.REPORT_DATE
This query is not completing probably because all the tables have a lot of data.
Is there another way to have the same output where one Sales Officer's information can be viewed from multiple tables and aggregated together as a single record, grouped by the reporting date.
You are getting a cartesian product for each sales officer -- the number of loans TIMES the number of open accounts TIME the number of customers TIMES the number of closed accounts.
You need to summarize each dimension prior to doing the join. Here is a start:
from sales_offices so left join
(select l.OFFICER_CODE, COUNT(*) AS LOANS_ADVANCES,
SUM(ISSUED_AMOUNT) AS TOTAL_ISSUED_AMOUNT
from loans l
group by l.OFFICER_CODE
)
on so.SALES_OFFICER_CODE = l.OFFICER_CODE
This is just a guess, because you don't use table aliases on all the columns.