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.
Related
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
I have a minor problem with SQL Server, I have five tables:
Klientet (ID_Klienti (PK), Emri, Mbiemri, Adressa, Qyteti, Telefoni)
Punetoret (ID_Puntori (PK), ID_Lokacioni (foreign key), Emri, Mbiemri, Adressa, Qyteti, Telefoni)
Lokacioni (ID_Lokacioni (PK), Emri_Lokacionit)
Veturat (ID_Veturat (PK), Modeli, Viti, Ngjyrat)
Shitjet (ID_Shitja (PK), ID_Klienti (FK), ID_Puntori (FK), ID_Lokacioni(PK), ID_Vetura)
I want to inner join or (join) all five of them, and I get a result with 47 rows instead of number between 4 and 10. I've done it but it is missing something and I can't find what. I hope you guys can help me .
This is where I got so far
SELECT
Klientet.Emri, Klientet.Mbiemri,
Punetoret.Emri, Punetoret.Mbiemri,
Lokacioni.Emri_Lokacionit,
Shitjet.Data_shitjes
FROM
Klientet
INNER JOIN
Shitjet ON Shitjet.ID_Klienti = Klientet.ID_Klienti
INNER JOIN
Punetoret ON Punetoret.ID_Punetori = Shitjet.ID_Punetori
INNER JOIN
Lokacioni ON Lokacioni.ID_Lokacioni = Punetoret.ID_Lokacioni
INNER JOIN
Veturat ON Lokacioni.ID_Lokacioni = Veturat.ID_Lokacioni
WHERE
Emri_Lokacionit = 'Prishtine'
OR Emri_Lokacionit = 'Gjilan'
OR Emri_Lokacionit = 'Mitrovice'
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i am using below query to pick User highest qualification but i get repeated values. because user have more than one qualifications.
SELECT hrQualifications.Qualification,hrUserQualifications.HRUserID,MAX(hrQualifications.QualificationLevel) as Qlevel
FROM hrQualifications RIGHT OUTER JOIN
hrUserQualifications ON hrQualifications.QualificationID = hrUserQualifications.QualificationID RIGHT OUTER JOIN
hrUserApplyforPositions ON hrUserQualifications.HRUserID = hrUserApplyforPositions.HRUserID
WHERE (hrUserApplyforPositions.HrPositionID = 1)
group by hrQualifications.Qualification,hrUserQualifications.HRUserID
output which i got
Qualification UserID QualificationLevel
B.Sc.(Hons) 12 16
F.Sc 12 12
B.Sc.(Hons) 18 16
require output. i want highest qualification of user.
Qualification UserID QualificationLevel
B.Sc.(Hons) 12 16
B.Sc.(Hons) 18 16
A good way to do what you want is to use row_number(). This adds a sequential number to rows, starting over again within a partition and ordered by another field.
For your query:
with t as (
SELECT q.Qualification, uq.HRUserID, q.QualificationLevel as Qlevel,
row_number() over (partition by uq.HRUserID
order by q.QualificationLevel desc
) seqnum
FROM hrQualifications q RIGHT OUTER JOIN
hrUserQualifications uq
ON q.QualificationID = uq.QualificationID RIGHT OUTER JOIN
hrUserApplyforPositions uap
ON uq.HRUserID = uap.HRUserID
WHERE uap.HrPositionID = 1
)
select *
from t
where seqnum = 1;
Note that I also added table aliases to make the query more readable.
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