SQL query missing select statement fetching data error - sql

I am using SQL query fetching some issue data is not fetching black show.
I am sharing this query here. Please help me.
SQL query here
select * from products where hitproduct='0' ORDER BY id DESC and user_id='$user_id'

A SQL query only has one where clause. Presumably, you intend:
select p.*
from products p
where user_id = ? and
hitproduct = 0 -- looks like a number, so I assume it is
order by id desc;
Note the use of ?. This represents a parameter placeholder. Don't munge query strings with parameters values! Learn to use parameters properly.

In your given query and user_id='$user_id' should be before ORDER BY.
select *
from products
where hitproduct='0' and user_id='$user_id'
ORDER BY id DESC

Related

SQL simple GROUP BY query

Is there a way to make a simple GROUP BY query with SQL and not use COUNT,AVG or SUM? I want to show all columns and group it with a single column.
SELECT * FROM [SPC].[dbo].[BoardSFC] GROUP BY boardsn
The query above is working on Mysql but not on SQL, is there a way to achieve this? any suggestion would be great
UPDATE: Here is my data I just need to group them by boardsn and get imulti equals to 1
I thing you just understand 'group data' in a different way than it is implemented in sql server. You simply want rows that have the same value together in the result and that would be ordering not grouping. So maybe what you need is:
SELECT *
FROM [SPC].[dbo].[BoardSFC]
WHERE imulti = 1
ORDER BY boardsn
The query above is working on Mysql but not on SQL, is there a way to achieve this? any suggestion would be great
No, there is not. MySQL only lets you do this because it violates the various SQL standards quite egregiously.
You need to name each column you want in the result-set whenever you use GROUP BY. The SELECT * feature is only provided as a convenience when working with data interactively - in production code you should never use SELECT *.
You could use a TOP 1 WITH TIES combined with a ORDER BY ROW_NUMBER.
SELECT TOP 1 WITH TIES *
FROM [SPC].[dbo].[BoardSFC]
ORDER BY ROW_NUMBER() OVER (PARTITION BY boardsn ORDER BY imulti)
Or more explicitly, use ROW_NUMBER in a sub-query
SELECT *
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY boardsn ORDER BY imulti) as RN
FROM [SPC].[dbo].[BoardSFC]
) q
where RN = 1

Which product is ordered most frequently?

SELECT ProductID
FROM OrderLine_T
GROUP BY ProductID
ORDER BY COUNT(ProductID) DESC
I'm ordering the products like this but LIMIT or ROWNUM is not functioning for some reason. I need to have a query with only the single most frequently ordered product. Im Using Teradata and the database name is db_pvfc10_big . Im sorry for the confusing question its my first question and im a beginner in using SQL
Thank you in advance
The LIMIT keyword is a MySQL-specific extension to the standard.
And ROWNUM is a pseudo column specific to Oracle.
So there are definitely "some reasons" that you might observe LIMIT and ROWNUM as "not functioning".
The question doesn't indicate which RDBMS is being used... MySQL, PostgreSQL, Oracle, SQL Server, DB2, Teradata, etc.
(NOTE: using "not functioning" as the only description of the behavior you observe is rather imprecise.
The description doesn't indicate whether the execution of the query is returning an error of some kind, or if the query is executing and returning a resultset that isn't expected.
The statement(s) you describe as "not functioning" aren't even shown.
One ANSI-standard SQL approach to getting a result is getting that "maximum" value using the standard MAX() aggregate. One way to do that is using an inline view. For example:
SELECT MAX(s.cnt) AS max_cnt
FROM ( SELECT COUNT(t.productid) AS cnt
FROM orderline_t t
GROUP BY t.productid
) s
That can also be used as an inline view...
SELECT MAX(q.productid)
FROM ( SELECT MAX(s.cnt) AS max_cnt
FROM ( SELECT COUNT(t.productid) AS cnt
FROM orderline_t t
GROUP BY t.productid
) s
) r
JOIN ( SELECT p.productid
, COUNT(p.productid) AS cnt
FROM orderline_t p
GROUP BY p.product_id
) q
ON q.cnt = r.max_cnt
Note that if there are two or more products that are ordered the same "maximum" number of times, this query will return just one of those productid.
This should work in most relational databases.
There are other query patterns that will return an equivalent result.
But this example should help explain why most RDBMS offer extensions to the SQL standard, which often make for simpler queries.
MySQL "... ORDER BY ... LIMIT 1"
SQL Server "SELECT TOP 1 ..."
etc.
You could try including the count(ProductID) in the select statement. Some sql databases use the keyword "top" instead of "limit". So if you're using one of those (like Teradata sql for example), do the following:
select top 1 ProductID, count(ProductID)
from OrderLine_T
group by ProductID
order by 2 desc;

How to read the maximum date in this SQL query?

Below is the image of the query result. I want to show Tucson/Boulder only once based on maximum 'addressvalidfrom'. How can I create/modify the query?
If you do not want to use grouping (to persist the rest of the query) you can add a ROW_NUMBER column and filter it where it is 1.
Example
SELECT * FROM
( -- insert your query here with new line below in the select fields
, ROW_NUMBER() OVER (PARTITION BY CUST_RETAIL_CHANNEL_NAME ORDER BY addressvalidfrom DESC) AS Rnk
) D
WHERE D.Rnk=1
use a max for the addressvalidfrom field, and a group by for the other fields.
I can show you if you post the actual query.
http://www.w3schools.com/sql/sql_groupby.asp
where the aggregate is your max(addressvalidfrom)
Can you also post what you want to get as a result if possible.

Why do partitions require nested selects?

I have a page to show 10 messages by each user (don't ask me why)
I have the following code:
SELECT *, row_number() over(partition by user_id) as row_num
FROM "posts"
WHERE row_num <= 10
It doesn't work.
When I do this:
SELECT *
FROM (
SELECT *, row_number() over(partition by user_id) as row_num FROM "posts") as T
WHERE row_num <= 10
It does work.
Why do I need nested query to see row_num column? Btw, in first request I actually see it in results but can't use where keyword for this column.
It seems to be the same "rule" as any query, column aliases aren't visible to the WHERE clause;
This will also fail;
SELECT id AS newid
FROM test
WHERE newid=1; -- must use "id" in WHERE clause
SQL Query like:
SELECT *
FROM table
WHERE <condition>
will execute in next order:
3.SELECT *
1.FROM table
2.WHERE <condition>
so, as Joachim Isaksson say, columns in SELECt clause are not visible in WHERE clause, because of processing order.
In your second query, column row_num are fetched in FROM clause first, so it will be visible in WHERE clause.
Here is simple list of steps in order they executes.
There is a good reason for this rule in standard SQL.
Consider the statement:
SELECT *, row_number() over (partition by user_id) as row_num
FROM "posts"
WHERE row_num <= 10 and p.type = 'xxx';
When does the p.type = 'xxx' get evaluated relative to the row number? In other words, would this return the first ten rows of "xxx"? Or would it return the "xxx"s in the first ten rows?
The designers of the SQL language recognize that this is a hard problem to resolve. Only allowing them in the select clause resolves the issue.
You can check this topic and this one on dba.stockexchange.com about order in which SQL executes SELECT clause. I think it aplies not only for PostgreSQL, but for all RDBMS.

Number of Groups returned by a query

I am using an SQL query with of form
SELECT...FROM...WHERE...GROUP BY id
I want to know how many groups this query returns. How to do this?
SELECT count(id) FROM....WHERE...GROUP BY id
#Tyler Ferraro answers should solve it.
In case the SQL query is very complicated, you can use a nested query:
SELECT COUNT(*) FROM (SELECT...FROM...WHERE...GROUP BY id)
Try this:
select count(*) from (Your SQL).