How to retrieve the records of the table from database to another database - sql

I have two tables with different database:
AccntID from table 'account' <--database name 'ECPNWEB'
AccountTID from table 'tblPolicy' <--database name 'GENESIS'
Now I want to insert 'tblPolicy' like this: <--database 'GENESIS'
INSERT INTO dbo.tblPolicy
(
PolicyID ,
AccountTID ,
DistributorID ,
CARDNAME ,
DENOMINATION ,
RETAILPRICE ,
COSTPAYABLE ,
ECPAYFEES ,
PLUCODE
)
-- Insert statements for procedure here
select t.* from
(Select AccountTID=#AccntID, DistributorID=#DistributorID, CARDNAME=#CARDNAME, DENOMINATION=#DENOMINATION, RETAILPRICE=#RETAILPRICE, COSTPAYABLE=#COSTPAYABLE, ECPAYFEES=#ECPAYFEES, PLUCODE=#PLUCODE) t,
account a
where a.AccntID = t.AccountTID --for account
Now what I want to do is to insert this "ONLY" to tblPolicy connected with the 'account' table with different database 'GENESIS'

You can select from two databases as shown below:
SELECT table1.SomeField, table2.SomeField
FROM [ServerName1].[Database1].[dbo].[Table1] table1
INNER JOIN [ServerName2].[Database2].[dbo].[Table2] table2
ON table1.SomeField = table2.SomeField

The key point is [ServerName].[DatabaseName].[databaseowner].[tableName]..i.e. Fully qualified name
This should work
;With Cte As
(
Select AccountTID=#AccntID
, DistributorID=#DistributorID
, CARDNAME=#CARDNAME
, DENOMINATION=#DENOMINATION
, RETAILPRICE=#RETAILPRICE
, COSTPAYABLE=#COSTPAYABLE
, ECPAYFEES=#ECPAYFEES
, PLUCODE=#PLUCODE
)
INSERT INTO GENESIS..dbo.tblPolicy
(
PolicyID ,
AccountTID ,
DistributorID ,
CARDNAME ,
DENOMINATION ,
RETAILPRICE ,
COSTPAYABLE ,
ECPAYFEES ,
PLUCODE
)
Select t.*
From Cte t,ECPNWEB..account a WITH (NOLOCK)
where a.AccntID = t.AccountTID

Related

An efficient way to group columns in MonetDB

I have the next INSERT with SELECT in MonetDB:`
insert into colombia.agregada_region_mes
(
cod_anomes
, cod_produto
, sg_estado
, cod_subcanal
, qtd_vendidas
, valor
, valor_dolar
, valor_euro
, fact_count
)
select
f.cod_anomes
, f.cod_produto
, f.sg_estado
, f.cod_subcanal
, sum(f.qtd_vendidas) as qtd_vendidas
, sum(f.valor) as valor
, sum(f.valor_dolar) as valor_dolar
, sum(f.valor_euro) as valor_euro
, count(*) as fact_count
from colombia.staging_rm_fact f
group by
f.cod_anomes
, f.cod_produto
, f.sg_estado
, f.cod_subcanal;
(Please, note the GROUP BY part).
Table "staging_rm_fact" has 50 million of rows, and MonetDB exceeds 16Gb of memory trying to resolve the INSERT:
Is there any other efficient way to resolve this group by?

sql count new id that did not exists before for each month

I have the follow set of data
enter image description here
how can I write the sql to gives the result on right side?
that is the counting of unique id that did appeared previously for each month.
After long time of reading and reading his question, Ssiu wanted to ask the following:
So here is the test data in MS SQL: at that time he didn't clarify on postgresql
create table tmp1 (
ddate datetime
, iid int
)
insert into tmp1 values
('2017-11-01',1)
,('2017-11-02',2)
,('2017-11-03',3)
,('2017-11-04',4)
,('2017-11-05',5)
,('2017-11-06',5)
,('2017-11-07',5)
,('2017-12-01',1)
,('2017-12-02',2)
,('2017-12-03',3)
,('2017-12-04',6)
,('2017-12-05',7)
,('2018-01-01',1)
,('2018-01-02',2)
,('2018-01-03',3)
,('2018-01-04',4)
,('2018-01-05',8)
Disclaimer: The following is not the best approach for this problem. It is not applicable for more months, however it can give Ssiu a clue.
with cte(mmonth, iid) as (
select distinct convert(varchar(7), ddate, 120) mmonth
, iid
from tmp1
)
, cte_201711 as (
select * from cte where mmonth = '2017-11'
)
, cte_201712 as (
select * from cte where mmonth = '2017-12'
)
, cte_201801 as (
select * from cte where mmonth = '2018-01'
)
, cte_cnt201712 as(
select cte_201711.mmonth as mm201711
, cte_201711.iid as id201711
, cte_201712.mmonth as mm201712
, cte_201712.iid as id201712
from cte_201711
full outer join cte_201712
on cte_201712.iid = cte_201711.iid
)
, cte_cnt201801 as (
select cte_201711.mmonth as mm201711
, cte_201711.iid as id201711
, cte_201712.mmonth as mm201712
, cte_201712.iid as id201712
, cte_201801.mmonth as mm201801
, cte_201801.iid as id201801
from cte_201711
full outer join cte_201712
on cte_201712.iid = cte_201711.iid
full outer join cte_201801
on cte_201801.iid = cte_201712.iid
or cte_201801.iid = cte_201711.iid
)
--select * from cte_cnt201801 order by isnull(mm201711,'z'), isnull(mm201712,'z')
select '2017-12' mmonth, count(*) Ssiu
from cte_cnt201712
where mm201711 is null
union all
select '2018-01' mmonth, count(*) Ssiu
from cte_cnt201801
where mm201711 is null
and mm201712 is null
Note the data for the cte_cnt201801 CTE:
select * from cte_cnt201801 order by isnull(mm201711,'z'), isnull(mm201712,'z')
So the result for the above query is:

ORA-30926: unable to get a stable set of rows in the source tables

I have a customer who gets: ORA-30926: unable to get a stable set of rows in the source tables:
Log show error Massage Error (30926)
13:52:19 (00:00:02.406) ERROR : Error (30926) (00:00:02.406) ORA-30926: Stabile Zeilengruppe in den Quelltabellen kann nicht eingelesen werden
TS03_MIN0100: UpdTable failed. Update inv_value in cMinTimeTable:
MERGE INTO HUBWBPMS5_ENTTS03005400223 a USING ( SELECT DISTINCT a.inv_value +
( a.inv_value_sum - h.inv_value ) AS inv_value , a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h , HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no AND a.sequence_no = h.max_seq_no
AND a.client = h.client ) xzfd_t ON ( xzfd_t.xzfd_rid = a.rowid )
WHEN MATCHED THEN
UPDATE
SET a.inv_value = xzfd_t.inv_value
I have checked for duplicate values in the tables but cant find anything unusual.
Maybe someone has an idea that could be useful.
The query is:
Query causing error (temp table):
INSERT INTO HUBWBPMS5_ENTTS03005700228 ( agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , inc_ref , inv_value , max_seq_no , pd , period , project , resource_id , resource_typ , trans_date , unit , voucher_no , work_order , work_type )
SELECT agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , inc_ref , SUM ( inv_value ) inv_value , max_seq_no , pd , period , project , resource_id , resource_typ , trans_date , unit , voucher_no , work_order , work_type
FROM HUBWBPMS5_ENTTS03005400228
WHERE agg_flag = 1
GROUP BY agg_flag , ace_code , activity , category , client , cost_dep , description , dim1 , dim2 , dim3 , dim4 , period , trans_date , voucher_no , max_seq_no , inc_ref , pd , project , resource_id , resource_typ , unit , work_order , work_type
When you get that error, it will be from a MERGE statement, and it indicates that there are multiple rows in the source dataset that match to a row you're joining to in the target table, and as such, Oracle doesn't know which one to use to do the update.
Taking your merge statement:
MERGE INTO HUBWBPMS5_ENTTS03005400223 a
USING (SELECT DISTINCT a.inv_value + ( a.inv_value_sum - h.inv_value ) AS inv_value,
a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h,
HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no
AND a.sequence_no = h.max_seq_no
AND a.client = h.client) xzfd_t
ON (xzfd_t.xzfd_rid = a.rowid)
WHEN MATCHED THEN
UPDATE SET a.inv_value = xzfd_t.inv_value;
it looks like the join between the two tables HUBWBPMS5_ENTTS03005700223 and HUBWBPMS5_ENTTS03005400223 in the xzfd_t subquery causes multiple rows to be returned for one or more of the HUBWBPMS5_ENTTS03005400223 rows (ie. you get multiple rows returned for at least one a.rowid).
To check this, run:
SELECT xzfd_rid,
COUNT(*) cnt
FROM (SELECT DISTINCT a.inv_value + ( a.inv_value_sum - h.inv_value ) AS inv_value,
a.rowid xzfd_rid
FROM HUBWBPMS5_ENTTS03005700223 h,
HUBWBPMS5_ENTTS03005400223 a
WHERE a.voucher_no = h.voucher_no
AND a.sequence_no = h.max_seq_no
AND a.client = h.client)
GROUP BY xzfd_rid
HAVING COUNT(*) > 1;
In order to fix this, you'd need to make the xzfd_t subquery return a single row for each xzfd_rid. Possibly using row_number() to pick a single row, or an aggregate query to sum up all the h.inv_value fields per a.rowid instead of the DISTINCT.

Add CDept_Id In first query for result

In my first query, I want to get CDept_Id. But CDept_Id column does not exist in inward_doc_tracking_hdr table.
It comes from inward_doc_tracking_trl table. like below
SELECT CDept_id
FROM inward_doc_tracking_trl
WHERE ref_mkey IN ( SELECT mkey
FROM inward_doc_tracking_hdr
WHERE doc_no = 'IW/HU/16/42' )
So, From this. I get CDept_Id. Now I want to add this in my below query.
SELECT mkey ,
Delivered_By ,
Department_Id ,
( SELECT mkey
FROM erp190516.dbo.emp_mst
WHERE mkey IN ( SELECT employee_mkey
FROM erp190516.dbo.user_mst
WHERE mkey = To_User )
) User_Id ,
Doc_Type ,
Email_Id ,
Ref_No ,
CONVERT(VARCHAR(25), Ref_date, 103) Ref_date ,
Inward_Amt ,
Remarks ,
party_name ,
disp_through
FROM erp190516.dbo.inward_doc_tracking_hdr ,
CDept_id -- add CDept_id here
WHERE doc_no = 'IW/HU/16/42'
AND Status_Flag = '13'
How to add this
UPDATE
inward_doc_tracking_hdr mkey is equal to inward_doc_tracking_trl ref_mkey
It is reading the magic glass bulb, but I think you might nead an INNER JOIN to the other table using the mkey and ref_mkey as link:
Select hdr.mkey
,hdr.Delivered_By
,hdr.Department_Id
,hdr.Doc_Type,Email_Id
,hdr.Ref_No
,convert(varchar(25),hdr.Ref_date,103) Ref_date
,hdr.Inward_Amt
,hdr.Remarks
,hdr.party_name
,hdr.disp_through
,trl.CDept_Id
from erp190516.dbo.inward_doc_tracking_hdr AS hdr
inner join erp190516.dbo.inward_doc_tracking_trl AS trl on hdr.mkey=trl.ref_mkey
where hdr.doc_no = 'IW/HU/16/42'
and hdr.Status_Flag = '13'
UPDATE ...even more guessing...
--First CTE to get the partioned order of CDept_Id
;WITH OrderedCDept AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY ref_mkey ORDER BY CDept_Id DESC) AS SortInx
,ref_mkey
,CDept_Id
FROM erp190516.dbo.inward_doc_tracking_trl
)
--Second CTE to use TOP 1 WITH TIES to fetch all first rows
,LatestCDept AS
(
SELECT TOP 1 WITH TIES *
FROM OrderedCDept
ORDER BY SortInx
)
--Now use the second CTE instead of the table to join
Select hdr.mkey
,hdr.Delivered_By
,hdr.Department_Id
,hdr.Doc_Type,Email_Id
,hdr.Ref_No
,convert(varchar(25),hdr.Ref_date,103) Ref_date
,hdr.Inward_Amt
,hdr.Remarks
,hdr.party_name
,hdr.disp_through
,trl.CDept_Id
from erp190516.dbo.inward_doc_tracking_hdr AS hdr
inner join LatestCDept AS trl on hdr.mkey=trl.ref_mkey
where hdr.doc_no = 'IW/HU/16/42'
and hdr.Status_Flag = '13'

Column 'city.POPULATION' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I was creating a query, and i saw this error-message.
This is the query i'm trying to execute:
select D.ID_city
, D.ID_year
, D.NB_deaths
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, COUNT( NB_births_M+ NB_births_F)/C.population as Birthrate
, COUNT(NB_deaths_M + NB_deaths_F) as Mortality
from Demography D
join region C
on (D.Id_city =C.Id_city)
group
by D.ID_city
, D.ID_year
, D.NB_deaths_M
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
You should include c.population in the group by, since you use that column outside the count aggregate function:
COUNT( NB_births_M+ NB_births_F)/C.population
Use this (added D.NB_deaths_F too since it is necessary too):
select D.ID_city
, D.ID_year
, D.NB_deaths
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, COUNT( NB_births_M+ NB_births_F)/C.population as Birthrate
, COUNT(NB_deaths_M + NB_deaths_F) as Mortality
from Demography D
join region C
on (D.Id_city =C.Id_city)
group
by D.ID_city
, D.ID_year
, D.NB_deaths_M
, D.NB_deaths_F -- <-- added
, D.NB_births_M
, D.NB_births_F
, D.NB_population_AS
, D.NB_population_ACL
, C.population -- <-- added