Combining two SQL results into a single table - mysql

In my mySQL project I need to get a result of 3 columns: name, items, items_with_condition. The data are stored in a single table (myTable).
It's easy get the result of name, items.
SELECT name, COUNT(DISTINCT pid) AS items
FROM myTable
GROUP BY name
Also easy to get the result for name, items_with_condition.
SELECT name, COUNT(DISTINCT pid) AS items_with_condition
FROM myTable
WHERE someColumn='something'
GROUP BY name
But now I am unable to combine these two results into one table with name, items, items_with_condition columns using only SQL. Could you please help me? Thanks a lot!

You can use conditional aggregation:
SELECT
name
, COUNT(DISTINCT pid) AS items
, COUNT(DISTINCT CASE
WHEN someColumn='something' THEN pid
END) AS items_with_condition
FROM myTable
GROUP BY name

Related

How to Select one of the repeated values in a column by groups

So I have this table:
I am trying to produce a column that will look like this (containing the computed results)
I tried this query by it gives an error
SELECT Name, SUM(Amount) AS TAmnt,
ProductPrice-SUM(Amount) AS OutB
FROM t1
GROUP BY Name;
I tried this as well, but the results don't seem to be what i want
SELECT Name,SUM(Amount) AS TAmnt,
SUM(ProductPrice)-SUM(Amount) AS OutB
FROM t1
GROUP BY Name;
Any help to see what I am missing?
Use ANY_VALUE() to prevent the error. It will select the ProductPrice value from any row in the group.
SELECT Name, SUM(Amount) AS TAmnt,
ANY_VALUE(ProductPrice)-SUM(Amount) AS OutB
FROM t1
GROUP BY Name;
You may try aggregating by name and product price:
SELECT
Name,
SUM(Amount) AS TAmnt,
ProductPrice - SUM(Amount) AS OutB
FROM t1
GROUP BY
Name,
ProductPrice;
As mentioned by #Barmar, your current table is not normalized. Given that a product has only one price, you should create a separate prices table, and store this information there.

Select entries appearing in all results from another query

I have a single table.
This table has 2 fields, product IDs and Store IDs. The same product ID can exist with many different Store IDs.
I need to find the products (if any) that are common across all the stores.
I'm having difficulty constructing the correct query, any advice?
You can check distinct store ids count with product id. If distinct Store ids count equal to total stores that will be the product ids you want.
SELECT productID, count(DISTINCT StoreID) as stroes FROM [Table name] GROUP BY productID
HAVING COUNT(DISTINCT StoreID) = (SELECT COUNT(DISTINCT StoreID) FROM [Table name] );
I'm sure you'll get many better answers, but it sounds like you are wanting the reverse of the distinct clause, not sure if this will work though:
SELECT NOT DISTINCT [Product_ID]
FROM TABLENAMEHERE
You could sue count(distinct productID)
select productID
from my_table
group by productID
having count(distinct productID) = (
select count(distinct store)
from my_table )

can a table be selected in an sql query to group the result

hello i just wanted to know is it possible to select the table name in an sql query as part of your result
so for example when using UNION to join 2 tables can you specify where each result is coming from because so far i have had to add an extra column called type to each table to specify and it just seems there could be a better way
SELECT id, name, type
FROM table1
UNION ALL
SELECT id, name, type
FROM table2
LIMIT 20`
result =
id, name, type
id, name, type
but i want to still have the type/table name without selecting it or even having it in my table
pleas let me know if this is possible or if this is the only way to do it ether way thanks in advance
Try this:
SELECT id, name, type, 'table1' AS FromWhichTable
FROM table1
UNION ALL
SELECT id, name, type, 'table2'
FROM table2
LIMIT 20

Mysql query to find distinct rows shows incorrect result

I wish to find the total number of distinct records in a table.
I have a table with the following columns
id, name, product, rating, manufacturer price
This has around 128 rows with some duplicates based on different column names.
I only want to select distinct rows:
select distinct name, product, rating, maufacturer, price from table
This returns 47 rows
For pagination purposes, I need to find the total number of distinct records, so I have another satatement:
select distinct count(name), product, rating, maufacturer, price from table
But this returns 128 instead of 47.
How can I get the total number of distinct rows? Any help will be much appreciated. Thanks
You have the distinct and count reversed.
SELECT COUNT(DISTINCT column_name) FROM table_name
Also, I would drop the extra fields when counting, your results will be unexpected for those other fields.
It is not quite clear if you want to get the count in the SAME query with the results or if you want to run a different query. Here go both solutions. In the result as a new column:
select distinct name, product, rating, manufacturer, price, (
select count(*) from (
select distinct name, product, rating, manufacturer, price from table1
) as resultCount) as resultCount
from table1
Notice the previous solution will repeat the count(*) for each row, which is not very efficient, not even visually appealing. Try running two queries one getting the actual data and the other one to get the amount of records in the table that match that data:
select distinct name, product, rating, manufacturer, price from table1
select count(*) from (
select distinct name, product, rating, manufacturer, price from table1
) as result
Hope this helps
Try adding GROUP BY name, product, rating, maufacturer, price clause
It would require running your actual query TWICE... an INNER for distinct and then get the count of those as a single row returned, and then join that to the original select distinct...
select distinct
t1.product,
t1.rating,
t1.maufacturer,
t1.price,
JustTheCount.DistCnt
from
table t1,
( select count(*) as DistCnt
from ( select distinct
t2.product,
t2.rating,
t2.maufacturer,
t2.price
from
table t2 )
) JustTheCount
In the following query, you're getting rows with distinct names since the DISTINCT clause precedes the name column:
SELECT DISTINCT name, product, rating, maufacturer, price FROM table
However, to get the count of the same records, use the following format:
SELECT COUNT(DISTINCT name) FROM table
Notice that DISTINCT goes inside of the COUNT function so that you're counting the distinct names. You probably don't want to include the other columns in the count query because they will be a random sample from the set. Of course, if you want a random sample, then include them.
Most applications will run the count query first, followed by the query to return the results. Also keep in mind that COUNT(*) is only an estimate, and the value may differ from the actual number of records returned.
SELECT DISTINCT COUNT(name), product FROM table isn't even a valid query in MySQL 4.x. You can't mix aggregate and non-aggregate columns. IN 5.x, it'll run, but the values for the non aggregate columns will be a random sample from the set.
At the risk of sparking some flames here.. you could always use:
SQL_CALC_FOUND_ROWS as the first part of your SQL. This is very mysql specific though.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();

MYSQL GROUP BY multiple and get count

I would like to group products by their associated quoteId and get only the unique products from a table like below:
On top of doing this I would like to add a column that shows the count of how many of that product was in the associated quote similar to the following:
This is beyond my MYSQL skills and I was wondering if I could get a hand figuring out what would I have to do for a query to achieve this?
You can use the aggregation function count() and group by productid, quoteid
select productid, quoteid, count(*)
from my_table
group by productid, quoteid
Take a look at following ...
SELECT
productid, quoteid, COUNT(*) AS count
FROM
product
GROUP BY
productid, quoteid
HAVING
COUNT(*) > 1