Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SELECT productid,productname,price
FROM products E1
WHERE 4 = (SELECT count(*)
FROM products E2
WHERE E1.price =E2.price)
It's working like this
SELECT productid,productname,price
FROM products E1
WHERE (SELECT count(*) FROM products E2 WHERE E1.price =E2.price) = 4
:) Now does it make more sense?
Although it can be simplified
SELECT productid,productname,price,COUNT(*) AS c
FROM products
GROUP BY PRICE
HAVING COUNT(*) = 4
The outer query ill scan all products.
For each product the subquery ill count how many products got the same price.
The filter (where clause) ill avoid subquery counts different from four.
The output ill be all products where there are four products with same price.
If anyone is using MS-SQL the same thing can be done using aggregates (count and having)
Edit Hanky already posted the MS-SQL equivalent query using count and having
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have written a left Join Query that returns all NULLS.
SELECT JourneyID,`TrainSeatID`, PassengerID FROM `TrainSeating`
LEFT JOIN Passenger ON TrainSeating.TrainSeatID = Passenger.PassTrainSeatID
WHERE PassengerID IS NULL;
Does anyone know how I can total up these NULLS and include the total number in the data set?
EDIT: I want to calculate the amount of NULL returns from a specific JourneyID.
Many thanks!
You can use COUNT() to get the number of rows in TrainSeating that have not passengers:
SELECT COUNT(*)
FROM TrainSeating LEFT JOIN
Passenger
ON TrainSeating.TrainSeatID = Passenger.PassTrainSeatID
WHERE PassengerID IS NULL;
EDIT:
If you want this per JourneyId then aggregate
SELECT ts.JourneyId, COUNT(*)
FROM TrainSeating ts LEFT JOIN
Passenger p
ON ts.TrainSeatID = p.PassTrainSeatID
WHERE p.PassengerID IS NULL
GROUP BY ts.JourneyId
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Why "WHERE" exists in SQL? Because "HAVING" can do its tasks plus more than that
Try using HAVING to do this query:
Sum the total sales per country for product 'XYZ'
SELECT country, SUM(amount)
FROM sales
GROUP BY country
HAVING product = 'XYZ'
No, that won't do it, because the groups will include sales for all products. And the HAVING condition is invalid because there are multiple products in each grouping.
The purpose of HAVING is to filter groups, after an aggregation is applied.
The purpose of WHERE is to filter rows, before an aggregation is applied.
SELECT country, SUM(amount)
FROM sales
WHERE product = 'XYZ'
GROUP BY country
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Ok, basically I've got a lot of temp tables I've created and I'm trying to create Validation for the ProvDiff table.
DROP TABLE #ProvDiff;
IF OBJECT_ID ('temp.dbo.#ProvDiff') IS NOT NULL
DROP TABLE #ProvDiff;
SELECT *
INTO #ProvDiff
FROM
(SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]) ProvDiff;
SELECT DISTINCT COUNT(DISTINCT ???) AS 'Unique EI NPIS'
FROM #ProvDiff
In my head it seems like the differences should be able to produce a result and I should be able to do a count on that. But for the life of me I can't figure out how to do that. If I do a count on rendering or pay to then those numbers wouldn't necessarily reflect the value for what are above. I know how many of each are produced for above validation.
Any help would be greatly appreciated
Is this what you want?
SELECT COUNT(*)
FROM (SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]
) ProvDiff;
I don't see why a temporary table would be used for this.
For better or worse, SQL Server does not support select count(distinct *), so you pretty much need a subquery.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get the total cost of all the invoices for a customer. Ideally the end format will be two columns [customer name] and [total of invoices]. I have broken it down into the parts so far so I can check and better understand the process of joining the tables and have done a calculation to get the total of items on each invoice but now I am stuck.
As you can see from my screenshot ( Had to link to my google docs as I couldn't post the image up here - sorry) I am getting the company name listed multiple times. Once for each item and also for each invoice number and then item. How can I change my query to show the customer name only once with the corresponding totals of all the invoices combined?
I have lines 3 and 4 as comments of what I think is next so I can work this in steps before fine tuning the query to my desired output.
Thanks
Select Customer.CustName, Sum(InvoiceItem.Quantity*Item.ItemPrice) As TotalValue
From Customer
Inner Join Invoice On Customer.CustABN = Invoice.CustABN
Inner Join InvoiceItem On Invoice.InvoiceNo = InvoiceItem.InvoiceNo
Inner Join Item On InvoiceItem.ItemNo = Item.ItemNo
Group By Customer.CustName
Something like this should work using SUM and GROUP BY:
SELECT CustomerName, SUM(itemPrice * qty) InvoiceTotal
FROM YourTables With Your Joins
GROUP BY CustomerName
If you posted your entire query above, I could copy and paste into the example. But this should get you going in the right direction.
grouping could help, also you need to check if your dbms allows grouping without using agregate functions (some DBMS do not allw it, and return misleading results).
multiple companies is because of the relation company-invoice-product i guess.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let me make it clear -
I have a table with information such as CourseID, Semester, GPA
I need to find all the CourseID's that have the same GPA(and some more fields) as CourseID='999'
I would also like a solution with AND without nested SELECT
Thanks!
So I have to find all the courseCode that has the same GPA and FailPerc as (Code 999, Year 2011, Sem B, Date 2)
Hope it's more clean now
this might work...
select c1.*
from course c1
inner join course c2 on c1.pga= c2.pga
where c2.courseid = 999
and c1.courseid <> c2.courseid
with subselects
select c1.*
from couser c1
where pga = (select pga
from course c2
where c2.courseid=999)
and c1.courseid <> 999
Before you run any query you need to somehow retrieve the data for the original data row. Unless you're writing your SQL for something like MS Access and can use domain functions like DLOOKUP(), I don't see any other way how you can get this information. This means, you need at least 2 SELECT queries and they must be nested.