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.
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'm trying to ultimately get a sum of two columns. The issue is that the sum is made up from two different tables. Basically it's tbl1-col1 + tabl2-col1.
I'm attempting a UNION ALL, but the issue is that for me to pull the right data from both queries to even get the right data in the col1's, I need to SELECT different parameters for both queries.
Obviously I'm working with different datatypes and header titles, so that UNION isn't going to work the way I want it to.
-------------------------------query 1------------------------------------------------
SELECT
RECEIVABLE_LINE.AMOUNT AS Total,
RECEIVABLE_LINE.INVOICE_ID,
RECEIVABLE_LINE.LINE_NO,
RECEIVABLE_LINE.GL_ACCOUNT_ID,
RECEIVABLE.INVOICE_DATE,
RECEIVABLE.TYPE
FROM RECEIVABLE_LINE INNER JOIN RECEIVABLE ON RECEIVABLE_LINE.[INVOICE_ID] = RECEIVABLE.[INVOICE_ID]
WHERE RECEIVABLE_LINE.GL_ACCOUNT_ID LIKE '4%'
AND RECEIVABLE.INVOICE_DATE >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
AND RECEIVABLE.INVOICE_DATE < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())+1, 0)
-------------------------------query 2------------------------------------------------
SELECT
SUM(SHIPPER_LINE.USER_SHIPPED_QTY*CUST_ORDER_LINE.UNIT_PRICE) AS 'Total',
SHIPPER_LINE.USER_SHIPPED_QTY,
CUST_ORDER_LINE.UNIT_PRICE,
SHIPPER.INVOICED_DATE,
SHIPPER.SHIPPED_DATE
--SUM(isnull(CAST('Total' AS decimal),0)) as 'Total of all totals'
FROM SHIPPER_LINE INNER JOIN SHIPPER ON SHIPPER_LINE.PACKLIST_ID = SHIPPER.PACKLIST_ID
LEFT JOIN CUST_ORDER_LINE ON SHIPPER_LINE.CUST_ORDER_LINE_NO = CUST_ORDER_LINE.LINE_NO AND SHIPPER_LINE.CUST_ORDER_ID = CUST_ORDER_LINE.CUST_ORDER_ID
WHERE SHIPPER.INVOICED_DATE Is Null
AND SHIPPER.SHIPPED_DATE BETWEEN '2020-09-01' AND '2020-09-10'
GROUP BY SHIPPER_LINE.USER_SHIPPED_QTY, CUST_ORDER_LINE.UNIT_PRICE, SHIPPER.INVOICED_DATE, SHIPPER.SHIPPED_DATE
ORDER BY SHIPPER.INVOICED_DATE
What are my options at this point?
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;
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 have a query that I am using for a report. Each line is a row of data containing a medical history, the client_id is repeated for each medical history.
I'm grouping by client_id and summing their conditions but I want to use a sub-query to find any conditions that are above a preset score. My current query is giving me the total for the whole table, not just the specific client_id.
Can someone help me out? Thanks!
Here my query:
select
DATEADD(month, DATEDIFF(month, 0, taken_on), 0),
client_id,
count(hscore),
sum(hscore),
(select count(hscore)
from amexmedscreen
where hscore >= '3.5')
from amexmedscreen
group by taken_on, client_id`
It should be sufficient to use CASE to get 1 for relevant rows, and sum those:
select
...
sum(hscore),
sum(CASE WHEN hscore >= 3.5 THEN 1 ELSE 0 END)
group by taken_on, client_id`
Your subquery is a separate query. It's not constrained by what's going on in the main query. You need to tell it to do the count only for the client_id of the current record in the outer query. You'll be referring to the same table twice in two different queries, so you'll have to use a different alias for each one.
Something like this should work:
select DATEADD(month, DATEDIFF(month, 0, taken_on), 0),
client_id,
count(hscore),
sum(hscore),
(select count(hscore)
from amexmedscreen subq
where
hscore >= '3.5'
and subq.client_id = outerq.client_id )
from amexmedscreen outerq
group by taken_on, client_id
select DATEADD(month, DATEDIFF(month, 0, taken_on), 0),
client_id,
count(hscore),
sum(hscore),
tb1.COUNT_HSCORE
from amexmedscreen
INNER JOIN
(select client_id, count(hscore) as COUNT_HSCORE
from amexmedscreen
where hscore >= '3.5'
group by client_ID) as tb1
ON tb1.client_id = client id
group by taken_on, client_id,tb1.COUNT_HSCORE