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 need help creating a large query to select a subset of data.
I need to filter out certain information before I can do my analysis. First I select any call which reached my programs. Then I need to filter out all of the fake/test calls. Then I need to eliminate the ineligible calls which is dependent on a few factors like if the Owner or Sender column is a specific designation, or if the Creator column is a specific designation ONLY IF the call type is one of two options. It is super complicated for me.. hopefully someone can help.
SELECT * FROM Table1
WHERE CustomerID IN (SELECT CustomerID FROM Table1
WHERE SiteID
IN ('Site1', 'Site2'))
WHERE CustomerID <> (108 different values)
AND OwnerID <> (A)
AND SenderID <> (A)
AND CreatorID <> (A) but ONLY if CallType <> CallType1 or CallType2;
SELECT *
FROM Table1
WHERE CustomerID IN (
SELECT CustomerID
FROM Table1
WHERE SiteID IN ('Site1', 'Site2'))
AND CustomerID not in (108 different values)
AND OwnerID <> (A)
AND SenderID <> (A)
AND not ( CreatorID = (A) and
CallType in ( CallType1, CallType2 ) )
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 9 hours ago.
Improve this question
SELECT
t.UniqueID, t.qty, t.name1, t.Surname, t.City,t.State,t.Specialty, t.Languages,t.Company, t.Email
FROM (
SELECT
s.name1, s.Surname, s.City,s.State,s.Specialty, s.Languages,s.Company, s.Email,s.UniqueID
, COUNT(*) OVER (PARTITION BY s.name1, s.Surname, s.City,s.State) AS qty
FROM [dbo].[Diverse_Full_Dataset] s
) t
WHERE
t.qty > 1
The above query gives the duplicate records like this with Speciality, Languages, Company and Email columns included from the table.
https://i.stack.imgur.com/YL2qx.png
The query gives about 113,721 records.
First I need to place weight on data fields for Speciality, LANGUAGE, COMPANY and EMAIL:
I want to write a query where if I have a record greater than 1, say 4, then I want to check for all 4 of the duplicate rows whether Speciality is null; if not null then delete all duplicate records; now if Speciality is null then check next whether LANGUAGE is null; if not null then delete all duplicate records; now if LANGUAGE is null then check whether Company is null; if not null then delete all duplicate records; now if Company is NULL then check whether Email is null. It sets a priority on these four columns
What is the query for the above scenario?
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 1 year ago.
Improve this question
select distinct b.brid,
(select count(1) from account a
where a.brid= b.brid
and a.acctype='current'
) as num_accounts,
b.baddress.street, b.baddress.city, b.baddress.p_code
from branch b;
the table should display the number of current account,but nothing is showing
Query you posted doesn't make sense. What is b.baddress.street? The way you put it,
b is user
baddress is table
street is column
but what is b.brid, then?
You posted screenshot of ... what? Desired result? Can't be as it displays 2 columns, while query returns 5 of them.
Anyway: this is how it might be done. Try to adjust it to your table(s). Mind letter case (is it really 'current'? Maybe 'CURRENT'? Or ...?)
SELECT b.brid,
COUNT (*) num_accounts,
b.street,
b.city,
b.p_code
FROM branch b JOIN account a ON a.brid = b.brid
WHERE a.acctype = 'current'
GROUP BY b.brid,
b.street,
b.city,
b.p_code;
You can use a correlated subquery for this purpose:
select b.*, -- or whatever columns you want
(select count(1)
from account a
where a.brid = b.brid and a.acctype = 'current'
) as num_accounts
from branch b;
In other words, you appear to have an error in how your are referencing columns in b, but the actual part of the query that does the count is correct.
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
Working on building a query at work for hotel program participation. I have a table dwp_list that has hotelcode (fk) and programname as the other column, plus a table all_hotels that lists all the hotel information using hotelcode as the primary key. What I am trying to do (with no success) is add columns for each programname to create a total participation report. Please see the image for better description. I have tried using the EXISTS function but the subquery is returning more than one record.
There are a total of 18 different programs, so one I figure out how to do one, I can manage the rest pretty easily, but its a pain to do manually.
For a fixed list of program names, you can do conditional aggregation:
select h.*,
max(case when l.programname = 'AAA' then 'x' end) as aaa,
max(case when l.programname = 'BBB' then 'x' end) as bbb,
max(case when l.programname = 'CCC' then 'x' end) as ccc
from all_hotels h
inner join dwp_list l on l.hotelcode = h.hotelcode
group by h.hotelcode
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 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 have a database with 20 columns but on my select I only want the following 3 columns:
Task
UniqueID
MessageID (unique identifier)
How can I get all the distinct UniqueID while returning the above 3 columns.
SELECT DISTINCT(UniqueID), Task, MessageID
doesn't seem to work? :/
DISTINCT is not a function :
SELECT DISTINCT UniqueID
, Task
, MessageID
FROM table
But this will return all unique rows not unique UniqueID. If you want to retrieve only rows with different UniqueID you should use the GROUP BY clause and aggregate functions with fields Task and MessageID to manipulate grouped data.
Try something like:-
SELECT task,
UniqueID,
MessageID,
FROM (SELECT task,
UniqueID,
MessageID,
Row_number() OVER(PARTITION BY UniqueID ORDER BY MessageID) rn
FROM TableName) t
WHERE rn = 1