SELECT information from another query SQL - sql

I am trying to select information from an already run query using SQL. I don't have the rights to create a view which I am aware would solve my problem. I have run the below (obfuscated) query which is throwing a number of errors:
SELECT
distinct(countValue),
count(countValue)
FROM
(
SELECT customer_identifier, count(distinct(2nd_customer_identifier)) AS countValue FROM table GROUP BY customer_identifier;
)
GROUP BY
distinct(countValue)
The subquery (below) is attempting to get a count of the unique payees for every customer:
SELECT
customer_identifier,
count(distinct(2nd_customer_identifier)) AS countValue
FROM table
GROUP BY aid
and the main query using that is attempting to get the counted values from the above table and count how many times each one occurs.
Any help would be much appreciated.

Can you logically replace it with the following? This will tell you how many multiple-payer situations you have.
SELECT
CountValue,
count(countValue) TotalRecords
FROM
(
SELECT
customer_identifier,
count(distinct([2nd_customer_identifier])) AS countValue
FROM table
GROUP BY customer_identifier
) a
GROUP BY countValue ;
Output will tell you something along the lines of:
There were 25 one-payer accounts
There were 17 two-payer accounts
There were 9 three-payer accounts
Etc.
If that's not what you want, please edit your question to describe the output you are after.

Related

How to use max on sum in oracle sql?

I am working in a task and got stuck at particular question. I am new to SQL so I am reaching out to this platform for the support. Below are the 2 tables. 1st is Theatre_play_table and 2nd is Ticketsales table.
Question: List titles, directors and writers of all shows/plays with the highest total sale.
Theatre_play_table
Ticketsales table
I have pasted screenshot of some part of the table. ID column in both the table represents the same information. Last column in Ticketsales table is Totalamount.
I have tried with below query;
Select theatre_play.title, theatre_play.director, theatre_play.writer, sum(totalamount)
from theatre_play, totalsales
where theatre_play.id = totalsales.id
group by theatre_play.title, theatre_play.director, Theatre_play.writer
order by sum(totalamount) desc
fetch first 3 rows only;
The above approach is not useful when data is huge. I wanted to apply max(sum(totalamount)) but oracle threw an error.
Can anyone please help me solve this question?
If I understand you right, the issue is to get the three highest values?
Then try something like this:
select * from (
Select dpro.title, dpro.director, dpro.writer, sum(fth.totalamount)
from dpro
join fth on dpro.id = fth.id
group by dpro.title, dpro.director, dpro.writer
order by sum(totalamount) desc )
where rownum <=3

How can I get multiplication between 2 different table columns with group by clause?

I tried this whole day but didn't got the result. Please go through the images on the link below
Here is my table 1 structure
and here is my table 2 structure
I want to multiply product_uom_qty from 1st table with cost in 2nd table and then group them by product_id which is there is both tables.
This is my query.
select sum(sale_order_line.product_uom_qty) * product_price_history.cost
from sale_order_line, product_price_history
group by product_id;
And result I am getting is
ERROR: column reference "product_id" is ambiguous. LINE 1: ...m
sale_order_line, product_price_history group by product_id...
please tell me where I am making mistake.
Presumably, you want something like this:
select product_id, sum(sol.product_uom_qty * pph.cost)
from sale_order_line sol join
product_price_history pph
using (product_id)
group by product_id;
That is, you need to join the tables on some condition.
Without sample data and desired results, it is unclear what you really want. But this at least is a sensible query that removes the syntax errors.

SQL Server: Counting Inventory Issue (with subquery)

I currently have a query that is going out into an inventory table (of servers), filtering which ones are 'Developer', and producing a list of distinct users from an audit-related table. Essentially, trying to find out who has access to development servers in this particular inventory.
Everything worked until I added the second line, which I commented out in the code below:
select distinct tAudit.[USER_ID]
--, count(tAudit.[USER_ID]) AS [USER_COUNT]
from table_audit as tAudit
where tAudit.inst_name IN (
SELECT (SUBSTRING([Computer Name],0,CHARINDEX('.',[Computer Name],0))) AS INST_NAME
FROM table_server_inventory
WHERE [SQL Server Edition] = 'Developer'
)
order by tAudit.user_id asc
So, the question is: How can I count how many times a particular user appears? Is there a conflict with the fact I am using distinct? There's another query I produced, purely to see if I was on the right track. This is an example:
select tAudit.[USER_ID]
, count(tAudit.[USER_ID]) AS [USER_COUNT]
from table_audit as tAudit
where tAudit.user_id IN ('user_001', 'user_009', 'user_199', 'user_222')
group by tAudit.user_id
And it looked something like this:
USER_ID USER_COUNT
user_001 5
user_009 32
user_199 14
user_222 8
Ideally, when the primary query is working it'll look like the example above, just with dozens more results.
NOTE: The table_audit is actually very large and lists servers and users each time. Example:
COMPUTER_NAME USER_ID
serverAA user_001
serverAA user_009
serverAA user_199
serverAA user_222
serverBB user_001
serverBB user_009
serverCC user_001
serverCC user_199
serverCC user_222
You just want a GROUP BY query, not SELECT DISTINCT:
select tAudit.[USER_ID], count(tAudit.[USER_ID]) AS [USER_COUNT]
from table_audit as tAudit
where tAudit.inst_name IN (
SELECT (SUBSTRING([Computer Name],0,CHARINDEX('.',[Computer Name],0))) AS INST_NAME
FROM table_server_inventory
WHERE [SQL Server Edition] = 'Developer'
)
group by tAudit.[USER_ID]
order by tAudit.user_id asc

SELECT TOP 1 is returning multiple records

I shall link my database down below.
I have a query called 'TestMonday1' and what this does is return the student with the fewest 'NoOfFrees' and insert the result of the query into the lesson table. Running the query should help explain what i mean. The problem im having is my SQL code has 'SELECT TOP 1' yet if the query returns two students who have the same number of frees it returns both these records. Wit this being a timetable planner, it should only ever return one result, i shall also put the code below,
Many thanks
Code:
INSERT INTO Lesson ( StudentID, LessonStart, LessonEnd, DayOfWeek )
SELECT TOP 1 Availability.StudentID, Availability.StartTime,
Availability.EndTime, Availability.DayOfWeek
FROM Availability
WHERE
Availability.StartTime='16:00:00' AND
Availability.EndTime='18:00:00' AND
Availability.DayOfWeek='Monday' AND
LessonTaken IS NULL
ORDER BY
Availability.NoOfFrees;
This happens because Access returns all records in case of ties in ORDER BY (all records returned have the same values of fields used in ORDER BY).
You can add another field to ORDER BY to make sure there's no ties. StudentID looks like a good candidate (though I don't know your schema, replace with something else if it suits better):
ORDER BY
Availability.NoOfFrees, Availability.StudentID;

SQL query that gives me the count of records with unique column value in table

I am new to SQL and learning some basic things with Treasure Data. I have many records of reservation in a table with 11 distinct resource values that users can reserve.
This gives me the resources in a table:
SELECT
DISTINCT resource
FROM
reservation
;
But what if I just want the output to be the number of records that get returned by the query (i.e., "11").
This doesn't work:
SELECT
COUNT(*) DISTINCT resource
FROM
reservation
;
What is the right syntax for this? I have not been able to figure this out.
The distinct keyword goes inside the aggregate function:
SELECT
COUNT(DISTINCT resource)
FROM
reservation
;