accumulating a value in SQL - sql

I am trying to accumulate a value when a certain condition exists such as
If statusCode = 0
then add 1 to a value.
I am trying to show the number of successful records as defined by the statusCode.
There must be a better way to do this.
Thanks

Select count(1) from yourTable where statusCode=0

Related

Scalar subqueries produced more than one element

I know this error has been produced many time and a lot of answers came by but I believe every situation might be unique.
So i am trying to get a deficit value(imports - exports) from a table. Both values are on one column
value account
100 export
200 import
SO now i need to calculate the deficit or surplus, which is either import-export or export-import. I tried scalar subqueries but i am always getting this error.
SELECT label, product_type,status,((select value from Task2.quarterly_report where account="Imports") - (select value from Task2.quarterly_report where account="Exports")) As trade_deficit
so basically i am trying to get a table with:-
label product_type status trade_deficit
Can anyone explain to me the issue and why is it happening and how to solve it.
Thanks in advance
You can use the conditional aggregation:
select sum(case when account = 'import' then value
when account = 'export' then - value
end)
from t;
This is based on the question and sample data. I don't see what your query has to do with the rest of the question.

Oracle SQL Fast Retrieval of a Negative condition

Not sure if I am asking the question correctly
I have a table with 2K records. Each record represents a huge Batch processing Unit and it is marked with an initial status of 0(Zero). Every time the batch is completed, the status is updated for that Batch record=10. This Marks that row/Batch as completed
What is the fastest way to see if All records have the status as 10. The query can return true or false or count as soon as it encounters the first 0(Zero). Only in the worst-case scenario, it has to go thru the entire table and return.
The fastest would probably be:
select (case when exists (select 1 from t where status <> 10)
then 'incomplete' else 'complete'
end)
from dual;
This can use an index on (status), which should make the lookup even faster.
However, I would recommend changing the "batch job" to write a message when it is complete -- such as updating a "completed time" column in a batches table. Why query for completeness when the batch knows if it is done?
I would do something like this:
select null as col
from your_table
where status = 0 AND ROWNUM = 1
;
(or select whatever you want from the first row encountered that has status = 0; the way I wrote the query, it will only tell you if there are incomplete batches, but it won't tell you anything else.)
The ROWNUM condition forces execution to stop as soon as the first status = 0 is encountered - either from the table itself, or from an index if you have an index on the status column (as you might, if this kind of query is common in your business).
The query will return either a single row - with NULL in the only column col, unless you choose to select something from the record - or no rows if all batches are complete.

How to get 0 if no row found from sql query in sql server

I am getting blank value with this query from sql server
SELECT TOP 1 Amount from PaymentDetails WHERE Id = '5678'
it has no row,that is why its returning blank,So I want if no row then it should return 0
I already tried with COALESCE ,but its not working
how to solve this?
You are selecting an arbitrary amount, so one method is aggregation:
SELECT COALESCE(MAX(Amount), 0)
FROM PaymentDetails
WHERE Id = '5678';
Note that if id is a number, then don't use single quotes for the comparison.
To be honest, I would expect SUM() to be more useful than an arbitrary value:
SELECT COALESCE(SUM(Amount), 0)
FROM PaymentDetails
WHERE Id = '5678';
You can wrap the subquery in an ISNULL:
SELECT ISNULL((SELECT TOP 1 Amount from PaymentDetails WHERE Id = '5678' ORDER BY ????),0) AS Amount;
Don't forget to add a column (or columns) to your ORDER BY as otherwise you will get inconsistent results when more than one row has the same value for Id. If Id is unique, however, then remove both the TOP and ORDER BY as they aren't needed.
You should never, however, use TOP without an ORDER BY unless you are "happy" with inconsistent results.

SQL Multiple Conditions in max statement not working

I am attempting to filter my table and get the item that sold for the most amount of money. In order to do this I am using "AuctionOpen" to determine whether or not the auction is still open. The auction cannot be open and have the item been sold (later I will use this for the most expensive item available).
I am able to use the AND operator to compare AuctionOpen by using the following:
select s.*
from auctionsite.dbo.Auction s
where s.HighestBid = (select max(s2.HighestBid) from auctionsite.dbo.Auction
s2) and s.AuctionOpen = 0;
When I set this equal to zero I get results, but when I set it equal to 1, it only returns the column titles even though there are values set to 1 in the table.
Results when compared to 0:
Results when compared to 1:
Clearly, the highest bid is on a record where AuctionOpen <> 1.
I recommend using order by and fetch (or the equivalent in your database):
select s.*
from auctionsite.dbo.Auction s
where s.AuctionOpen = 0
order by s.HIghestBid desc
fetch first 1 row only
In SQL Server, use either select top (1) or offset 0 rows fetch first 1 row only.
I think you should try the Count aggregate function
here, try this:
**Select count(Item_name) As
[Item with the highest money]
from table_name
Group by Item_name DSEC;**
You can check my page hereSQL/MySQL tips for some SQL/MySQL lessons

Countif query in access

I am trying to run a query that calculate with a countif function but I am having trouble with it. I have used the count and the iif functions in the builder but I think something weird is going on. I am trying to count the number of times a certain value occurs in a column so I do not want a specific value to equal to if that's possible?
Thanks!
To count the number of times a value appears you can use something like.
If you want to know how many times each value appears just omit the WHERE clause (without a sample of data I've used a table in the database I'm working on).
SELECT ProcessID,
COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
if you need just the value you can use:
SELECT COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
Another way is:
SELECT DCOUNT("ProcessID","tbl_PrimaryData_Step1","ProcessID = 4")
Edit:
In reply to your comment on your original post this SQL will give the result you're after:
SELECT Concatenate,
COUNT(Concatenate)
FROM MyTable
GROUP BY Concatenate