I have a table like that. (there are more records)
Here is the question. How can I get the first 20 customers having money (amount) between 9000 and 24000 in same bank branch (BranchID), multiple same currency accounts (not only one).
Maybe something like this?
select distinct top 20
yt.CustomerId
from [$your_table] yt
group by yt.CustomerId, yt.BranchId, yt.Currency
having count(*)>1
and sum(yt.amount) between 9000 and 24000
The count(*)>1 catchs those customers with the same branch and currency more than once. Then, for the sum of the amounts (sum(yt.amount)) it gets just those between 9000 and 24000.
Finally, because the same customer could have more than one record (same branch and two different currencys several times - or even several branches), we get the distinct top 20 CustomerId.
In SQL Server you can use TOP for this.
As in:
SELECT TOP 20
FROM TABLE
WHERE filter condition...
Related
This is the SQL I have so far:
SELECT RESERVATION.RESERVATION_ID, RESERVATION.TRIP_ID, CUSTOMER.CUSTOMER_NUM,
RESERVATION.NUM_PERSONS,RESERVATION.TRIP_PRICE
FROM RESERVATION, CUSTOMER
WHERE NUM_PERSONS > '4';
I need to create a column named TOTAL_PRICE that calculates the price for all the reservations with more than 4 people. I've tried multiple different ways and I'm still really confused.
The code I have works and it shows all reservations that have more than 4 people attending. But I need the price for each reservation to be calculated in a separate column.
I think you just want an aggregation query:
SELECT SUM(r.TRIP_PRICE)
FROM RESERVATION r
WHERE NUM_PERSONS > 4;
Currently have a single table with large amount of data in access, due to the size I couldn't easily work with it in Excel any more.
I'm partially there on a query to pull data from this table.
7 Column table
One column GL_GL_NUM contains a transaction number. ~ 75% of these numbers are pairs. I'm trying to pull the records (all columns information) for each unique transaction number in this column.
I have put together some code from googling that hypothetically should work but I think I'm missing something on the syntax or simply asking access to do what it cannot.
See below:
SELECT SOURCE_FUND, GLType, Contract, Status, Debit, Credit, GL_GL_NUM
FROM Suspense
JOIN (
SELECT TC_TXN_NUM TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM HAVING COUNT(GL_GL_NUM) > 1 ) SUB ON GL_GL_NUM = GL_NUM
Hey Beth is this the suggested code? It says there is a syntax error in the FROM clause. Thanks.
SELECT * from SuspenseGL
JOIN (
SELECT TC_TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM
HAVING COUNT(GL_GL_NUM) > 1
Do you want detailed results (all rows and columns) or aggregate results, with one row per tx number?
If you want an aggregate result, like the count of distinct transaction numbers, then you need to apply one or more aggregate functions to any other columns you include.
If you run
SELECT TC_TXN_NUM, COUNT(GL_GL_NUM) GL_NUM
FROM Suspense
GROUP BY TC_TXN_NUM
HAVING COUNT(GL_GL_NUM) > 1
you'll get one row for each distinct txn, but if you then join those results back with your original table, you'll have the same number of rows as if you didn't join them with distinct txns at all.
Is there a column you don't want included in your results? If not, then the only query you need to work with is
select * from suspense
Considering your column names, what you may want is:
SELECT SOURCE_FUND, GLType, Contract, Status, sum(Debit) as sum_debit,
sum(Credit) as sum_credit, count(*) as txCount
FROM Suspense
group by
SOURCE_FUND, GLType, Contract, Status
based on your comments, if you can't work with aggregate results, you need to work with them all:
Select * from suspense
What's not working? It doesn't matter if 75% of the txns are duplicates, you need to send out every column in every row.
OK, let's say
Select * from suspense
returns 8 rows, and
select GL_GL_NUM from suspense group by GL_GL_NUM
returns 5 rows, because 3 of them have duplicate GL_GL_NUMs and 2 of them don't.
How many rows do you want in your result set? if you want less than 8 rows back, you need to perform some sort of aggregate function on each column you want returned.
You could do something like the following:
SELECT S.* FROM
SUSPENSE AS S
INNER JOIN (SELECT DISTINCT GL_GL_NUM, MIN(ID) AS ID FROM SUSPENSE
GROUP BY GL_GL_NUM) AS S2
ON S.ID = S2.ID
AND S.GL_GL_NUM = S2.GL_GL_NUM
Which would return a single row for a unique gl_gl_num. However if the other rows have different data it will not be shown. You would have to either aggregate that data up using SUM(Credit), SUM(Debit) and then GROUP BY the gl_gl_num.
I have attached a SQL Fiddle to demonstrate my results and make this clearer.
http://sqlfiddle.com/#!3/8284f/2
I need to retrieve the number of related subprograms where there is a shared master Prgm_ID in all of the tables in a SQL Server database. There's the master table T_Program which stores the basic info about the parent program, but there are about 20 other tables which contain info about subprograms related to the given Prgm_ID (I.E. If the program's ID is 11353 in the T_Program table, that ID may appear many times in each of the other tables with subprograms).
What I want to do is for each Prgm_ID in the T_Program table, get the count of all the subprograms for it and sum them and then see if that number is over 500. I am really not sure of how to approach this. This query returns the desired results for just one of the tables:
select Prgm_ID, COUNT(prgm_type_a_ID) as TypeA_Count
from T_Prgm_Type_A
order by Prgm_ID
What I want to do is find all the Prgm_ID's that have a summed total of subprograms over 500. This query is returning one of the tables I want to be included in the total sum, but I want each of the tables counts added up and then summed for each Prgm_ID in a single column.
Based off the above query, if a given Prgm_ID returned a count of 200 in that query and then for one of the other tables it had a count of 350 and all the other tables were 0 for that Prgm_ID, I want the final select query's result to look like this:
| Prgm_ID | Subprgm_Count |
| 11353 | 550 |
If the final sum of all the different counts from the subprogram tables is over 500 I want it to show me the Prgm_ID and give me the sum of all those different counts.
You want to use UNION ALL to chain together queries to each of your tables like this, and then do a COUNT against your records, making sure to GROUP BY your Prgm_ID.
SELECT Prgm_ID, COUNT(ID) AS Subprgm_Count FROM (
SELECT a.prgm_type_a_ID AS ID, Prgm_ID FROM TableA a UNION ALL
SELECT b.prgm_type_b_ID, b.Prgm_ID FROM TableB b UNION ALL
SELECT c.prgm_type_c_ID, c.Prgm_ID FROM TableC c
)t
GROUP BY Prgm_ID
HAVING COUNT(ID) >= 5 --Your limit here
SQL Fiddle example
quick disclaimer, THIS IS FOR AN ASSIGNMENT.
The question itself is:
List each credit limit that is held by more than one customer, together with the number of customers of Johnny Smith who have this
limit
There are 5 tables in my schema, the relevant ones are (not complete, just relevant info):
TableCUSTOMER C
C.CREDIT_LIMIT
C.REP_NUM REFERENCES REP
(i.e. the table name is Customer, has columns credit_limit and rep_num, which is a foreign key to the "rep" table.)
Table REP R
R.REP_NUM
R.NAME
(i.e. table name is REP, reps have a number and a name).
The rep in question's number is 20.
My current attempt is this:
SELECT COUNT(C.REP_NUM), C.CREDIT_LIMIT
FROM CUSTOMER C
WHERE C.REP_NUM = '20'
GROUP BY C.CREDIT_LIMIT
HAVING COUNT(C.CREDIT_LIMIT) >= 2;
Which is almost working. The only issue is that there are two repeating credit limits, one where the rep has customers in it, and one where it doesn't. The one where he has customers is showing up, and the other isn't.
To sum it up, rather than getting:
Occurences - Credit_Limit
3 - 7500
0 - 10,000
I'm getting
Occurences - Credit Limit
3 - 7500
How would I go about making that other tuple show up?
use left outer join, eg:
select
*
from a
left outer join b on a.id=b.id
so if there is two rows in and one row in b, will get two rows with left half empty for b part...
Thanks for any help in advance, I can't wrap my SQL skills around this one...
I have two tables like this:
Settings
Customerid ViewerLimit
1 50
2 50
Distribution
Customerid ServerIP
1 stream3
1 stream4
2 stream3
I want to calculate the load on each server. A customer divides the load if they have more than one server, so here customer 1 puts a load of 25 on each server. The result I'm trying to get is this:
ServerIP Load
stream3 75
stream4 25
I tried to do a sum function similar to this:
sum(viewerlimit/(count # of servers)) as load group by serverip
But I can't do a sub query within a sum function. There are many customers and possibly many servers per customer so it will become too complex to do manually. I appreciate any input.
Here is uninspired version with count in derived table:
select serverip, sum (viewerlimit/cast (ServerCount as float)) Load
from
(
select customerid, count(*) ServerCount from distribution group by customerid
) a
inner join settings
on a.customerid = settings.customerid
inner join distribution
on settings.customerid = distribution.customerid
group by serverip
Sql Fiddle to play around
UPDATE - an attempt at explanation
Derived tables are used to produce ad-hoc result sets that can be joined to main body of a query. It is placed in from clause and enclosed in parenthesis. You can use anything an ordinary select can use, top, order by, aggregate functions etc. The only thing you cannot use is correlation to a table in main body. Oh, and CTE. Derived table must be aliased.
In previous sample derived table 'a' selects counts of servers by customerid. Main body sees it as a table with CustomerId and ServerCount columns, ready to be used as any column from all listed tables. A join on customerid is performed between settings and 'a'. As this is 1:1 relation (both tables produce single row given a customerid), no duplication occurs.
How about doing a count of servers in a sub query and assign that to a query variable and then use that query variable inside the sum function?
select d.serverip, sum(s.viewerlimit/d.devider)
from (
select customerid, serverip, COUNT(serverip) over (partition by customerid) servercount
from distribution
) d
join settings s
on s.customerid=d.customerid
group by d.serverip