Family records in SQL [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 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.

Related

How to remove null from results [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 5 months ago.
Improve this question
I have this SQL statement:
select
v.venue_id, v.name
from
venues v
where
not exists (select e.vid
from events e
where e.vid = v.venue_id);
Here is the result of the query:
It works fine but the last row in the result is null, how do I remove that? Been trying for a bit now with no luck
I think this might be what you're experiencing
Why does SQL workbench always return a row full of null values in every query?
Looks like that's normal behavior
The null row is per design, disregard it. i thought you were firstly referring to a column.
on your where statement <field> is not null
So:
select v.venue_id, v.name
from venues v
where not exists (
select e.vid
from events e
where e.vid = v.venue_id
and name is not null
);

Complex Postgres Query Issues [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 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

I have joined few tables and need total of the last column on the end of my query [closed]

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 3 years ago.
Improve this question
I have wrote SQL query pulling 6 columns from 4 different tables, however I need on the last row total just for one column. It should look like this.
Well, you can do:
with cte as (
<your query here>
)
select cte.*
from cte
union all
select 'Total', 'n/a', 'n/a', 'n/a', 'n/a', 'n/a', sum(consentv), 'n/a'
from cte;
Notes:
This does not guarantee that Total' is at the end. You might need order by.
This assumes that all other columns have strings.

sql in ms-access [closed]

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 4 years ago.
Improve this question
I have a table name customers with two columns, case id and owner. I need to write a query to select random 5 caseids for every name in owner column.please help
For a start, you need something like:
SELECT TOP 5
ID,
[Case ID],
[Owner],
Rnd(-Timer()*[ID]) AS RandomRecord
FROM
[Cases]
ORDER BY
Rnd(-Timer()*[ID]);
to be used as a subquery filtered on OwnerID of your Owners' table.
I once posted an article on this with a lot more details:
Random Rows in Microsoft Access
You can use in:
select t.*
from t
where t.id in (select top 5 id
from t as t2
where t2.name = t.name
order by Rnd(-Timer()*[ID])
);

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.