create following tables with mentioned columns:branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
employee (employee-name, branch-name, salary).Insert 8 records in each table.then perform following queries:
Related
We have an organization that can have more than one customer type basically. However, what a user wants to see is either the partner or direct type (customer type is either Direct, Partner1, Partner2, or Partner3 but can be direct plus a partner value but only can be one of the partner values). So if a customer is both (ex: Direct and Partner1) they just want the type that is a partner (ex: Partner1). So I tried splitting out partners only into one temp table from a few tables joining together different org data. I have the same query without any limit pulling into a different temp table. Then I calculate count and put that into a temp table. Then I tried gathering data from all the temp tables. That is where I run into trouble and lose some of the customers where the type is direct (I have a image link below for a directcustomer and a customer who is both). I have been out of SQL for a bit so this one is throwing me...I figure the issue is the fact that I have a case statement referencing a table that a direct customer will not exist in (#WLPO). However I am not sure how to achieve pulling in these customers while also only selecting which partner type it is for a customer that has a partner and is also direct. FYI using MSSMS for querying.
If OBJECT_ID('tempdb..#WLPO') IS NOT NULL
DROP TABLE #WLPO
IF OBJECT_ID('tempdb..#org') IS NOT NULL
DROP TABLE #org
IF OBJECT_ID('tempdb..#OrgCount') IS NOT NULL
DROP TABLE #OrgCount
IF OBJECT_ID('tempdb..#cc') IS NOT NULL
DROP TABLE #cc
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #WLPO
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where os.WhiteLabelPartnerID in (2,3,4)
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #WLPO
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #org
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where 1=1--os.WhiteLabelPartnerID = 1
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #org
Select
OrganizationID,
count(OrganizationID) AS CountOrgTypes
INTO #OrgCount
from #org
where OrganizationID = 7613
group by OrganizationID
select * from #OrgCount
Select distinct
ct.OrganizationID,
ok.OrganizationName,
ct.CountOrgTypes,
case when ct.CountOrgTypes = 2 then wlp.WhiteLabelPartnerID
when ct.CountOrgTypes = 1 then ok.WhiteLabelPartnerID
END AS CustomerTypeCode,
case when ct.CountOrgTypes = 2 then wlp.StateName
when ct.CountOrgTypes = 1 then ok.StateName END As OrgState
INTO #cc
from #org ok
left join #WLPO wlp on wlp.OrganizationID=ok.OrganizationID
join #OrgCount ct on wlp.OrganizationID=ct.OrganizationID
select * from #cc
Select
OrganizationID,
OrganizationName,
CountOrgTypes,
case when CustomerTypeCode = 1 then 'Direct'
when CustomerTypeCode = 2 then 'Partner1'
when CustomerTypeCode = 3 then 'Partner2'
when CustomerTypeCode = 4 then 'Partner3' ELSE Null END AS CustomerType,
OrgState
from #cc
order by OrganizationName asc
DirectCustomer
CustomerwithBoth
I have ORDS 21 installed in a local 19c oracle database. I have created a stored procedure to list all the departments from the dept table with a cursor that lists the employees from the emp table. If I just list all departments and their employees the api works fine, but if I add a parameter to the query to specify which department, I get an error 17410 no data left .
Both queries are backed by plsql stored procedures. I have created many stored procedures using this same format with parameters and nested cursors before without a problem.
MWE for query that works:
create or replace procedure get_dept
as
l_cur sys_refcursor;
begin
open l_cur for
select d.deptno,d.dname,
cursor (select e.empno,e.ename
from emp e
where e.deptno = d.deptno
order by e.deptno,e.empno
) as employees
from dept d
order by d.deptno;
-- return the resultset in json format
apex_json.open_object;
apex_json.write('emps',l_cur);
apex_json.close_object;
end get_dept;
/
MWE for query that does not work:
create or replace procedure get_dept1
(
p_dept_no in varchar2
) as
l_cur sys_refcursor;
begin
open l_cur for
select d.deptno,d.dname,
cursor (select e.empno,e.ename
from emp e
where e.deptno = d.deptno
order by e.deptno,e.empno
) as employees
from dept d
where d.deptno = to_number(p_dept_no)
order by d.deptno;
-- return the resultset in json format
apex_json.open_object;
apex_json.write('emps',l_cur);
apex_json.close_object;
end get_dept1;
/
I am trying to select places infos from different tables: I have the place in one table, the geographical coordinates on another, and telephone and email in another. The issue is that we may or not have the email and telephone. I am looking for a way to get the email / phone when they exist, and have a blank value if they are not known. So I use the "CASE" instruction on the SELECT.
If we have no email and no phone, everything goes ok : we have only one line returned.
But if we have the email or the phone, it returns 2 lines; and the worst case is when we have both email and phones: in this case it returns three lines for each place :
place name , blank phone , blank mail
place name , blank phone , populated mail
place name , populated phone , blank mail
I am looking for a way to get only one line :
0) place name + populated (or not) phone + populated (or not) mail
I tried the 'distinct' but it doesn't do the trick :(
Here is my query :
select distinct
places.name as place,
CASE place_properties.pkey WHEN 'gen.telephone'
THEN place_properties.pvalue
END
as telephone,
CASE place_properties.pkey WHEN 'gen.email'
THEN place_properties.pvalue
END
as email
from
places
inner join
place_properties on place_properties.place_id = places.id
And here is a simplified answer example :
"ALLIAT" ;"0561059858" ;""
"ALLIAT" ;"" ;"contact#leslonguespistes.com"
"ALLIAT" ;"" ;""
"TARASCON SUR ARIEGE" ;"0561023281" ;""
"TARASCON SUR ARIEGE" ;"" ;"hotel#manoiragnes.com"
"TARASCON SUR ARIEGE" ;"" ;""
"Les Cabanes" ;"" ;""
We see that 'ALLIAT' and 'Tarascon' are returned three time because it has both a phone number and an email while "Les cabanes" is returned only once because it doesn't have any of it.
How to change the SQL to have only one line when we habe the email and/or the phone on the database?
try this:
SELECT
places.name AS place
, pp.telephone
, pp.email
FROM places
LEFT JOIN (
SELECT
place_id
, MAX(CASE place_properties.pkey WHEN 'gen.telephone' THEN place_properties.pvalue END) AS telephone
, MAX(CASE place_properties.pkey WHEN 'gen.email' THEN place_properties.pvalue END) AS email
FROM place_properties
WHERE place_properties.pkey IN ('gen.telephone', 'gen.email')
GROUP BY
place_id
) AS pp
ON places.id = pp.place_id
It might be possible to use alternatives to this approach but for that I would suggest we need to a good set of sample data.
It isn't clear if you only want places that have a phone of email, so I have used a LEFT JOIN which allows all places to be listed. If you do only want those places with (phone or email) then use an INNER JOIN instead
I think you can try this for get all email and telephone number
SELECT places.name AS place,
pp.telephone
pp.email
FROM places
LEFT JOIN (
SELECT
place_id,
ARRAY_TO_STRING(ARRAY_AGG(DISTINCT NULLIF(CASE place_properties.pkey WHEN 'gen.telephone' THEN place_properties.pvalue END,'')),',') AS telephone,
ARRAY_TO_STRING(ARRAY_AGG(DISTINCT NULLIF(CASE place_properties.pkey WHEN 'gen.email' THEN place_properties.pvalue END,'')),',') AS email
FROM place_properties
WHERE place_properties.pkey IN ('gen.telephone', 'gen.email')
GROUP BY 1
) AS pp
ON places.id = pp.place_id
You can use sub-selects here, like: select
name as place,
(select pvalue from place_properties where pkey = 'gen.telephone' and place_properties.place_id = places.id) as telephone,
(select pvalue from place_properties where pkey = 'gen.email' and place_properties.place_id = places.id) as email
from
places
I need a Query to extract the first instance and last instance only between date parameters.
I have a Table recording financial information with financialyearenddate field linked to Company table via companyID. Each company is also linked to programme table and can have multiple programmes. I have a report to pull the financials for each company
on certain programme which I have adjusted to pull only the first and last instance (using MIN & MAX) however I need the first instance.
after a certain date parameter and the last instance before a certain date parameter.
Example: Company ABloggs has financials for 1999,2000,2001,2004,2006,2007,2009 but the programme ran from 2001 to 2007 so I only want
the first financial record and last financial record between those years i.e. 2001 & 2007 records. Any help appreciated.
At the moment I am using 2 queries as I needed the data in a hurry but I need it in 1 query and only where financial year end dates are between parameters and only where there are minimum of 2 GVA records for a company.
Query1:
SELECT
gva.ccx_companyname,
gva.ccx_depreciation,
gva.ccx_exportturnover,
gva.ccx_financialyearenddate,
gva.ccx_netprofitbeforetax,
gva.ccx_totalturnover,
gva.ccx_totalwages,
gva.ccx_statusname,
gva.ccx_status,
gva.ccx_company,
gva.ccx_totalwages + gva.ccx_netprofitbeforetax + gva.ccx_depreciation AS GVA,
gva.ccx_nofulltimeequivalentemployees
FROM
(
SELECT
ccx_companyname,
MAX(ccx_financialyearenddate) AS LatestDate
FROM Filteredccx_gva AS Filteredccx_gva_1
GROUP BY ccx_companyname
) AS min_1
INNER JOIN Filteredccx_gva AS gva
ON min_1.ccx_companyname = gva.ccx_companyname AND
min_1.LatestDate = gva.ccx_financialyearenddate
WHERE (gva.ccx_status = ACTUAL)
Query2:
SELECT
gva.ccx_companyname,
gva.ccx_depreciation,
gva.ccx_exportturnover,
gva.ccx_financialyearenddate,
gva.ccx_netprofitbeforetax,
gva.ccx_totalturnover,
gva.ccx_totalwages,
gva.ccx_statusname,
gva.ccx_status,
gva.ccx_company,
gva.ccx_totalwages + gva.ccx_netprofitbeforetax + gva.ccx_depreciation AS GVA,
gva.ccx_nofulltimeequivalentemployees
FROM
(
SELECT
ccx_companyname,
MIN(ccx_financialyearenddate) AS FirstDate
FROM Filteredccx_gva AS Filteredccx_gva_1
GROUP BY ccx_companyname
) AS MAX_1
INNER JOIN Filteredccx_gva AS gva
ON MAX_1.ccx_companyname = gva.ccx_companyname AND
MAX_1.FirstDate = gva.ccx_financialyearenddate
WHERE (gva.ccx_status = ACTUAL)
Can't you just add a where clause using the first and last date parameters. Something like this:
SELECT <companyId>, MIN(<date>), MAX(<date>)
FROM <table>
WHERE <date> BETWEEN #firstDate AND #lastDate
GROUP BY <companyId>
declare #programme table (ccx_companyname varchar(max), start_year int, end_year int);
insert #programme values
('ABloggs', 2001, 2007);
declare #companies table (ccx_companyname varchar(max), ccx_financialyearenddate int);
insert #companies values
('ABloggs', 1999)
,('ABloggs', 2000)
,('ABloggs', 2001)
,('ABloggs', 2004)
,('ABloggs', 2006)
,('ABloggs', 2007)
,('ABloggs', 2009);
select c.ccx_companyname, min(ccx_financialyearenddate), max(ccx_financialyearenddate)
from #companies c
join #programme p on c.ccx_companyname = p.ccx_companyname
where c.ccx_financialyearenddate >= p.start_year and c.ccx_financialyearenddate <= p.end_year
group by c.ccx_companyname
having count(*) > 1;
You can combine your two original queries into a single query by including the MIN and MAX aggregates in the same GROUP BY query of the virtual table. Also including COUNT() and HAVING COUNT() > 1 ensures company must have at least 2 dates. So query should look like:
SELECT
gva.ccx_companyname,
gva.ccx_depreciation,
gva.ccx_exportturnover,
gva.ccx_financialyearenddate,
gva.ccx_netprofitbeforetax,
gva.ccx_totalturnover,
gva.ccx_totalwages,
gva.ccx_statusname,
gva.ccx_status,
gva.ccx_company,
gva.ccx_totalwages + gva.ccx_netprofitbeforetax + gva.ccx_depreciation AS GVA,
gva.ccx_nofulltimeequivalentemployees
FROM
(SELECT
ccx_companyname,
ccx_status,
MIN(ccx_financialyearenddate) AS FirstDate,
MAX(ccx_financialyearenddate) AS LastDate,
COUNT(*) AS NumDates
FROM Filteredccx_gva AS Filteredccx_gva_1
WHERE (ccx_status = ACTUAL)
GROUP BY ccx_companyname, ccx_status
HAVING COUNT(*) > 1
) AS MinMax
INNER JOIN Filteredccx_gva AS gva
ON MinMax.ccx_companyname = gva.ccx_companyname AND
(MinMax.FirstDate = gva.ccx_financialyearenddate OR
MinMax.LastDate = gva.ccx_financialyearenddate)
WHERE (gva.ccx_status = MinMax.ccx_status)
ORDER BY gva.ccx_companyname, gva.ccx_financialyearenddate
I have 3 server:
ECPAYSERV2
POSSQLSERVER
ECPAYSERV1
3 databases
ECPNWEB
ECPNPOS
ECPNDB
and 3 tables
account
branch
terminal
I have also finish to link the server :
ECPAYSERV2.ECPNWEB.dbo.account
POSSQLSERVER.ECPNPOS.dbo.branch
ECPAYSERV1.ECPNDB.dbo.terminal
The fields that should be seen would be the following
AccountID = ECPAYSERV2.ECPNWEB.dbo.account.(accntid)
BranchID = ECPAYSERV2.ECPNWEB.dbo.branch.(branchid)
TID = ECPAYSERV1.ECPNDB.dbo.terminal.(TID)
Store # = POSSQLSERVER.ECPNPOS.dbo.branch.(Storeno)
Store name = ECPAYSERV2.ECPNWEB.dbo.account.(accountholder)
Branch name = ECPAYSERV2.ECPNWEB.dbo.brachn.(branchname) , POSSQLSERVER.ECPNPOS.dbo.branch.(branchname) , ECPAYSERV1.ECPNDB.dbo.terminal.(retail_store_code)
The data enclosed with parethesis are the columns.,
To be more clear of all this are the columns in the table..
Table : Account
|Accntid|managedby|Accountholder|Description|AccountType|ContactPerson|ContactNumber|EmailAddress|
Table : Branch
|BranchID|BranchName|AccountID|StoreNo|Description|Status|
Table : Terminal
|TerminalID|TID|retail_store_code|t_distributor_code|
All I want is to Get this output with RIGHT data ..
|accntid|accountholder|tid|storeno|branchname|branchid|
This is what I tried for but not getting the RIGHT output
SELECT account.accntid,account.accountholder,terminal.tid,branch.storeno,branch.branchname,branch1.branchid
FROM ECPAYSERV2.ECPNWEB.dbo.account as account
INNER JOIN POSSQLSERVER.ECPNPOS.dbo.branch as branch
ON account.accntid=branch.branchid
INNER JOIN ECPAYSERV2.ECPNWEB.dbo.branch as branch1
ON account.accntid=branch1.branchid
INNER JOIN ECPAYSERV1.ECPNDB.dbo.terminal as terminal
ON account.accntid=terminal.tid
Please help me about This matter Thanks :(
SELECT
account.accntid, account.accountholder, terminal.tid, branch.storeno, branch.branchname, branch1.branchid
FROM
ECPAYSERV2.ECPNWEB.dbo.account as account
INNER JOIN POSSQLSERVER.ECPNPOS.dbo.branch as branch
ON account.accntid=branch.AccountID
INNER JOIN ECPAYSERV2.ECPNWEB.dbo.branch as branch1
ON account.accntid=branch1.AccountID
INNER JOIN ECPAYSERV1.ECPNDB.dbo.terminal as terminal
ON account.accntid=terminal.???
the ??? is not correct, but it is not clear which column of terminal is a FK for account.