I have a table like as shown above. As you can see that value in the "v_ctra_no" column repeats. What type of sql query should I use so that I am able to select those rows whose "v_ctra_no" value is unique. Like for example
col_name1 col_name2 col_name3
Ctra#001 Cash 200
Ctra#002 Sales 40
Ctra#003 Purchase 1000
I tried to use the query mentioned below but it does not work
SELECT DISTINCT(v_ctra_no),v_ctra_date,v_ctra_id FROM TBL_ACC_CTRA_VCH_MSB
It looks like you're trying to sum up the sales amounts. Seems that this is more what you are looking for:
SELECT v_ctra_no, v_ctra_dr_ledgeacc_name, SUM(v_ctra_dr_ledgeacc_amt) AS v_total_sales
FROM TBL_ACC_CTRA_VCH_MSB
GROUP BY v_ctra_no, v_ctra_dr_ledgeacc_name
This will show the account number and account name, along with the total (SUM) of all of the entries in the account amount column. As long as the account name is the same for all of the account number entries, then you won't have any duplicates.
However, you may want to consider restructuring your database (if you have that option), so that account details are separate to the separate sales amounts.
If you don't want the sales information and just want the account details along with the dates, then you can do a distinct on the name like this:
SELECT DISTINCT v_ctra_no, v_ctra_date
FROM TBL_ACC_CTRA_VCH_MSB
However, you will get duplicates for each time the date is different but the account number stays the same. If you think about it for a bit, it makes sense.
If you just want the distinct account numbers, then just use:
SELECT DISTINCT v_ctra_no
FROM TBL_ACC_CTRA_VCH_MSB
Related
I am trying to display the development of new created ERC-20 smart contracts on Ethereum. For this purpose I am using the Analytics platform Dune which provides a several databases for SQL querying.
My problem is the following one:
I have a table that shows all transactions of a specific contract. Every transaction is displayed by one row of the table and contains following columns "date", "smart_contract_address"(identifier for a unique ERC20 smart contract) and other details of the transaction as "amount"
Simplified example:
smart_contract_address
date
Amount
A
Q1/2022
50
B
Q1/2022
20
C
Q2/2022
10
A
Q1/2022
5
A
Q2/2022
7
I would like to count the different smart_contract_addresses per quarter. I want to make sure that every address is only counted once. After an address was counted it should be "ignored", not only if it appeared in the same quarter, but also in following ones.
For my example my expected query result would look like:
Quarter
Count
Q1/2022
2
Q2/2022
1
However my, query does not show my expected result. With the distinct keyword I make sure that in every quarter one address is only counted once, but will be counted again in the following quarters...
Can you tell me how I need to adjust my query that I count same addresses only once and for the quarter where they appeared for the very first time?
with everything as (
select contract_address as ca, date_trunc('quarter', evt_block_time) as time
from erc20."ERC20_evt_Transfer"
)
select time, count(distinct ca) as "Count"
from everything
group by time
try this:
with everything as (
select
contract_address as ca,
min(date_trunc('quarter', evt_block_time)) as time
from erc20."ERC20_evt_Transfer"
group by contract_address
)
select time, count(ca) as "Count"
from everything
group by time
I am trying to answer a question on a case using the Query function on Google Sheets and am stuck on a particular problem.
I need to get the total number of unique orders per year. I used the formula below and managed to get the total orders per year.
=QUERY(raw_data!$A$1:$U$9995, "select YEAR(C), COUNT(B) group by YEAR(C)", 1)
Where column C is the date and B is the order_id.
The problem is that this returns a total of 9994 orders and includes duplicates of the same order. For example, if a customer purchased 3 different products, they would each be given a line in the database and would count as 3 of the 9994 orders. However, they all have the same order_id.
I need to get the number of unique orders per year. I know this number is 5009 since I did some manual research through Excel, but wanted to find that same total, separated by year, using the Query Function since this is a case to test my SQL Knowledge.
Is this possible? Does the Query Function have a way to get the count for unique order_ids? Thank you very much for your help!
See if this helps
=QUERY(UNIQUE(raw_data!$B$1:$C$9995), "select YEAR(Col2), COUNT(Col1) where Col2 is not null group by YEAR(Col2)", 1)
I have a query that I am currently using to find counts
select Name, Count(Distinct(ID)), Status, Team, Date from list
In addition to the counts, I need to calculate a goal based on weighted average of counts per status and team, for each day.
For example, if Name 1 counts are divided into 50% Status1-Team1(X) and 50% Status2-Team2(Y) yesterday, then today's goal for Name1 needs to be (X+Y)/2.
The table would look like this, with the 'Goal' field needed as the output:
What is the best way to do this in the same query?
I'm almost guessing here since you did not provide more details but maybe you want to do this:
SELECT name,status,team,data,(select sum(data)/(select count(*) from list where name = q.name)) FROM (SELECT Name, Count(Distinct(ID)) as data, Status, Team, Date FROM list) as q
I want to display the customer name and its sum of all the bills he paid between specific dates. I also want the whole list of customers between the 2 given dates with sum of their total bills.
In list what I want is first Customer Name then its regarding sum of all the bill totals.
I have tried many options for it one where I am getting the sum of that bill only every bill is appearing separately while I want to group it by the customer thats all.
Raw query is working in phpMyAdmin but I failed to convert it to Laravel Eloquent. I want to make this raw query to work using Eloquent.
Query:
select customer_id,sum(total),sum(totalcgst),sum(totalsgst),
sum(totaligst) from sales where
date BETWEEN '2018-03-01' and '2018-03-31'
group by customer_id
This is the result I am getting in database
Try this:
ORM_NAME::selectRaw('customer_id, sum(total), sum(totalcgst), sum(totalsgst)')->whereBetween('date', ['2018-03-01', '2018-03-01'])->groupBy('customer_id)->get();
I have reviewed the various 'Conditional Sum' questions in the forum and none quite match what I'm trying to do:
In the database, there is Date, Store#, Item#, and %Total Sales. In some cases, the same item# for the same date may be given more than one value for %Total Sales. (For some reason this is a valid business scenario that happens rarely, but it happens.)
In that situation only, the requirement is to sum the two values together into one line. So if Item# 123 has a line with a value of .05%, and another line with a value of .08%, I need to sum those two values into one line for Item #123 that has a %Total of .13%. ONLY when an item has more than one percentage assigned, those percentages should be summed. Otherwise, if an item# has only one percentage value assigned, we just want to see that value.
I cannot figure out how to do this. Basically, I would like to implement logic that would work like this:
SELECT Date, Store#, Item#,
CASE WHEN Count(%Total Sales) >1 THEN Sum(%Total Sales)
ELSE %Total Sales
END
FROM (some tables and joins)
GROUP BY Date, Store#, Item#
However, I'm not sure how to craft it so that I don't get a syntax error (this query produces errors).
Any help would be appreciated.
Thank you!
Provided you group by Date, Store#, Item# the only values that return multiple lines are those with multiple values for Total Sales of the same item on the same date.
Therefore grouping the items should be sufficient.
SELECT Date, Store#, Item#, Sum(%Total Sales)
FROM (some tables and joins)
GROUP BY Date, Store#, Item#
Why don't you make an update instead of entering a new value in the table.
Rather than manipulating of the data after insertions.
I believe this is a better idea that you can check for the existing date and update the percentage at that moment itself.
Is it not possible?