I have working in database Postgresql for the first time. I need your help to finding out a solution. One table contains 15 rows With a regn_srno as P.K., another table has the same regn_srno as F.K. I want to count the total number of rows which has the same regn_srno. But My problem is the second table has contain 2 or 3 fields with the same regn_srno. So when i use count in the query it shows 12 (including the same regn_srno), but the original number is 10. Due to the same regn_srno repeat in the second table i got the answer as 12.
When we group by regn_srno we get the result as 1,1,1,1,2,1,2,1,1,1. So i need the query to get the count as 10.Please help me. Please send me the answer through my mail id.
For what I could figure out without tables schema, I think you want
SELECT count(DISTINCT regn_smo) FROM t1 JOIN t2 USING (regn_smo);
You could simply do:
SELECT count(DISTINCT regn_smo) FROM t2
Related
I have two tables-
1) ****Company_Form****
[Contract_No#,Software_Name,Company_Name,Vendor_Code]
2) ****User_Form****
[Contract_No,Invoice_No#,Invoice_Date,Invoice_Amount,Invoice_Submit_Date]
Fields denoted with # and bold are primary keys.
=>The user has to enter a software name for which he wants to get the data of.
=>I have to structure a query in which I have to display the result in the following form:
[Contract#,Software_Name,Company_Name,Invoice_No,Invoice_Date,Invoice_Submission_Date]
Now,
one Contract_No can contain many Invoice_no under its name in
the User Form table.
One Contract_No can occur one time only in
Company_Form table
The retrieved records have to be group by the latest Invoice_Date
I came to the logic that:
I have to first retrieve all the contract numbers with that software
name from Company_Form table.
I have to query that contract number from User_Form table and display
the data for each matched contract no. fetched from Company_Form
table.
The problem is that I am unable to structure a query in SQL that can do the task for me.
Kindly help me in formulating the query.
[PS] I am using SQL with PHP.
I tried a query like:
I tried one approach as :
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.InvoiceSubmitDate
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN(SELECT ContractNo FROM Company_Form WHERE
SoftwareName='$Sname') AND a.ContractNo=b.ContractNo;
But I am getting a error that sub query returns more than 1 row.
Can I get help from this?
I am assuming you are attempting to find the most recent price of the user selected software and its corresponding invoice. Here is an approach to do this. If this is tested to your satisfaction, I can add necessary explanation.
select uf.Contract_No#,
cf.Software_Name,
cf.Company_Name,
uf.Invoice_No#,
uf.Invoice_Date,
uf.Invoice_Amount,
uf.Invoice_Submit_Date
from User_Form uf
inner join (
-- Most recent sale of software
select Contract_No#, max(Invoice_Date)
from User_Form
group by Contract_No#
) latest
on (
-- Filter via join for latest match records
uf.Contract_No# = latest.Contract_No#
and uf.Invoice_Date = latest.Invoice_Date
)
inner join Company_Form cf
on cf.Contract_No# = uf.Contract_No#
where cf.Software_name = :software_name
If the requirement allows your sub query to return more than one row, I would suggest you to use IN instead of = in the where clause of your main query.
Please note that I have just looked at the query and have not fully understood the requirements.
Thanks.
I worked around for some time and finally came to the following query which works like a charm
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.ISD
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN (SELECT ContractNo FROM Company_Form WHERE SoftwareName='$Sname')
AND a.ContractNo=b.ContractNo;
If anybody needs help in understanding the logic of this query,feel free to comment below.
I want to create a statistics page where i can count the number of reasons given in a table ...The reasons are created dynamically and stored in another table.
Table1 contains all the reasons which populate a drop down box...
Table2 contains the entries given by the user one of the columns is the reason value.
I would like to count the amount of times each reason appears .
Thankyou for your time and help
Looks like simply group statement:
select
reason.name reason,
count(*) count
from entry, reason
where
entry.reason = reason.id and
entry.time > now() - interval 7 day -- you probably need some filter
group by reason.id
I have a very strange scenario and would be highly grateful if you can help me out. I have one flat table of five fields, and I need to select everything from say the last three columns.
SAMPLE:
ID CODE BRAND T1 T2
== ==== ===== == ==
1 KUU RR4 1 2
2 KUU R56 2 1
=====================
I need only the values of T1 and T2. The reason I need to get the last two columns is that the table is generated automatically from an Excel file, so the first three columns are static (ID, CODE, BRAND), and only last two columns are dynamic (T1, T2) and can be different next time, like (P1, P2), so I cannot do something like SELECT T1, T2 FROM sampleTable. I am totally stumped any help or advice would be great.
You didn't mention php, but there it is in the tags so:
In your php script, you could select the whole shebang and still access the fields in a numeric order. For example:
$q=mysqli_query("SELECT * FROM SAMPLE");
while($row=mysqli_fetch_array($q,MYSQLI_NUM))
{
printf("col#4: %s -- col#5: %s",$row[3],$row[4]);
}
SELECT 4,5 FROM SampleTable;
4,5 specifies the column number in your table.
In a MySQL table, I would like to take 10 records with DISTINCT values.
I am using Zend Framework.
$select = $this->getAdapter()->select()
->from('table', 'column')->group('column')
->limit(10, 0);
This is the query generated by the above code.
SELECT table.column FROM
table GROUP BY column LIMIT 10
What happens here is that MySQL is taking 10 records first and then applying the group by. So finally, I am getting only 7 records.
How to apply DISTINCT first and then take 10 records from it?
Test that SQL against a table -- MySQL applies the limit last, so doesn't do what you're saying. eg test against
a0 a1
1 1
2 1
3 2
4 2
and do select A.a1 from A group by a1 limit 2. You should see 1, 2, not 1, 1.
[I wanted to say this as a 'comment' rather than an 'answer', but couldn't]
I'm not 100% sure what you are trying to do.
But if i am reading it correct you need 10 records with a certain criteria and then apply the group. not the other way around.
Can't you use WHERE in this case?
SELECT table.column FROM table WHERE "criteria" GROUP BY column LIMIT 10
Regards
Mike
This may help you (I didn't test it but so I'm not sure it's working)
SELECT DISTINCT column FROM table LIMIT 10
If it's not working, you may use a temporary table (like (SELECT column FROM table) TEMP), which will select the distinct elements, then a query which will select the first ten results into this table.
Hope this'll help :)
In ZF, You should use distinct() method into Your query chain :
$select = $this->getAdapter()->select()
->distinct()
->from('table', 'column')
->limit(10, 0);
SELECT DISTINCT column
FROM table
LIMIT 10
GROUP BY column
Not sure how to get it into classes though...
SELECT webcal_entry.cal_id, webcal_entry.cal_name , webcal_entry.cal_priority,
webcal_entry.cal_date , webcal_entry.cal_time , webcal_entry_user.cal_status,
webcal_entry.cal_create_by , webcal_entry.cal_access, webcal_entry.cal_duration ,
webcal_entry.cal_description , webcal_entry_user.cal_category
FROM webcal_entry, webcal_entry_user
WHERE webcal_entry.cal_date BETWEEN '20090601' AND '20090631'
When I execute that query php throws back:
mysql_query(): unable to save result set
MySQL client ran out of memory
When I limit the results I see that it is pulling some 2.8 million results back. This table has 7,241 rows.
I realize I could use LIKE, but I really don't want to go that route.
Thanks for any help!
You are joining the webcal_entry and webcal_entry_user tables in your FROM clause but you do not have any JOIN ON or WHERE conditions constraining which rows from webcal_entry_user to return. The result will be the cartesian product of the two tables, which basically means you'll get back each webcal_entry row that has a valid cal_date, multiplied by the number of rows in the webcal_entry_user table.
In other words, if you webcal_entry_user has 400 rows you'll get back 400*7241 = 2.8 million rows! Yikes!
You're doing a cartesian join. Basically, for each row in the first table you're joining against all the rows in the second. If the first table has 4 rows and the second 10 the result set will have 40 (4x10)
You need to add the key you're joining on that exists in both tables to the WHERE clause. Something like:
AND webcal_entry.user_id = webcal_entry_user.user_id