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
Hi friends I have table from customer where in I want to get distinct values of its three columns CustomerType, CustomerReg,CustomerID.
Instead of doing three separate queries for the distinct
like select distinct CustomerType From Customer.. and then for CustomerReg..Can it be achieved in one query...
The reason is that i would attach each column distinct values to a specific drop down list box..
You can do this with grouping sets. If you want the values in three separate columns:
select CustomerType, CustomerReg, CustomerId
from Customer
group by grouping sets ((CustomerType), (CustomerReg), (CustomerId))
If I understand correctly, you want to show a distinct list of all the customer types, customerreg and customerIds in a list, in one column?
Try this...
select distinct CustomerType + ' : ' + CustomerReg + ' (' + CustomerId + ')' as Name
from Customer
This will return a string like 'External : 23423412 (2344)'
You should probably order it by something meaningful too.
Try adding
order by Name
Although, you shouldn't need the DISTINCT if a customer can only appear once in the customer table.
Reading your question again, it looks like you want to return a distinct list of each column in one query, not a distinct combination? Then the group by grouping sets mentioned above will probably get you the closest, although depending on the structure of your data, performance might become an issue here if you have lots of customers.
What language are you using in your UI?
If it's .Net, you could open a datareader using 3 separate queries, and then use the datareader.nextresult
There's an explanation here,
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.data.framework.datareader.nextresult.aspx
and an example here,
http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx
Hope that helps
You can even do it like this:
select distinct CustomerType, CustomerReg, CustomerId
from Customer
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 4 years ago.
Improve this question
Sorry, if question unclear, i had misstakes it first tables. I made some updates:
Database: PostgreSQL
I want to group table based on transition (if a=b & b=c then a=c)
Adding a pair (4,c) will merge 2 groups to one "group1".
i assume u want a.b.c to be group1 and the d as group2..
the groupby will work perfectly fine with aliases..
but the number of group op wants is 3 millions groups so a stored proc with a incremental in the end and group by will work fine..
From your comments, it looks like you want to find out transitive relationship.
You can do that with following query. But if the goal here is just to identify the relationship among different groups with their respective id, i guess you can afford to have groups which are not getting incremented with 1.
According to your given example in OP, i think it won't affect you if end result has group1 and group5 instead of group2.
If mention result is fine then you can do that with following updated query. Giving group names in successive manner will impact on query performance which you don't want as you've 3 million of groups.
Please try following query:
select t1.id, concat('group', min(t2.minId)) groups
from t1
join
(select min(id) minId, groups
from t1
group by groups
) t2
on t1.groups = t2.groups
join (select #cnt := 1)y
group by t1.id;
Demo : Click here
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 5 years ago.
Improve this question
Hello, Does anuone knows how can I remowe the null values corresponding to the description of the same product key? So, I have one product Key and its description in each language.
I'm trying to use the coalesce function, but it doesnt return me anything.
It looks like you want to combine the rows together for each ProductKey.
So instead of 5 lines, each with a single column populated, you want one line, with all 5 columns populated.
Do it like this:
Select Distinct T.ProductKey, C1.Column1, C2.Column2, C3.Column3 from MyTable T
left join MyTable C1 on C1.ProductKey = T.ProductKey and C1.Column1 is not null
left join MyTable C2 on C2.ProductKey = T.ProductKey and C2.Column2 is not null
left join MyTable C3 on C3.ProductKey = T.ProductKey and C3.Column3 is not null
Just replace "MyTable" above with your table name, and "Column1, Column2, Column3" with the names of the columns your data is in.
Pretend that each column is on its own separate table, and you need to use joins to connect all the tables back to your master set of ProductKeys.
Think about it in sets:
Basically you are going to make one master list of the keys that is distinct/unique (step 1), and then do a new left join for each column you want to attach to the master list (step 2), and as part of the joins, tell it to get the non-NULL values and ignore the NULLs.
you can use GROUP BY as below.
SELECT ProductKey,
MAX(C_ar_description) AS C_ar_description,
MAX(C_en_description) AS C_en_description,
MAX(C_fr_description) AS C_fr_description
FROM YourTable
GROUP BY ProductKey
You should use the results to fix the data so you dont need to do this every time.
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
I am trying to select all fields and get a distinct in 1 field where a certain condition is true but I am getting an error saying incorrect syntax this is what I have
var sql = #"SELECT * distinct threadID from Threadposts where profileID = 1";
I am new to mssql but can not figure out what is wrong with the query above, I simply want all fields included where profileID= 1 and select distinct values from threadID
You need to be clearer about what you want. You are querying a tabled called Threadposts for records where the profileID is 1. This will return multiple rows with different values I expect.
Are you wanting to count how many posts were made by that person? Are you wanting a list of threads by that person?
-- count of rows
SELECT COUNT(*)
FROM Threadposts
WHERE profileID = 1
-- list of threads
SELECT *
FROM Threadposts
WHERE profileID = 1
If you are wanting something different then you'll need to update your question.
PS: the DISTINCT keyword will look at the data returned and give distinct values only, e.g. if you wanted distinct topics posted by this user regardless of if they created multiple threads with the same name:
SELECT DISTINCT ThreadTopic
FROM Threadposts
WHERE profileID = 1
Using DISTINCT * almost never makes sense, since each row will have a unique ID (hopefully) so will already be distinct.
DISTINCT is essentially short-hand for GROUP BY on all selected columns when you don't need any aggregates. What you are asking for is a query where you are grouping on a single column, but not aggregating the remaining columns. That doesn't make sense.
In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values. The DISTINCT keyword can be used to return only distinct (different) values.
You want to select all the columns but distinct only with a particular one (threadID)
Try using:
var sql = #"SELECT DISTINCT ON threadID * FROM Threadposts WHERE profileID = 1;
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.