Insert occasionally missing rows - sql

I am running below query, sometimes these query skip few records to insert into newordersholdentry and didn't get any error.
but if run same query again after finding it is missed some records for order, it will insert all.
Please let me know what could be the reason.
INSERT INTO newordersholdentry
(itemid,
lookupcode,
description,
gamacode,
ordered,
IsForceItem,
Scanned,
Location,
SortOrder
)
SELECT ID,
ItemLookupCode,
Description,
GamaCode,
SUM(Qty) AS Qty,
ForceItem,
0 AS Scanned,
SubDescription1,
Sortorder
FROM
(
SELECT Item.ID,
Item.ItemLookupCode,
Item.Description,
NewOrderItems.GamaCode,
NewOrderItems.Qty,
Item.SubDescription1,
Item.Binlocation,
ISNULL(
(
SELECT TOP (1) SortSno
FROM NewOrderPickPackSorting
WHERE(Bin = LEFT(Item.SubDescription1, 3))
), 99999) AS Expr1,
0 AS ForceItem,
p.sortorder
FROM NewOrderItems(NOLOCK)
INNER JOIN Item(NOLOCK) ON NewOrderItems.GamaCode = Item.SubDescription2
LEFT OUTER JOIN pickpath(NOLOCK) p ON concat(RTRIM(p.aisle), '-', p.section) = UPPER(LEFT(item.subdescription1, 6))
WHERE(NewOrderItems.Discontinue = 0)
AND (NewOrderItems.OrderID = 123456)
) AS t
Group by ID
, ItemLookupcode
, Description
, GAMACODE
, ForceItem
, Expr1
, BinLocation
, SubDescription1
, sortorder

Related

Where should i put the AS Clause for tax rate (VAT_RATE)

I want to put a AS VAT_RATE in this SELECT statement but i don't know where.
SELECT ROW_NUMBER() OVER(ORDER BY QD.DETAIL_ID) AS No,
QD.PRODUCT_ID AS PROD_ID,PM.'+#ProdCode+' AS PROD_CODE,pm.DESCRIPTION AS SHORT_DESC,
QD.CORPORATE_PRICE AS Corpo_Price,CONVERT(DECIMAL(18,2),QD.RETAIL_PRICE) AS UNIT_SP,QD.COST_PRICE AS COST_SP,
QD.GM,QD.DETAIL_ID,QD.DISC AS Discount,QD.NOTE,
VAT_RATE=(SELECT VAT_RATE/100 FROM dbo.vat
WHERE VAT_ID=(SELECT TOP 1 VAT_ID FROM dbo.product_detail(NOLOCK) WHERE PRODUCT_ID=PM.PROD_ID))
,
Img=(SELECT TOP 1 IMAGE_DATA FROM dbo.PRODUCT_IMAGE WHERE PRODUCT_ID=PM.PROD_ID), QD.CostPrice_Percentage
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
JOIN dbo.product_master(NOLOCK) PM ON PM.PROD_ID=QD.PRODUCT_ID
In TSQL you there is 3 way to name your columns
1) With the AS (optional in tsql)
SELECT QD.PRODUCT_ID AS PROD_ID
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
2) Without the AS (since it is optional)
SELECT QD.PRODUCT_ID PROD_ID
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
3) with an equal sign as if it is a formula
SELECT PROD_ID = QD.PRODUCT_ID
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
Specifically for your query this is where the AS should go.
You would have to remove the equal and put the AS at the end of the sub-query.
Please do note that you have various other issues with the queries that is beyond the scope your original question. If you run into preformance issue, do investigated on the subject of CROSS APPLY / CROSS OUTER JOIN and/or CTE : Common Table Expression.
SELECT ROW_NUMBER() OVER (
ORDER BY QD.DETAIL_ID
) AS No
, QD.PRODUCT_ID AS PROD_ID
--, PM.'+#ProdCode+' AS PROD_CODE
, #ProdCode AS PROD_CODE
, pm.DESCRIPTION AS SHORT_DESC
, QD.CORPORATE_PRICE AS Corpo_Price
, CONVERT(DECIMAL(18, 2), QD.RETAIL_PRICE) AS UNIT_SP
, QD.COST_PRICE AS COST_SP
, QD.GM
, QD.DETAIL_ID
, QD.DISC AS Discount
, QD.NOTE
, (
SELECT TOP 1 (VAT_RATE / 100)
FROM dbo.vat
WHERE VAT_ID = (
SELECT TOP 1 VAT_ID
FROM dbo.product_detail(NOLOCK)
WHERE PRODUCT_ID = PM.PROD_ID
)
) AS VAT_RATE
, (
SELECT TOP 1 IMAGE_DATA
FROM dbo.PRODUCT_IMAGE
WHERE PRODUCT_ID = PM.PROD_ID
) AS Img
, QD.CostPrice_Percentage
FROM dbo.CUSTOMER_QUOTATION_DETAIL(NOLOCK) QD
JOIN dbo.product_master(NOLOCK) PM
ON PM.PROD_ID = QD.PRODUCT_ID

always get a "not a GROUP BY expression" exception

The select part is fine, I can run it and get result, but if I insert the query result into a table, the "not a group by expression" exception was thrown, see my sql statement below:
INSERT INTO RAWDATA_FACT
(
LINEITEMID
,CALENDARYEAR
,CALENDARQUARTER
,CALENDARMONTH
,DEPARTMENTID
,PRODUCTID
,DEALERID
,ACTUALVALUE
,TARGETVALUE
,AGGREGATION
,TODATE
,CREATEDATE
,BATCH_ID
)
select parentid
,calendaryear
,calendarquarter
,calendarmonth
,departmentid
,productid
,dealerid
,sum(case when unaryoperator = '-' then actualvalue * (-1)
when unaryoperator = '~' then actualvalue * 0
else actualvalue
end
) actualvalue
,sum(case when unaryoperator = '-' then targetvalue * (-1)
when unaryoperator = '~' then targetvalue * 0
else targetvalue
end
) targetvalue
,aggregation
,todate
,sysdate createdate
,'201808' batch_id--v_batch_ID
from
(
select
x.parentid
, x.unaryoperator
, y.calendaryear
, y.calendarquarter
, y.calendarmonth
, y.departmentid
, y.productid
, y.dealerid
, y.actualvalue
, y.targetvalue
, y.aggregation
, y.todate
from
(select substr(lineitemid,instr(lineitemid,'_')+1) lineitemid,
parentid, unaryoperator from lineitem_temp where levelid = 14 /*v_cur_level*/) x
inner join
(select lineitemid,calendaryear, calendarquarter, calendarmonth, departmentid, productid, dealerid,
coalesce(actualvalue,0) actualvalue,coalesce(targetvalue,0) targetvalue,
aggregation, todate
from RAWDATA_FACT where BATCH_ID = '201808'/*v_batch_ID*/) y --eg. 201809
on x.lineitemid = y.lineitemid
--parent node's id contains "_" will not take part in calculation from current level to parent level
where regexp_like(x.parentid , '^\d+$') and not exists
--parent node contains formula will not participate calculation from current level to parent level
(select 1 from LINEITEM_TEMP where lineitemid = x.parentid and custommember is not null)
) t
GROUP BY
t.parentid
, t.calendaryear
, t.calendarquarter
, t.calendarmonth
, t.departmentid
, t.productid
, t.dealerid
, t.aggregation
, t.todate
;
who can tell me why? is there a way to insert the result into that table? If I run the select parts, it works fine, but if I want to insert the result into that table, it reports that exception.

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:

use recently defined alias in sql in case when

I'm trying to clean up a product column in a sql table when querying. My (fake example) query looks like the following:
select
Id
, Name
, ProductName
, CASE
WHEN ProductName IN ('macbook', 'dell', 'air', 'hp') THEN 'Laptop'
WHEN ProductName IN ('ipod', 'walkman', 'headset') THEN 'Music_Device'
WHEN ProductName IN ('mop', 'broom', 'sponge') THEN 'Household Utilities'
WHEN ProductName IN ('bike', 'bike_2', 'bike_3') THEN 'Bicycle'
WHEN ProductName IN ('tesla', 'ford', 'prius') THEN 'Car'
ELSE null
END AS Prod_Group
, CASE
WHEN Prod_Group IN ('Laptop', 'Music_Device') THEN 'Electronics'
WHEN Prod_Group IN ('Bicycle', 'Car') THEN 'Transportation'
WHEN Prod_Group IN ('Household Utilities') THEN 'Utilities'
ELSE null
END AS Line_of_Business
from prod_database
Is there no way to reference an aliased column? Would I have to repeat my whole case when statement and change Laptop to Electronics etc.? I'm coming from R and learning SQL, so this is a bit new to me.
In Sql Server, we can use Cross Apply
SELECT Id,
NAME,
ProductName,
cs.Prod_Group,
CASE
WHEN cs.Prod_Group IN ( 'Laptop', 'Music_Device' ) THEN 'Electronics'
WHEN cs.Prod_Group IN ( 'Bicycle', 'Car' ) THEN 'Transportation'
WHEN cs.Prod_Group IN ( 'Household Utilities' ) THEN 'Utilities'
ELSE NULL
END AS Line_of_Business
FROM prod_database
CROSS apply (SELECT CASE
WHEN ProductName IN ( 'macbook', 'dell', 'air', 'hp' ) THEN 'Laptop'
WHEN ProductName IN ( 'ipod', 'walkman', 'headset' ) THEN 'Music_Device'
WHEN ProductName IN ( 'mop', 'broom', 'sponge' ) THEN 'Household Utilities'
WHEN ProductName IN ( 'bike', 'bike_2', 'bike_3' ) THEN 'Bicycle'
WHEN ProductName IN ( 'tesla', 'ford', 'prius' ) THEN 'Car'
ELSE NULL
END) cs (Prod_Group)
You could refer to with a CTE I suppose...
WITH CTE
AS
(
SELECT CASE WHEN 1>0 THEN 'A' END AS derivedCol
)
SELECT CASE WHEN derivedCol < 'B' THEN 'Pos1' ELSE 'Other' END
FROM CTE
--could use a real table here
declare #prodTypes as table
(ProductName varchar(50),
ProductGroup varchar(50),
Line_of_Business varchar(50));
insert into #prodTypes
values ('macbook', 'Laptop', 'Electronics'),
('dell', 'Laptop', 'Electronics'),
('hp', 'Laptop', 'Electronics'),
('ipod', 'Music_Device', 'Electronics')
-- etc.
-- the results you want:
select
p.Id
, p.Name
, p.ProductName
, pt.ProductGroup
, pt.Line_of_Business
from prod_database p
left join #prodTypes pt
on p.ProductName = pt.ProductName
-- to double check your settings you can do:
select Line_of_business, ProductGroup, ProductName
from #prodTypes
order by Line_of_business, ProductGroup, ProductName

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'