Complex Postgres Query Issues [closed] - sql

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

Related

oracle sql 12 how to display the number of current account is not displaying , any suggestions please?the query and table below [closed]

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.

Family records in SQL [closed]

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
I have a table structure. One of the missing component is the 'Family column' in my table.
I want to family all the child programs along with the Parent program. So Child programs CP_1 to CP_5 belong to Parent PP_1. So they should all be familiarized as '1'. Similarly the next set CP_1 belong to second set of PP_1 and they should be familiarized as '2'. Any help?!
Desired output:
Rextester: https://rextester.com/UCNJ96841
If I understand correctly, you can just use a cumulative sum on the number of times that CCODE is 'NULL':
select t.*,
sum(case when ccode = 'NULL' then 1 else 0 end) over (partition by cid order by eid) as family_num
from #test t;
Note that 'NULL' is a highly unusual string value. You seem to know the difference between 'NULL' and NULL, but I think the latter might be more appropriate.

SQL Server Temp Table to a Select Distinct Count Distinct quetsion [closed]

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.

Multiple WHERE Query to eliminate data? [closed]

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 ) )

How to find rows with similar data as a specific row in sql [closed]

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.