I have read answers here but I am still not quite sure how I would do this regarding two columns of a table and more than one result per query.
So, the first query would look like this in my Node app:
select stype, lid from Profiles where lid_P=${profile.lid} and stype_P='${profile.stype}';
// result can consist of 1-50 objects
I need the result for the following query:
select * from an where stype=stype and lid=lid;
// where lid and stype are results from the other query
I only need the results of the second query but I fail to implement only one query doing both. Can someone help me out? Any help is appreciated.
Thank you!
You seem to want exists:
select *
from an a
where exists(
select 1
from profiles p
where
p.stype = a.stype and p.lid = s.lid
and p.lid_p = #profile_lid and p.style_p = #profile_stype
)
#profile_lid and #profile_stype are the parameters to the query - that I would recommend using instead of concatenating variables in the query string.
Did you try
select *
from an
where (stype, lid) in (select distinct ... <your first query>)
Related
I have table which is named os_txn.pay and I have columns merchant_no and insert_date_time.
What I am looking for is that I want to know numbers of rows, I will give merchant_no as a parameter and will give value in java code, and I also want to look at insert_date_time.
My purpose is that how many rows in the insert_date_time with a specified merchant_no?
I tried some query below but it didnt give results that I wished. I think it should be inclued count(*) and group by but looks like I couldnt succeed in the syntax.
I am open your suggestions from now, thank you.
SELECT COUNT(*)
FROM os_txn.pay
WHERE merchant_no = :merchantNoValue --it takes merchantNoValue FROM service request in java
AND insert_date_time = :insert_date_time
GROUP BY insert_date_time, merchant_no
I want a result like a single cell data for example Count = 9.
For example our insert_date_time is 20221009 and in this insert_date_time my specified merchant_no has 9 rows of data. I want to know this data, I hope I could express myself clearly, thank you from now
I think I found my solution :
SELECT COUNT(*)
FROM (SELECT COUNT(*)
FROM os_txn.pay
WHERE merchant_no = :merchantNoValue
GROUP BY insert_date_time, merchant_no)
Your WHERE condition specifies the values of both merchant_no and insert_date_time:
WHERE merchant_no = :merchantNoValue AND insert_date_time = :insert_date_time
That means that GROUP BY statement is not needed here. After the WHERE condition is applied, there will only be one group. If I am understanding correctly, this is what you need:
SELECT COUNT(*)
FROM os_txn.pay
WHERE merchant_no = :merchantNoValue --it takes merchantNoValue FROM service request in java
AND insert_date_time = :insert_date_time
I am trying to split out few values from a database.
There are two columns, Test and Test_Parameter. I want to extract those rows which have different "test_parameter" values for same "Test" value. For example, in my screenshot, I want "Grade:" to be selected as it has different values of Test_Parameter. I know it has a very simple solution but I am not able to figure it out.
You can use aggregation and having:
select test
from t
group by test
having min(test_parameter) <> max(test_parameter);
I might be confused by the ask, but wouldn't this work?
SELECT *
FROM t
WHERE test <> test_parameter
Try any of below to remove colon and compare :
SELECT DISTINCT test FROM table1 WHERE test NOT LIKE test_parameter||'%'
SELECT DISTINCT test FROM table1 WHERE SUBSTR(test,1,INSTR(test,':')-1) <> test_parameter
I like Gordon's answer better, but a maybe simple you understand albeit I suspect slower method would be:
SELECT test
FROM (
SELECT test,
COUNT(DISTINCT(test_parameter) AS c
FROM table1
GROUP BY 1
)
WHERE c > 1
the above is if you really don't want the counts in the results, otherwise the having clause makes its simpler:
SELECT test,
COUNT(DISTINCT(test_parameter) AS c
FROM table1
GROUP BY 1
HAVING c > 1
I am new to SQL and have had pretty good luck figuring things out thus far but I am missing something in this query:
The question is how to return a distinct count from two columns using another column and the criteria if the value is greater than 0.
I have tried IF and AND operators (My current query returns a 0 not an error, and it works when only using one .shp criteria)
select count (distinct ti.TO_ADDRESS)
from ti
where ti.input_id = 'xxx_029_01z_c_zzzzbab_ecrm.shp'
and ti.input_id = 'xxx_030_01z_c_zzzzbab_ecrm.shp'
and ti.OPENED>0;
Thanks so much!!
I think you want two levels of aggregation:
select count(*)
from (select ti.TO_ADDRESS
from ti
where ti.input_id in ('xxx_029_01z_c_zzzzbab_ecrm.shp', 'xxx_030_01z_c_zzzzbab_ecrm.shp') and
ti.OPENED > 0
group by ti.TO_ADDRESS
having count(distinct ti.input_id) = 2 -- has both of them
) ti;
I have a query working the way I want, by executing SQL directly, but am curious (just for my own learning purposes) if this same thing could be done in an ActiveRecord statement?
The part I'm struggling with the most is the COALESCE part of this query, which just makes sure that any NULL values from the LEFT JOIN are counted as zeros instead, to keep the summation in order.
Any ideas? I'm using Postgres.
SELECT Inventories.id, Inventories.name, Inventories.unit_of_measure,
COALESCE(Sum(Stocks.count),0) as totalcount
FROM Inventories
LEFT JOIN Stocks
ON Inventories.id = Stocks.inventory_id
WHERE Inventories.property = 'material' AND Inventories.organization_id = #{current_organization.id}
GROUP BY Inventories.id, Stocks.inventory_id
ORDER BY totalcount ASC
LIMIT(5)")
This is the closest I've gotten for an AR equivalent. When I try to add a sum or something like it, that's when it errors out.
#lowmaterials = current_organization.inventories.materials.left_joins(:stocks).group(:id, :inventory_id).order(count: :asc).limit(5)
You can use ActiveRecord::QueryMethods#select:
your_relation.select("column1, column2, COALESCE(1,2) AS column3").left_joins...
iam not a big ORACLE - SQL Expert, so i hope someone knows a good way to find the "duplicate" record wich is causing the: single-row subquery returns more than one row error.
This my Statement:
SELECT
CAST(af.SAP_SID AS VARCHAR2(4000)) APP_ID,
(SELECT DR_OPTION
FROM
DR_OPTIONS
WHERE DR_OPTIONS.ID = (
select dr_option from applications where applications.sap_sid = af.sap_sid)) DR_OPTION
FROM
APPLICATIONS_FILER_VIEW af
it works on my test system, so iam "sure" there must be an error inside the available data records, but i have no idea how to find those ..
Try with this query:
select applications.sap_sid, count(dr_option)
from applications
group by applications.sap_sid
having count(dr_option) > 1
This should give you the sap_sid of the duplicated rows
I'd suggest simplifying your query:
SELECT CAST(af.SAP_SID AS VARCHAR2(4000)) APP_ID,
dr.DR_OPTION
FROM APPLICATIONS_FILER_VIEW af
INNER JOIN applications a ON af.sap_sid = a.sap_sid
INNER JOIN DR_OPTIONS dr ON a.dr_option = dr.ID
I would investigate what you get when you run:
select dr_option from applications where applications.sap_sid = af.sap_sid
but you could force only one row to be returned (I see this as being a fudge and would not recommend using it at least add an order by to have some control over the row being returned) with something like:
SELECT
CAST(af.SAP_SID AS VARCHAR2(4000)) APP_ID,
(SELECT DR_OPTION
FROM
DR_OPTIONS
WHERE DR_OPTIONS.ID = (
select dr_option
from applications
where applications.sap_sid = af.sap_sid
and rownumber = 1)
) DR_OPTION
FROM
APPLICATIONS_FILER_VIEW af
(not tested just googled how to limit results in oracle)
If you fix the data issue (as per A.B.Cades comment) then I would recommend converting it to use joins as per weenoid's answer. this would also highlight other data issues that may arise in the future.
IN SHORT: I have never fixed anything in this way.. the real answer is to investigate the multiple rows returned and decide what you want to do maybe:
add more where clauses
order the results and only select top row
actually keep the duplicates as they represent a scenario you have not thought of before