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 9 years ago.
Improve this question
I have 3 tables:
user(use_id, use_role),
message(mes_id, from_use_id[fk->user.use_id], mes_create_at),
message_user(mes_use_id, mes_id[fk->message.mes_id], to_use_id[fk->user.use_id])
User can be use_role=1(student) or use_role=2(teacher).
I have to get all Users that are use_type=1(student) sorted by last contact with teacher. No matter if student write to teacher or teacher write to student.
I assume you use ms sql:
select *
from
(
select student.use_id, Message.mes_create_at, Message.from_use_id, Message_User.to_use_id
from User student
join Message on Message.use_id = student.use_id and student.use_role = 1
join Message_User on Message_User.mes_id = Message.mes_id
where Message_User.to_use_id in (select use_id from User where use_role = 2)
union
select Message_User.to_use_id, Message.mes_create_at, Message.from_use_id, Message_User.to_use_id
from User teacher
join Message on Message.use_id = teacher.use_id and teacher.use_role = 2
join Message_User on Message_User.mes_id = Message.mes_id
where Message_User.to_use_id in (select use_id from User where use_role = 1)
) as temp
order by mes_create_at desc
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
I need to know record where ParentSpecID = to ChildSpecID cannot think of how to approach
It seems very simple using inner join, if I followed you correctly.
Select p.*, c.*
From parentspecid p
Join itemspecfullstruc i on i.rootitemspecid = p.itemspecid
Join childspecid c on i.childitemspecid = c.itemspecid
Where p.itemspecid = c.itemspecid
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
I have this data:
I'm looking for a query which can return the results of combination of Type (Phone and Memory) based on overlapping of startdate and enddate as below:
Thanks
Probably something like this:
SELECT *
FROM
table p
INNER JOIN
table m
ON
NOT(m.startdate > p.enddate OR m.enddate < p.startdate)
WHERE
p.Type = 'Phone' AND
m.Type = 'Memory'
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 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.