How to select year-1?
This is my code:
select
a.*
from
(select
met_men, kli_kod, pre_kod, galutinis, savik_group, marza,
KLR_KOD, KLI_POZ1, KLI_POZ2, KLI_POZ3, KLG_KOD, PRE_RUS,
PRE_POZ1, PRE_POZ2, PRE_POZ3, PRE_POZ4, PRE_POZ5, PRE_POZ6,
did_dat, savi_suproc, marza_suproc, pre_ska dbo.SVF_View_10) AS a
left outer join
(select
pre_kod, kli_kod, met_men, did_dat
from
dbo.SVF_View_10_sum
where
dateadd(year, -1, 'did_dat')) as b on a.kli_kod = b.kli_kod
and a.pre_kod = b.pre_kod
and a.did_dat = b.did_dat
This error occurs on the line where DATEADD(year, -1, 'did_dat')) as b:
Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Please help me
the required data: order date, id, quantity, orderdate-1 year, quantity. It is necessary to compare the quantity sold for this year and for the last year
You give column name in single quote dateadd(year, -1, 'did_dat') which is no need here change into dateadd(year, -1, did_dat) and when you use where clause need to give comparison operation in where clause.
so changed into
where did_dat <= dateadd(year, -1, did_dat)
but it doesn't make any sense because query should be execute on previous year against current date like
where did_dat <= dateadd(year, -1, GETDATE())
I suspect what you want is:
select . . .
from dbo.SVF_View_10 v left outer join
dbo.SVF_View_10_sum vs
on v.kli_kod = vs.kli_kod and a.pre_kod = b.pre_kod and
v.did_dat = dateadd(year, -1, vs.did_dat);
Notes:
Subqueries are not necessary for this query. I think they just make the query harder to write and to read.
Use table aliases that are abbreviations for the table names.
You have a date calculation in the where clause rather than a boolean expression. That is causing your error.
The date expression has a string for the third argument. The would be your next error. Strings do not refer to column names.
I am speculating that you want the tables joined on that date expression.
Try This :-
SELECT a.*
FROM
(
SELECT met_men,kli_kod,pre_kod,galutinis,savik_group,marza,KLR_KOD,LI_POZ1,
KLI_POZ2,KLI_POZ3,KLG_KOD,PRE_RUS,PRE_POZ1,PRE_POZ2,PRE_POZ3,PRE_POZ4,
PRE_POZ5,PRE_POZ6,did_dat,savi_suproc,marza_suproc,pre_ska
FROM SVF_View_10_sum
) AS a
LEFT OUTER JOIN
(
SELECT pre_kod,kli_kod,met_men,did_dat
FROM dbo.SVF_View_10_sum
WHERE did_dat = DATEADD(YEAR, -1, did_dat)
) AS b
ON a.kli_kod = b.kli_kod AND a.pre_kod = b.pre_kod AND a.did_dat = b.did_dat;
Related
I have two tables where I am attempting to join the results into one. I am trying to get the INV_QPC which is the case pack size shown in the results (SEIITN and SKU) are the same product numbers.
The code below gives two results, but the goal is to get the bottom result into the main output, where I was hoping the join would be the lookup to show the case pack size in relation to SKU.
INV_QPC = case pack size
SKU = SKU/Product Number
SEIITN = SKU/Product Number
Thanks for looking.
SELECT
ORDER_QTY, SKU, INVOICE_NUMBER, CUSTOMER_NUMBER, ROUTE,
ALLOCATED_QTY, SHORTED_QTY, PRODUCTION_DATE,
DATEPART(wk, PRODUCTION_DATE) AS FISCAL_WEEK,
YEAR(PRODUCTION_DATE) AS FISCAL_YEAR,
CONCAT(SKU, CUSTOMER_NUMBER) AS SKU_STORE_WEEK
FROM
[database].[dbo].[ORDERS]
WHERE
[PRODUCTION_DATE] >= DATEADD(day, -3, GETDATE())
AND [PRODUCTION_DATE] <= GETDATE()
SELECT INV_QPC
FROM [database].[dbo].[PRODUCT_MASTER]
JOIN [database].[dbo].[ORDERS] ON ORDERS.SKU = PRODUCT_MASTER.SEIITN;
It looks like you are on the right track, but your second SQL statement is only returning the INV_QPC column, so it is not being joined to the first query. Here is an updated SQL statement that should give you the result you are looking for:
SELECT
ORD.ORDER_QTY, ORD.SKU, ORD.INVOICE_NUMBER, ORD.CUSTOMER_NUMBER, ORD.ROUTE,
ORD.ALLOCATED_QTY, ORD.SHORTED_QTY, ORD.PRODUCTION_DATE,
DATEPART(wk, ORD.PRODUCTION_DATE) AS FISCAL_WEEK,
YEAR(ORD.PRODUCTION_DATE) AS FISCAL_YEAR,
CONCAT(ORD.SKU, ORD.CUSTOMER_NUMBER) AS SKU_STORE_WEEK,
PROD.INV_QPC
FROM
[database].[dbo].[ORDERS] ORD
JOIN [database].[dbo].[PRODUCT_MASTER] PROD ON ORD.SKU = PROD.SEIITN
WHERE
ORD.PRODUCTION_DATE >= DATEADD(day, -3, GETDATE())
AND ORD.PRODUCTION_DATE <= GETDATE()
In this query, I have added the INV_QPC column to the SELECT statement, and also included the join condition in the JOIN clause. Additionally, I have given aliases to the tables in the FROM and JOIN clauses to make the query easier to read. Finally, I have updated the WHERE clause to reference the ORD alias instead of the table name directly.
I know very little SQL and have been asked to fix a problem in existing code. The code is PHP but the SQL causing the problem is:
$sql = "INSERT INTO Intranet.dbo.DailyBilling (Date, JobCode, SubJob, TotalTTC, TotalATTC, CompletedBillableHours, WIP, CurrencyCode, ContractValue, Invoiced, BillableTotal, BillableToday)
SELECT
Date = CONVERT(VARCHAR(10), dateadd(day,-1, getdate()), 111),
JobCode,
vwJobValueVsInvoiced.SubJob,
TotalTTC,
TotalATTC = ATTC,
CompletedBillableHours,
WIP,
vwJobValueVsInvoiced.CurrencyCode,
ContractValue,
vwJobValueVsInvoiced.Invoiced,
BillableTotal = IIF(TotalTTC <> 0,((CompletedBillableHours/TotalTTC)*ContractValue), 0),
BillableToday = IIF(TotalTTC <> 0,
IIF(Yesterday.InvoicedYesterday <> vwJobValueVsInvoiced.Invoiced,
((CompletedBillableHours/TotalTTC)*ContractValue)-vwJobValueVsInvoiced.Invoiced ,
(((CompletedBillableHours/TotalTTC)*ContractValue)-vwJobValueVsInvoiced.Invoiced)- (Yesterday.BillableTotal-Yesterday.InvoicedYesterday)),
0)
FROM
Intranet.dbo.vwJobValueVsInvoiced
LEFT JOIN
Intranet.dbo.vwCurrentRate ON vwJobValueVsInvoiced.CurrencyCode = vwCurrentRate.CurrencyCode
LEFT JOIN
(SELECT
SubJob,
BillableTotal,
BillableToday,
InvoicedYesterday = Invoiced
FROM
Intranet.dbo.DailyBilling
WHERE
Date = CONVERT(VARCHAR(10), dateadd(day,-2, getdate()), 111)) Yesterday ON vwJobValueVsInvoiced.SubJob = Yesterday.SubJob
WHERE
vwJobValueVsInvoiced.Status <> 'Complete' AND IIF(TotalTTC <> 0,((CompletedBillableHours/TotalTTC)*ContractValue)-vwJobValueVsInvoiced.Invoiced, 0) <> 0";
The tables look like this:
dbo.dailybilling
vwJobValueVSInvoice
vwCurrencyRate
SQL returns this error:
Array ( [0] => Array ( [0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'WIP'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'WIP'. ) )
In working through it the first question I have is What is Yesterday.InvoicedYesterday? It isn’t a table I can find and is not in the outer PHP.
Second questions, why the problem with WIP? It appears in order.
Assistance much appreciated.
Yesterday is an alias given to a subquery, and InvoicedYesterday is an alias given to a column in that subquery.
LEFT JOIN (
SELECT
SubJob
, BillableTotal
, BillableToday
, InvoicedYesterday = Invoiced
FROM Intranet.dbo.DailyBilling
WHERE Date = CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 111)
) Yesterday ON vwJobValueVsInvoiced.SubJob = Yesterday.SubJob
All results of that subquery in the larger query will now be referenced through that alias, so all the columns of the subquery become
Yesterday.SubJob
Yesterday.BillableTotal
Yesterday.BillableToday
Yesterday.InvoicedYesterday
If you trace Yesterday.InvoicedYesterday backwards through that query it is sourced from the table Intranet.dbo.DailyBilling and from the column [Invoiced] in that table, but the subquery is filteing data using WHERE Date = CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 111)
If you run this
select CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 111)
you will see that this is "2 days ago" or "the day before yesterday" e.g. if today is 2018-10-27 the query returns '2018/10/25' (as a string, literally in that format)
I generally would not recommend use of date style 111 in your query (it is used twice). Instead I would recommend using style number 112 instead (this has no delimiter and is the safest format to use).
Like was mentioned in the previous answer, Yesterday is an alias for the subquery and invoicedYesterday is one of its columns alias.
I think the problem with the WIP column is that it belongs to the DailyBilling table but the column is not present in the select clause from the left join named with the Yesterday alias.
I create reports in SQL Server Report Builder. I hope anyone can help me in this case. My query shows me an error if I add a third inner join
How can I add a third select statement?
You need to update the final GROUP BY clause. Including the full name in the SELECT clause requires the full name in the GROUP BY.
As a rule of thumb: When using GROUP BY every field used in the SELECT clause, that isn't combined with an aggregate function, must appear.
Select
SUM(cast(Units as int)) as CounterName,
left(Name,4) as Machine,
Name as WasteLabel
from
Trouble
inner join tsystem ON Trouble.systemid = tsystem.id
where
Name not in ('Aus', 'An', 'Produced')
and IntervalDateWeek >= dateadd(wk, datediff(wk, 0, getdate()) - 2, 0)
and IntervalDateWeek <= dateadd(wk, datediff(wk, 0, getdate())-1, 0)
and left(Name, 4) in (Select name from tSystem
where
ID in
(
Select
systemid
from
tsystemviewSystemwhere
WHERE
SystemViewID = 4)
)
group by
Name
I suspect your query could be improved. Adding clear sample data and expected output will help here.
I created a column in the select statement (DaysSinceCheck).
How do I use it in my WHERE clause?
SELECT DISTINCT
Name,
(DATEDIFF(D, max(lastDaTe), GETDATE())) AS DaysSinceCheck
FROM
event_table
WHERE
cust = 'usa'
AND ((DATEDIFF(D, max(lastDaTe), GETDATE()))) <= 2
GROUP BY
Name
Receiving the error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
You test the value of an aggregate function using a HAVING clause.
...
WHERE cust = 'usa'
GROUP BY Name
HAVING ((DATEDIFF(D, max(lastDaTe), GETDATE()))) <= 2
The HAVING solution works fine... an alternate way is using a CTE to break things into two parts like this:
WITH NameAndDays AS (
SELECT DISTINCT Name,
(DATEDIFF(D, max(lastDaTe), GETDATE())) AS DaysSinceCHECK
FROM event_table
WHERE cust = 'usa'
GROUP BY Name
) SELECT *
FROM NameAndDays
WHERE DaysSinceCHECK <= 2
I am trying to get the sum of "Qty" in a Table A called "Quote_Items" based on a "Required_by_Date" from Table B called Quotes. Both tables have a common "Quote_No" The required date is one month ago.
I have used the following but it produces a NULL, but I cannot see why
select sum(Qty)
from quotes.Quote_Items_Quantities
left outer join quotes.Quotes on (Quote_Required_by_Date = Qty)
WHERE (DatePart(yy, Quote_Required_by_Date) = DatePart(yy, DateAdd(m,1,getdate()))) and
datepart(m,Quote_Required_by_Date) = datepart(m,dateadd(m,1,getdate()))
Any suggestions what I am doing wrong here.
Try this:
SELECT SUM(i.Qty)
FROM Quote_Items i
JOIN Quotes q on i.Quote_No = q.Quote_No
AND CONVERT(varchar, q.Required_by_Date, 112) = CONVERT(varchar, DATEADD(month, -1, getdate()), 112)
or this (equivalent without using JOIN)
SELECT SUM(i.Qty)
FROM Quote_Items i
WHERE EXISTS(SELECT 1 FROM Quotes WHERE Quote_No = i.Quote_No AND CONVERT(varchar, Required_by_Date, 112) = CONVERT(varchar, DATEADD(month, -1, getdate()), 112))
Your query is producing NULL because of the join condition. You have
on Quote_Required_by_Date = Qty
However, the date is not going to match the quantity. Instead, you need to match on the Quote_No, according to your question:
select sum(Qty)
from quotes.Quote_Items_Quantities qiq left outer join
quotes.Quotes q
on q.Quote_Required_by_Date = qiq.Qty
WHERE (DatePart(yy, Quote_Required_by_Date) = DatePart(yy, DateAdd(m,1,getdate()))) and
datepart(m,Quote_Required_by_Date) = datepart(m,dateadd(m,1,getdate()));
You can also simplify your query by using the month() and year() functions:
select sum(Qty)
from quotes.Quote_Items_Quantities qiq left outer join
quotes.Quotes q
on q.Quote_Required_by_Date = qiq.Qty
WHERE year(Quote_Required_by_Date) = year(DateAdd(m, 1, getdate()) and
month(Quote_Required_by_Date) = month(dateadd(m,1,getdate());
Finally, you mind find it useful to use group by and get the results for many months:
select year(Quote_Required_by_Date), month(Quote_Required_by_Date), sum(Qty)
from quotes.Quote_Items_Quantities qiq left outer join
quotes.Quotes q
on q.Quote_Required_by_Date = qiq.Qty
group by year(Quote_Required_by_Date), month(Quote_Required_by_Date)
order by 1 desc, 2 desc;