error while executing query in access - sql

Getting error while executing following query in access 2007, how to solve this?
my query is :
SELECT
A.PRODUCT
, A.DISPBANK
, COUNT(*) AS RecCount
, SUM(Amt) AS TotAmt
FROM
CBWCFAPENDINGPAYMENTDATA A
WHERE
A.MATCH ='Y'
AND A.ID LIKE'*(SELECT [CASHIN_ID] FROM CBWCFAMISUPLOAD WHERE VENDOR='BRINKS' AND NZ(MATCH,'')='Y')*'
GROUP BY
A.PRODUCT
, A.DISPBANK

You can't have a subquery inside the LIKE string.
You could use DLookup and string concatenation for this.
AND A.ID LIKE '*' &
DLookup("[CASHIN_ID]", "CBWCFAMISUPLOAD", "VENDOR='BRINKS' AND NZ(MATCH,'')='Y'" ) & '*'

Ok so your answer should be like the below, and will calculate what you need if the tables have a one to many relationship, and returns all rows from table a where the id is found in table b:
SELECT A.PRODUCT, A.DISPBANK,Count(*) as RecCount,Sum(Amt) as TotAmt FROM CBWCFAPENDINGPAYMENTDATA A WHERE A.MATCH="Y" AND Format(CStr([A.ID])) IN (SELECT [CASHIN_ID] FROM CBWCFAMISUPLOAD WHERE VENDOR="BRINKS" AND NZ(MATCH,"")="Y")) GROUP BY A.PRODUCT, A.DISPBANK

Related

Avoiding aggregation when selecting values from tables

I have the following code which selects value from table2 when 'some string' occurs more than once in 1990
SELECT a.value, COUNT(*) AS test
FROM table1 c
JOIN table2 a
ON c.value2 = a.value_2
JOIN table3 o
ON c.value3 = o.value_3
AND o.value4 = 1990
WHERE c.string = 'Some string'
GROUP BY a.value
HAVING COUNT(*) > 1
This works fine but I am attempting to write a query that produces a similar result without using aggregation. I just need to select values with more then 1 c.string and select those rather than counting and selecting the count as well. I thought about searching for pairs of 'some string' occurring in 1990 for a value but am unsure of how to execute this. Pointing me in the right direction would be appreciated! Struggling to find any documentation referencing this. Thank you!
Use window function ROW_NUMBER() to assign a sequence number within the rows of each table2.value. And use window function FIRST_VALUE() to get the largest row number for each table2.value. Use DISTINCT to remove the duplicates:
select distinct value, first_value(rn) over ( order by rn desc) as count
from
(
SELECT a.value , row_number() over (partition by a.value order by null) rn
FROM table1 c
JOIN table2 a
ON c.value2 = a.value_2
JOIN table3 o
ON c.value3 = o.value_3
AND o.value4 = 1990
WHERE c.string = 'Some string' ) t
where rn > 1;
To check for duplicates, you can use 'WHERE EXISTS', as a starting point. You could start by reading this:
https://www.w3schools.com/sql/sql_exists.asp
This will give you quite a long, cumbersome piece of code compared to using aggregation. But I expect that's the point of the task - to show how useful aggregation is.

Query division returns 0

I am working on a query that devide 2 columns, I tried CAST and CONVERT but still returns 0. Will apperciate your help
SELECT a.Disposition,a.[Disposition Reason Breakdown],a.CSP,b.Total FROM
(
SELECT a.[Disposition],a.[Disposition Reason Breakdown],a.[CSP] FROM
(
SELECT [Disposition],[Disposition Reason Breakdown],COUNT(*) as CSP FROM [dbo].[Disposition]
WHERE [Disposition] <> 'Interested'
GROUP BY [Disposition],[Disposition Reason Breakdown]
) a
)a
INNER JOIN
(
SELECT a.Disposition,SUM(a.CSP) as Total FROM
(
SELECT [Disposition],[Disposition Reason Breakdown],COUNT(*) as CSP FROM [dbo].[Disposition]
WHERE [Disposition] <> 'Interested'
GROUP BY [Disposition],[Disposition Reason Breakdown]
)a
GROUP BY a.Disposition
)b ON a.Disposition = b.Disposition
I am using sql
I solved it, it turns out that I just used the wrong data type which in my case is decimal I should've thought of REAL here is the final query a.CSP/CAST(b.Total as REAL)

SELECT with not equal condition

How to i display the invoicenumber only if the extended price is not equal to subtotal?
Following query is fail to run and error group function is not allowed here
SELECT a.INVOICENUMBER,SUM(a.EXTENDEDPRICE),SUM(a.SUBTOTAL)
FROM CUSTOMERORDERHISTORYVIEW a
WHERE EXISTS
(SELECT a1.INVOICENUMBER FROM CUSTOMERORDERHISTORYVIEW a1 WHERE
a.INVOICENUMBER=a1.INVOICENUMBER AND SUM(a1.EXTENDEDPRICE) <> SUM(a1.SUBTOTAL))
GROUP BY a.INVOICENUMBER;
You are looking for the having clause:
SELECT a.INVOICENUMBER, SUM(a.EXTENDEDPRICE), SUM(a.SUBTOTAL)
FROM CUSTOMERORDERHISTORYVIEW a
GROUP BY a.INVOICENUMBER;
HAVING SUM(a.EXTENDEDPRICE) <> SUM(a.SUBTOTAL);

How to Get a Query Result even if Result is 0 or Null

I am hoping to get an answer to this problem.
I am using SQL Developer to write queries, connected to an Oracle database. What I need is, if the query result is nothing (null or 0, I guess?), I still need something to show up. As of now, when the query result is nothing, then nothing but column headers come up.
The code below is what I have/tried so far with no success.
SELECT
to_char(rs.cr_date, 'MM/DD/YYYY') "Date",
COUNT(os.ord_id) "RTS Returned Orders"
FROM return_sku rs,
order_sku os
WHERE
rs.s_method Like '%RTS%'
AND trunc(created_date) = trunc(SYSDATE)
AND os.ord_sku_id = rs.ord_sku_id
GROUP BY to_char(rs.cr_date, 'MM/DD/YYYY')) rts
This works fine when there is an "RTS" in the s_method column; as in, a number will appear in my query result. The problem is that when there are no query results where rs.s_method has "RTS" in it, my query just returns column headers and nothing else (see below).
Date | RTS Returned Orders
------------------------------
I need it so that when there are no results with "RTS" in s_method, it will return a row with the date and the number 0 in the "RTS Returned Orders" column. Something like below:
Date | RTS Returned Orders
------------------------------
12/4/2013 | 0
I have tried using decode and NVL to no avail. Either I am not using them correctly, or there is something else that I can use that I am unaware of.
Please help! Thanks in advance. Any help is greatly appreciated.
Best Regards,
-Anthony C.
I think the query that you want uses conditional aggregation, instead of filtering in the where clause:
SELECT to_char(rs.cr_date, 'MM/DD/YYYY') as "Date",
sum(case when rs.s_method Like '%RTS%' then 1 else 0 end) as "RTS Returned Orders"
FROM return_sku rs join
order_sku os
on os.ord_sku_id = rs.ord_sku_id
WHERE trunc(created_date) = trunc(SYSDATE)
GROUP BY to_char(rs.cr_date, 'MM/DD/YYYY');
Note a few things. The group by is unnecessary, because you are only returning one row (but I'm leaving it in because it was part of your original question). I also fixed the join syntax to use standard join syntax (join . . . on) rather than implicit joins.
You could do something like this:
select "date", sum('MM/DD/YYYY') "Date")
from
(select to_char(sysdate, 'MM/DD/YYYY') "Date"
, 0 "'MM/DD/YYYY') "Date"
from dual
union
SELECT
to_char(rs.cr_date, 'MM/DD/YYYY') "Date",
COUNT(os.ord_id) "RTS Returned Orders"
FROM return_sku rs,
order_sku os
WHERE
rs.s_method Like '%RTS%'
AND trunc(created_date) = trunc(SYSDATE)
AND os.ord_sku_id = rs.ord_sku_id
GROUP BY to_char(rs.cr_date, 'MM/DD/YYYY')) rts
) temp
Not sure about the syntax with all those quoted aliases, but the general idea should work.
You can use a subquery.
This will be pseudo code.
Select check if columnA is null then show the alternate value
From
(Select columnA from your current query) as MyQuery
WITH os_counts AS (
SELECT COUNT(rs.ord_id) AS "RTS Returned Orders",
os.ord_id
FROM order_sku os
LEFT OUTER JOIN
( SELECT ord_id
FROM return_sku
WHERE s_method LIKE '%RTS%'
) rs
ON ( os.ord_sku_id = rs.ord_sku_id )
AND TRUNC(created_date) = TRUNC(SYSDATE)
GROUP BY
os.ord_id
)
SELECT TO_CHAR( NVL( rs.cr_date, SYSDATE ), 'MM/DD/YYYY') "Date",
"RTS Returned Orders"
FROM os_counts os
LEFT OUTER JOIN
return_sku rs
ON ( os.ord_sku_id = rs.ord_sku_id );
I have no idea which table your created_date column is in so you might need to move that statement about.
The lower LEFT OUTER JOIN might be able to be an INNER JOIN but I have no idea on your table structures and dependencies.

Why I need Group by in this simple query?

UPDATE :
-----
the error might be in sum(si.amt_pd) from item table (as there is no relation) :
select SUM(si.amt_pd)amt_pd from [HMIS_REPORTING].HMIS_RPT_ME.dbo.item i
where
is there a work around?
----------
I am trying to run this query. The query just fetches the amount of a month based on some tables. It is just a part of a big query.
select s.sales_Contract_Nbr
, s.Sales_Id
, s.Sale_Dt
, YEAR(s.Sale_Dt) 'YEAR'
, MONTH(s.Sale_Dt) 'MONTH'
, s.Sales_Need_TYpe_Cd
, s.Sales_Status_Cd
, si.Posted
, s.location_Cd
, jan2011 = (
select SUM(si.amt_pd)amt_pd
from [HMIS_REPORTING].HMIS_RPT_ME.dbo.item i
where i.Item_Id = si.Product_Item_ID
and i.Item_Cd <> '*INT'
and convert(varchar(10),SI.Sales_Item_Dt,126) >= '2011-01-01'
and convert(varchar(10),SI.Sales_Item_Dt,126) >= '2011-01-31'
) INTO dbo.#a_acomparision
FROM [HMIS_REPORTING].HMIS_RPT_ME.dbo.Sales S
, [HMIS_REPORTING].HMIS_RPT_ME.dbo.Sales_Item SI
WHERE SI.Sales_Id = S.Sales_Id
and s.Sales_Contract_Nbr in (
select distinct (Sales_Contract_Nbr)
from mountainviewContracts
where Sales_Contract_Nbr <> '')
but I am getting the following error message.
Msg 8120, Level 16, State 1, Line 1
Column 'HMIS_REPORTING.HMIS_RPT_ME.dbo.Sales.Sales_Contract_Nbr' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I just can't understand why my query should have a group by for sales_contract_nbr and even if I put in the group by clause it tells me that inner query si.Product_item_id and SI.sales_item_dt should also be contained in group by clause.
Please help me out.
Thanks in advance
This is a very subtle problem. However, I think the subquery should be:
select SUM(i.amt_pd)amt_pd from [HMIS_REPORTING].HMIS_RPT_ME.dbo.item i
That is, the alias should be i not si.
What is happening is that the sum in the subquery is on a value in the outer query. So, the SQL compiler assumes an aggregation query. As soon as the first column is found that is not an aggregation, it complains with the message that you have.
By the way, you should use proper join syntax, so you from clause looks like:
FROM [HMIS_REPORTING].HMIS_RPT_ME.dbo.Sales S join
[HMIS_REPORTING].HMIS_RPT_ME.dbo.Sales_Item SI
on SI.Sales_Id = S.Sales_Id
As #Gordon Linoff says, this is almost certainly because the query optimizer is treating this like a SUM operation, normalizing away the subquery for "jan2001".
If the amt_pd column is present in the ITEM table, Gordon's solution is the right one.
If not, you have to add the group by statement, as below.
select s.sales_Contract_Nbr
, s.Sales_Id
, s.Sale_Dt
, YEAR(s.Sale_Dt) 'YEAR'
, MONTH(s.Sale_Dt) 'MONTH'
, s.Sales_Need_TYpe_Cd
, s.Sales_Status_Cd
, si.Posted
, s.location_Cd
, jan2011 = (
select SUM(si.amt_pd)amt_pd
from [HMIS_REPORTING].HMIS_RPT_ME.dbo.item i
where i.Item_Id = si.Product_Item_ID
and i.Item_Cd <> '*INT'
and convert(varchar(10),SI.Sales_Item_Dt,126) >= '2011-01-01'
and convert(varchar(10),SI.Sales_Item_Dt,126) >= '2011-01-31'
) INTO dbo.#a_acomparision
FROM [HMIS_REPORTING].HMIS_RPT_ME.dbo.Sales S
, [HMIS_REPORTING].HMIS_RPT_ME.dbo.Sales_Item SI
WHERE SI.Sales_Id = S.Sales_Id
and s.Sales_Contract_Nbr in (
select distinct (Sales_Contract_Nbr)
from mountainviewContracts
where Sales_Contract_Nbr <> '')
GROUP BY s.sales_Contract_Nbr
, s.Sales_Id
, s.Sale_Dt
, YEAR
, MONTH
, s.Sales_Need_TYpe_Cd
, s.Sales_Status_Cd
, si.Posted
, s.location_Cd