How to fetch data from same column using different criteria? - sql

Hi I have table like this in SQL anywhere database
CUSTID-------DDAT.----------AMOUNT
1. 01-01-2021. 1000
1. 02-02-2021. 2000
1. 03-02-2021. 3000
1. 04-02-2021. 4000
2. 01-04-2021. 1000
2. 02-04-2021. 2000
04-04-2021. 1000
I want data like this in VB.net where amount is only for one date and total amount is for 4 day
Cust id.------date ---------------Amount.-------Total amount
1. 04-04-2021. 4000. 10000
2. 04-04-2021. 1000. 4000
Can you give me any solution..thanks in advance

My take on it:
select custid, dat, amount, total_amount
from (
select custid, dat, amount, sum(amount) over (partition by custid) as total_amount
from data
) d
where dat = '2021-04-04' -- or any other date
Might be that the inner select is all that you need. Not sure if the filter on date is necessary.

Related

How to select, ignore the data when some values contain same value in the latest input(make it like no change commited in the data)?

Please help with SQL Server query. I need to ignore data when some values contain same value or no change in the values from dates a to dates b, and only show data that have different values. For example if I have the following table:
So I have data that has dates between 20230101 - 20230110
id
date
before
after
pers
20230101
400
600
ashe
20230104
600
800
pers
20230105
600
400
wowa
20230105
800
900
incl
20230107
850
900
incl
20230108
900
850
chro
20230109
900
950
The output should be as follow:
id
date
before
after
ashe
20230104
600
800
wowa
20230105
800
500
chro
20230109
900
950
Its remove pers and incl. because there is no change in their values.
Please help, Thanks in advance.
Already doing some query from this forum with min max but there is no result cause I'm confused with that before after values
Find the sum of 'before' and 'after' for each id, then select rows where the difference between two sums is not equal to 0.
WITH CTE AS
(
SELECT *,
SUM(before) OVER (PARTITION BY id) before_sum,
SUM(after) OVER (PARTITION BY id) after_sum
FROM yourTbl
)
SELECT id, date_, before, after
FROM CTE
WHERE before_sum - after_sum <> 0
ORDER BY ID
Demo
This can be simplified to SUM(before - after) OVER (PARTITION BY id) flag ... where flag <> 0.
select id, sum(before) as b, sum(after) as a
from t1
group by id
having (b - a) <> 0;

Summing numbers don't match up?

I have a table with data like this (acc_v in query):
value
id
100
1
300
2
200
1
As you can see there are multiple values per id. I want to sum all of the values across the ids and end up with this:
value
id
300
1
300
2
;with accV as (SELECT
d_id
,[period_end_date]
,max(a_value) as value
,id
FROM bh
where period_end_date = '2021-6-30'
group by d_id, period_end_date, id)
SELECT bh.id, sum(value)
FROM bh join accV on accV.id = bh.id
group by bh.id
For some reason the total values are adding up to significantly larger amounts than they should be. I verified this by taking the original values and summing them in excel. If anyone knows what I am doing wrong the help is much appreciated.
You can use window functions:
select id, sum(sum(value)) over () as total_sum
from t
group by id;

PostgreSQL, SQL: how to use rows values as parameters in a ready-made function?

Is it possible to use rows in a table as parameters in a function?
For example, I have a simple function that calculates total sales by amount and quantity.
SELECT sales from public.my_function(v_amount, v_qty)
I have a table with two columns: sum and quantity
Amount QTY
100 5
200 10
300 20
400 30
500 40
I want to add another column where there will be a function result for parameters that are in each row.
I try to place columns within a function but it returns me an error.
It seems to me that my try is really wrong:
SELECT Amount,
QTY
(select sales from public.my_function(select Amount from public.table, select QTY from public.table) as Sales
FROM public.table
Result need as :
Amount QTY Sales
100 5 500
200 10 2000
300 20 6000
400 30 12000
500 40 20000
Is there a way to do it right?
Thanks
You want to use a lateral join if the function returns a table:
SELECT Amount, QTY, x.Sales
FROM public.table t cross join lateral
public.my_function(t.amount, t.qty) x;
If it just returns a scalar, then you don't have to do anything special:
SELECT t.Amount, t.QTY,
public.my_function(t.amount, t.qty) as Sales
FROM public.table t;

hoe to make sum of one sql table column with ignoring duplicate values?

quoteId price
1 50
1 50
2 10
3 40
3 40
3 40
4 10
In this table I always get the same price for each quoteId.
Example: quoteId = 1 has multiple entries with the price of 50.
By using SUM I get the total sum of the price column which is:50 + 50 + 10 + 40 + 40 + 40 + 10 = 240
However, I only want to sum the unique price for quoteId which is:
50+10+40+10 = 110
How can I approch this?
Another option is DISTINCT
Example
Select MyTotal = sum(price)
from (Select Distinct quoteId,price From YourTable) A
Returns
MyTotal
110
Following query will work:
select sum(price)
from yourTablename
group by quoteId,price;
You need a nested query to compute an intermediate value by quoteId using avg ( or max or min with your data)
But you need know why you have duplicate value by quotedId, may be you have a mistake before.
select sum(price) from (
select
quoteId,
avg(price) price,
from
your_table
group by
quoteId
) as x
This query is compliant with ISO standard SQL and will works with several database engine

display the row with the first and last odd number

good day everyone . I've been searching for a long time for the activity that our professor wants us to do .
for example my column name is tax, tax has these values:
TAX
-----
3681
4292
4895
1894
1127
the program should show numbers 3681 and 1127 . any response is much appreciated . btw our school is using MS SQL Server 2000 .
From my understanding of your question, you are looking for the rows with the lowest or highest odd number as the last character? If so, then this should work:
select t.tax
from yourtable t
join (
select min(right(tax,1)) minoddchar, max(right(tax,1)) maxoddchar
from yourtable
where tax % 2 = 1 --ensure odd result
) t2 on right(t.tax,1) in (t2.minoddchar, t2.maxoddchar)
SQL Fiddle Demo
SELECT MIN(tax), Max(tax)
FROM Table
WHERE tax %2 <> 0
Demo: SQL Fiddle
If you actually want first and last from your unordered list, it's a trickier proposition and results could be inconsistent, since without a value to ORDER BY you can't guarantee order. Would usually use ROW_NUMBER(), but not available in 2000, if that's what you're after there are options, the easiest would be to do:
SELECT Tax, IDENTITY(1,1) as TaxID
INTO Table2
FROM Table1
SELECT (SELECT TOP 1 Tax FROM Table2 WHERE Tax %2 <> 0 ORDER BY TaxID)
,(SELECT TOP 1 Tax FROM Table2 WHERE Tax %2 <> 0 ORDER BY TaxID DESC)
You can use this:
SELECT tax FROM Table ORDER BY tax.id ASC LIMIT 1
UNION
SELECT tax FROM Table ORDER BY tax.id DESC LIMIT 1
or this:
SELECT MIN(tax.id), tax FROM Table
UNION
SELECT MAX(tax.id), tax FROM Table
select tax from Table fetch first 1 row only;
select tax from table fetch last 1 row only;