How to insert a particular record 10 times in postgresql - cakephp-2.0

I have 2 tables. Student master table and student qualification table. I want to select a record from the master table and insert it into the qualification table. The only problem is that in the qualification table, each student should have 10 records each.
For Ex: If student master has 5 records, student qualification table must have 5*10 = 50 records
I have tried using Insert into qualification with select from master but the records are not saved for 10 times.
Below is my query:
Insert INTO student.qualification(std_id,qualf,count)
Select std_id,qualf,$i from student.master where status = 'pass');
$i because i am trying to repeat the loop 10 times
can anyone please suggest an alternative to achieve this result
Expected output:
Master Table
Student ID
1
2
3
Qualification Table
Student ID Qualification Count
1 A 1
1 B 2
1 C 3
......
1 J 10
2 A 1
...
2 J 10
Similarly other student records. As mentioned above, i am trying to use a for loop and within that loop the insert_ select query to increment the records
can anyone please suggest an alternative to achieve this result

Related

How to use the Subquery Syntax of SQL Server to Join the Two Table Together and Remove Duplicates? [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 two tables are structured as follows,
Table a
province_id
1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
.
.
.
Table b
f_name city_value label
city_id 1 Austin
ac_id 1 Mayor: Jason Lee
df_id 1 Republican
ef_id 1 Property tax is high
city_id 2 Dallas
dfg_id 2 Mayor: ABC
fth_id 2 Republican-3
tpr_id 2 Property tax is low
city_id 3 Waco
ddd_id 3 Mayor: DEF
ers_id 3 Republican-4
qws_id 3 Property tax is middle
city_id 4 Arlington
zxg_id 4 Mayor: HGR
zUg_id 4 Republican+4
.
.
.
The first table is called a and it just has 1 column province_id.
(It has duplicates and almost 1500000 rows and the unique value is almost 200)
The second table is called b and it has 3 columns:
(the "b" table is a longitudinal data and the total row is almost 400000)
column 1 in table b is called f_name and the value of this variable is linked with column 3 label.
For example, when f_name=city_id and city_value=1, then label`l=Austin (city name).
When f_name=city_id and city_value=2, then label=Dallas (city name).
When f_name=city_id and city_value=3, then label=Waco (city name).
When f_name=city_id and city_value=4, then label=Arlington (city name).
column 2 in table b is called city_value and it has duplicates as well like province_id. For example, for a specific city "Austin", its city_value is 1.
column 3 in table b is called label and the content under this variable is about the information of the city specific to a province.
Within each city_value, the cell content of the variable label is unique. Among all cells within each city_value, the city name (e.g., Austin) is of interest.
What I want is this:
For table "a", please remove the duplicates of province_id column
After deduplicates, table a should look like this
Table a
province_id
1
2
3
.
.
.
As for table "b", please just keep all data records that have city_value and the city name;
After deduplicates, table b should look like this
Table b
f_name city_value label
city_id 1 Austin
city_id 2 Dallas
city_id 3 Waco
city_id 4 Arlington
.
.
.
Join the results of (1) and (2) together using the common key (deduplicated province_id and city_value) and the resulting data record should be equal to the number of deduplicated province_id.
In fact, the unique province_id is just about 200 while the total rows after deduplicas is still 200000 or so. In other words, the range of total rows for table a after deduplicates is far less than that of table b.
If I want to get the same results, how can I write a CTE (common table expressions) code to do this with SQL Server?
It is evident that the cities such as "Austin","Dallas","Waco","Arlington" is what I need.
My code below is not correct because the duplicates is still there after joining the two tables.
SELECT DISTINCT a.province_id, b.label
FROM a
JOIN b ON
a.province_id=b.city_value;
You can first remove all unwanted rows from your tables
SELECT a.province_id, b.label
FROM (SELECT DISTINCT province_id FROM a) a
JOIN (SELECT city_value,label FROM b WHERE f_name = 'city_id') b ON
a.province_id=b.city_value;
or at least remove all duplicates form Table a
SELECT a.province_id, b.label
FROM (SELECT DISTINCT province_id FROM a) a
JOIN b ON
a.province_id=b.city_value
WHERE f_name = 'city_id';

Join return only single row

We have a table called category_merchants where merchants are assigned categories.
category_merchants
unique_id category_id merchant_code
------------------------------------------
1 1 SS64007160
5 1 SS64008089
6 1 SS64000775
All I want to do is get all merchants from the "category_merchants" table who have a category_id of "1". I need to join on the merchants table to grab the merchant name for each.
merchants
id merchant_num merchant_nm
-------------------------------------
892 SS64000775 Ashburton Dressage
2024 SS64007160 Amuri A & P Association
2372 SS64008089 Ashburton A & P Association
I tried the following query, which I thought would return three rows, but only returns one.
SELECT cm.*, me.merchant_nm
FROM category_merchant_t cm
JOIN merchant_t me on me.merchant_num = cm.merchant_code
and cm.category_id = 1
I guess I'm not understanding something. I need all three rows from category_merchants, with the merchant_nm from the merchants table appended to each row.

Oracle update where select statement returns multiple row error

Hi i have a table like this:
ID Name Number
1 john 91234567
2 tom 98765432
3 ken 91357246
...
I am trying to change number [91234567] to another number but i get this single row query returns more than 1 row.
My statement:
Update table set number = '9000000' where id = (select id from table where number = '91234567')
Perhaps i have another record with the same number further down the table.
Since i do not have access to the id, how can i change my statement? Thanks.
Try to use IN instead of =
Update table
set number = '9000000'
where id IN (select id from table where number = '91234567')
If you need to change all the records containing 91234567 to 9000000:
update table
set number = 9000000
where number = 91234567

SQL Deletion of only one field with where clause

I have a table thus
ID stuff1 stuff2
1 10 cool
2 4 poor
3 8 cool
4 1 sucks
I need an sql to delete only 10 in row 1
I used->>> delete stuff1 from mytable where ID=1
This works on access db but does not work on sql server
How do I do this on SQL server???
Are you trying to delete the row, or do you just want to remove the values from column stuff1?
In case you want to delete the row:
DELETE FROM mytable where ID = 1
In case you just want to remove the value of stuff1:
-- to set it to null
UPDATE mytable SET stuff1 = NULL WHERE ID = 1
-- to set it to zero
UPDATE mytable SET stuff1 = 0 WHERE ID = 1
By delete one field, I'm guessing you really mean null the column value:
UPDATE mytable
SET stuff1 = null
WHERE ID=1
DELETE works on whole rows, not individual columns.

how to use count when joining two tables

I have two tables
First Table tblHostel it contains three field like that
HostelName, RoomId, Capacity and content is like that
HostelName RoomId Capacity
Vivakanand 1-1 2
Vivakanand 1-2 1
Vivakanand 1-3 3
And second table is tblStudent having three field UserId,RoomId,HostelName and Data is like that
HostelName RoomId UserId
Vivakanand 1-1 101
Vivakanand 1-1 102
Vivakanand 1-3 103
I want to merge these two tables in such a way I find the following type of out-put
HostelName RoomId Capacity Count
Vivakanand 1-1 2 2
Vivakanand 1-3 3 1
the count table counts room-id in tblStudent.
For individual table I find that output by following command
select RoomId,HostelName,count(RoomId) from tblStudent group by Roomid,Hostel;
But how I merge these two tables to achieve the desire output,I also use join but I can't achieve that.
You need to first join the tables based on the HostelName and RoomID, and then use COUNT function to get the count of students per room, like so:
select h.hostelname, h.roomid, h.capacity, count(s.userid) Count
from tblhostel h
inner join tblstudent s on h.hostelname = s.hostelname
and h.roomid = s.roomid
group by h.hostelname, h.roomid, h.capacity

Resources