column ambiguously defined on using offset and fetch first - sql-server

SELECT
qplt.description,
qplab.status_code,
qplab.start_date,
qplc.start_date,
qplc.end_date
FROM
price_lists_dur qplab,
PRICE_LISTS_Tbl qplt,
PRICE_LIST_CHARGES qplc
WHERE
qplt.price_list_id = qplab.price_list_id
AND qplt.price_list_id = qplc.price_list_id
OFFSET 10 ROWS FETCH FIRST 40 ROWS ONLY
The above code returns an error.
But when I remove the last line offset fetch first, it works fine.
Can someone help with the query?

ORDER BY is mandatory to use OFFSET and FETCH clause
So use:
SELECT
qplt.description,
qplab.status_code,
qplab.start_date,
qplc.start_date,
qplc.end_date
FROM price_lists_dur qplab,
PRICE_LISTS_Tbl qplt,
PRICE_LIST_CHARGES qplc
WHERE qplt.price_list_id=qplab.price_list_id
AND qplt.price_list_id =qplc.price_list_id
ORDER BY qplab.status_code --(or the column you want)
OFFSET 10 ROWS FETCH FIRST 40 ROWS ONLY

to fix this try to give different aliases to your similar columns, so for example :
SELECT
qplt.description,
qplab.status_code,
qplab.start_date,
qplc.start_date as qplc_start_date, --notice the alias
qplc.end_date
FROM
price_lists_dur qplab,
PRICE_LISTS_Tbl qplt,
PRICE_LIST_CHARGES qplc
WHERE
qplt.price_list_id = qplab.price_list_id
AND qplt.price_list_id = qplc.price_list_id
OFFSET 10 ROWS FETCH FIRST 40 ROWS ONLY

Related

Script to find the the number of rows those were update in the last 1hr

This script does give me the list of changed rows but I also need to make sure that the number of changed rows should be equal to the number of rows those were update in the last 1 hour which would give me more comfort level on validation.
Here is the code which gives me the list of changed rows. Except does give me all the rows from the first select statement which are not in the second select if I am not wrong. I am just wondering how to check the number of rows that were updated in the last hr which must match the count of rows when I run this below query.
Select [Accounting_Period_ID]
,[Policy_Number]
,[Risk_ID]
,[Product_ID]
,[Inception_Date_ID]
,[Effective_Date_ID]
,[Expiration_Date_ID]
,[Cancellation_Date_ID]
,[Reinstate_Date_ID]
,[Policy_Source_System_ID]
,[Risk_Geo_ID]
,[Risk_Profile_ID]
,[Policy_Status_ID]
,[Agency_ID]
,[Limit_Selection_ID]
,[Written_Premium_MTD]
,[Written_Premium_ITD]
,[Fees_MTD]
,[Fees_ITD]
,[Commission_MTD]
,[Commission_ITD]
,[Earned_Premium_MTD]
,[Earned_Premium_ITD]
,[In_Force_Count]
,[New_Business_Count]
,[Renewed_Count]
,[Cancelled_Count]
,[Reinstated_Count]
,[Dwelling_Limit]
,[Other_Structures_Base_Limit]
,[Other_Structures_Extended_Limit]
,[Other_Structures_Total_Limit]
,[Contents_Limit]
,[Additional_Living_Expense_Limit]
,[Liability_Limit]
,[Medical_Limit]
,[Total_Insured_Value]
,[Replacement_Value]
,[AOP_Deductible]
,[Days_in_Force]
,[Earned_House_Years]
,[Cancellation_Entry_Date_ID]
,[Reinstate_Entry_Date_ID]
,[Seq]
,[Inserted_Date]
,[Inserted_By]
,[Last_Updated_Date]
,[Last_Updated_By]
,[Insurance_score]
,[Rewrite_Count]
,[Entry_Date_ID] from Datamart.Policy.Fact_Monthly_Policy_Snap_20190403
where Policy_Source_System_ID = 8
EXCEPT
Select [Accounting_Period_ID]
,[Policy_Number]
,[Risk_ID]
,[Product_ID]
,[Inception_Date_ID]
,[Effective_Date_ID]
,[Expiration_Date_ID]
,[Cancellation_Date_ID]
,[Reinstate_Date_ID]
,[Policy_Source_System_ID]
,[Risk_Geo_ID]
,[Risk_Profile_ID]
,[Policy_Status_ID]
,[Agency_ID]
,[Limit_Selection_ID]
,[Written_Premium_MTD]
,[Written_Premium_ITD]
,[Fees_MTD]
,[Fees_ITD]
,[Commission_MTD]
,[Commission_ITD]
,[Earned_Premium_MTD]
,[Earned_Premium_ITD]
,[In_Force_Count]
,[New_Business_Count]
,[Renewed_Count]
,[Cancelled_Count]
,[Reinstated_Count]
,[Dwelling_Limit]
,[Other_Structures_Base_Limit]
,[Other_Structures_Extended_Limit]
,[Other_Structures_Total_Limit]
,[Contents_Limit]
,[Additional_Living_Expense_Limit]
,[Liability_Limit]
,[Medical_Limit]
,[Total_Insured_Value]
,[Replacement_Value]
,[AOP_Deductible]
,[Days_in_Force]
,[Earned_House_Years]
,[Cancellation_Entry_Date_ID]
,[Reinstate_Entry_Date_ID]
,[Seq]
,[Inserted_Date]
,[Inserted_By]
,[Last_Updated_Date]
,[Last_Updated_By]
,[Insurance_score]
,ISNULL([Rewrite_Count],0) Rew
,[Entry_Date_ID] from Datamart.Policy.Fact_Monthly_Policy_Snap
where Policy_Source_System_ID = 8
DATEADD(hh,-1,GETDATE()) gives you the actual Time minus 1 hour .this you can compare with Last_Updated_Date.
Count(*) gives you the number of rows.

How to remove space when concatenating data from different rows into one column using xml?

I am trying to combine data from different rows into one column, and this is working with just one minor problem.
declare #RitID int = 16
select ...,
( select distinct
ISNULL(LTRIM(RTRIM(r2.LotNr)), LTRIM(RTRIM(r.LotNr))) + '+' as 'data()'
from tblExtraBestemming eb2
inner join tblRit r2 on eb2.RitID = r2.RitID
where eb2.BestemmingID = eb.BestemmingID
and eb2.BestemmingTypeID = eb.BestemmingTypeID
and ( (eb.CombinedChildExtraBestemmingID is null and eb2.RitID = #RitID)
or
(eb.CombinedChildExtraBestemmingID is not null and eb2.RitID in (select r4.RitID from tblRit r4 where r4.MasterRitID = #RitID) )
)
for XML PATH('')
) as LotNr
from tblExtraBestemming eb
where ...
this returns the correct data for the column LotNr, like this
GTT18196
GTT18197
GTT18198+ GTT18199
Now my only problem is the space after the + sign in the third row from the result, how can I get rid of this ?
I expect this result
GTT18196
GTT18197
GTT18198+GTT18199
PS, actually there is also a + at the end of each row, but that is removed by the client. I thought I better mentions this already.
EDIT
I checked the data, there are no spaces at the end or the beginning of the data
EDIT
Query updated as suggested by #Larnu
EDIT
if I check the data in the table, this is the result
select '/' + r.LotNr + '/' from tblRit r where r.RitID in (50798, 50799)
COLUMN1
-------
/GTT18198/
/GTT18199/
So it appears to me there are no characters before or after the data
Just remove AS 'data()' from your query (it is not required in the above case).
And if trailing + is a problem, move it to the beginning and use STUFF function to chop off the first character from the result.

Count the 'X' then arrange the value as row in SQL

I have a table in SQL that have many columns which the value of each columns in every row is either ' ' or 'X'. I need to count this 'X' for every columns which can be done by following code;
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
The result of the query is;
GVI0 NDT0 GVI1 NDT1 GVI2 NDT2
11 11 2 4 11 11
However, (in my understanding) in order for this count value to be bind into ASP.net Chart Control with multiple series name 'GVI' and 'NDT', I need to make the column into row for the DataTable.
I try to use UNPIVOT in SQL like this;
SELECT GVI0Count
FROM (
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
)
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0)) AS unpivott
but it seem that the code is wrong.
How do I do this?
I think the following might work for you. At least, as a start.
SELECT *
FROM (
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
) P
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0, GVI1, GVI2)) AS unpivott

Convert Statement to Crystal Reports SQL Expression

I have a SQL command that works great in SQL Server. Here's the query:
SELECT TOP 1000
(
SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
)
- (SELECT COUNT(LINENUM)
FROM OEORDD D1
WHERE D1.ORDUNIQ = OEORDD.ORDUNIQ
AND D1.LINENUM > OEORDD.LINENUM)
FROM OEORDD
ORDER BY ORDUNIQ, LINENUM
The query looks at the total lines on an order, then looks at the current "LINENUM" field. With the value of the LINENUM field, it looks to see how many lines have a greater LINENUM value on the order and subtracts it from the number of lines on an order to get the correct Line number.
When I try to add it as a SQL expression in version 14.0.2.364 as follows:
(
(
SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
)
- (SELECT COUNT("OEORDD"."LINENUM")
FROM "OEORDD" "D1"
WHERE "D1"."ORDUNIQ" = "OEORDD"."ORDUNIQ"
AND "D1"."LINENUM" > "OEORDD"."LINENUM"
)
)
I get the error "Column 'SAMDB.dbo.OEORDD.ORDUNIQ' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I try to add GROUP BY "OEORDD"."ORDUNIQ" at the end, I get "Incorrect syntax near the keyword 'GROUP'. I've tried adding "FROM OEORDD" at the end of query and it errors out on the word "FROM". I have the correct tables linked in the Database Expert.
EDIT --------------
I was able to get the first query working by getting rid of the alias, it's as follows:
(
SELECT COUNT(LINENUM)
FROM OEORDD
WHERE OEORDH.ORDUNIQ=OEORDD.ORDUNIQ)
)
However, I believe I need to use the alias in the second query to compare line numbers. I'm still stuck on that one.

Update query failed '0' rows affected

I am trying to run this query but the query keeps giving up on me:
Update StockInvoiceInfo set Quantity = Quantity - 2 where p_id = 5 AND ProductDate = convert(Cast('31-5-2015' as datetime)) ;
After Running this code it returns an error below:
Incorrect syntax near '31-5-2015'
The datatype of the ProductDate column isDate. I am using Sql Server 2012.
You have used Convert functions but didn't supplied it with parameters. Also there is no need for this function here. Also take care of date format. I have changed it to standard format:
Update StockInvoiceInfo set Quantity = Quantity - 2
where p_id = 5 AND ProductDate = Cast('2015-05-31' as datetime)
If all you are trying to do is compare a Sql Date, then just use an agnostic format like '20150531' or easier to read '2015-05-31'. No need for casts or convert at all, i.e.
WHERE ... AND ProductDate = '2015-05-31'
However, if ProductDate isn't a date, but one of the *DATETIME* data types, and you are looking to update any time on the same day, then I believe you are looking for something like:
Update StockInvoiceInfo
set Quantity = Quantity - 2
where
p_id = 5
AND CAST(ProductDate AS DATE) = '2015-05-31';
However, the performance will be lousy as the clause isn't likely to be SARGable. You're better off simply doing:
AND ProductDate >= '2015-05-31' AND ProductDate < '2015-06-01';
(Resist the temptation to use between, or hacks like ':23:59:59' as there will be data corner cases which will bite you)
use CAST('5-31-2015' as DATETIME)
with the above update statement you started convert but with incomplete syntax
here the convert syntax
Update StockInvoiceInfo
set Quantity = Quantity - 2
where p_id = 5
AND ProductDate = convert(datetime,Cast('31-5-2015' as varchar),103) ;

Resources