I execute following query but query is not running properly? - postgresql

I execute following query
select sub_category, sum(quantity) from "Recon".fk_starchi group by sub_category
how to edit output in postgrsql
the output of above query is
sub_ctegory sum
0
shoe 1121
sandle 2101
I dont want first row where sum is 0 and sub category is blank space .
what changes should i do in my query .

Your output tells me that you have one or more rows where the subcategory is null. To find them,
select *
from "Recon".fk_starchi
where sub_category is null;
To fix that data, you need to either update those rows with a valid subcategory, or delete those rows.
If you don't want to fix the data, you can suppress that unwanted row like this.
select sub_category, sum(quantity)
from "Recon".fk_starchi
where sub_category is not null
group by sub_category;

The output means you have a record with empty sub_category and quantity=0 somewhere in the table.
If you want to ignore it you can simply add 'WHERE quantity>0'

Related

How to check if multiple column values exists in another array?

I hope I will explain my problem
I have table something like this:
id|id_2 (table test)
1,2
1,3
1,4
2,3
2,4
now i have array of values (id_2's) [2,3,4].
How to get only id's (from table test) where ALL id_2 columns are in array [2,3,4]?
So, in my example, i need value id=1 because id=2 doesn't have value 2.
SELECT DISTINCT(t.id) FROM test t WHERE t.id_2 IN
(SELECT DISTINCT(t2.id_2) FROM test t2);
This query would solve your problem.

How to select and count a column in Postgres?

So I have two tables,
From the first table app_catalog, I want to select all the data,
the complete the condition for the second table name statistics_log
So at the second table, I want to select app_id that relates to the first table and the most important thing I want to count the field log_type for each app and do a grouping and get 10 with max number.
I am really trying to get this done, I would appreciate every help thank you!!
Thank you but i manage to solve it,
SELECT * FROM public.app_catalog as a , (SELECT app_id , COUNT(*) as count
FROM public.statistics_log
where logged_at between '2019-08-20 12:22:40.186' and '2019-08-22 12:22:40.186'
GROUP BY app_id
LIMIT 10) AS App_id
where App_id.app_id = a.app_catalog_id

How to get distinct results with max of a field

I have a query :
select distinct(donorig_cdn),cerhue_num_rfa,max(cerhue_dt) from t_certif_hue
group by donorig_cdn,cerhue_num_rfa
order by donorig_cdn
it returns me some repeated ids with different cerhue_num_rfa
how do i return only one line for the repeated ids with cerhue_num_rfa that matches the max of date (cerhue_dt) .. and have at the end only 10 results instead of 15 ?
Postgres has SELECT DISTINCT ON to the rescue. It only returns the first row found for each value of the given column. So, all you need is an order that ensures the latest entry comes first. No need for grouping.
SELECT DISTINCT ON (donorig_cdn) donorig_cdn,cerhue_num_rfa,cerhue_dt
FROM t_certif_hue
ORDER BY donorig_cdn, cerhue_dt DESC;

Duplicate values returned with joins

I was wondering if there is a way using TSQL join statement (or any other available option) to only display certain values. I will try and explain exactly what I mean.
My database has tables called Job, consign, dechead, decitem. Job, consign, and dechead will only ever have one line per record but decitem can have multiple records all tied to the dechead with a foreign key. I am writing a query that pulls various values from each table. This is fine with all the tables except decitem. From dechead I need to pull an invoice value and from decitem I need to grab the net wieghts. When the results are returned if dechead has multiple child decitem tables it displays all values from both tables. What I need it to do is only display the dechad values once and then all the decitems values.
e.g.
1 ¦123¦£2000¦15.00¦1
2 ¦--¦------¦20.00¦2
3 ¦--¦------¦25.00¦3
Line 1 displays values from dechead and the first line/Join from decitems. Lines 2 and 3 just display values from decitem. If I then export the query to say excel I do not have duplicate values in the first two fileds of lines 2 and 3
e.g.
1 ¦123¦£2000¦15.00¦1
2 ¦123¦£2000¦20.00¦2
3 ¦123¦£2000¦25.00¦3
Thanks in advance.
Check out 'group by' for your RDBMS http://msdn.microsoft.com/en-US/library/ms177673%28v=SQL.90%29.aspx
this is a task best left for the application, but if you must do it in sql, try this:
SELECT
CASE
WHEN RowVal=1 THEN dt.col1
ELSE NULL
END as Col1
,CASE
WHEN RowVal=1 THEN dt.col2
ELSE NULL
END as Col2
,dt.Col3
,dt.Col4
FROM (SELECT
col1, col2, col3
,ROW_NUMBER OVER(PARTITION BY Col1 ORDER BY Col1,Col4) AS RowVal
FROM ...rest of your big query here...
) dt
ORDER BY dt.col1,dt.Col4

Select only half the records

I am trying to figure out how to select half the records where an ID is null. I want half because I am going to use that result set to update another ID field. Then I am going to update the rest with another value for that ID field.
So essentially I want to update half the records someFieldID with one number and the rest with another number splitting the update basically between two values for someFieldID the field I want to update.
In oracle you can use the ROWNUM psuedocolumn. I believe in sql server you can use TOP.
Example:
select TOP 50 PERCENT * from table
You can select by percent:
SELECT TOP 50 PERCENT *fields* FROM YourTable WHERE ...
update x set id=#value from (select top 50 percent * from table where id is null) x
The following SQL will return the col_ids of the first half of the table.
SELECT col_id FROM table
WHERE rownum <= (SELECT count(col_id)/2 FROM table);
If the total number of col_ids is an odd number then you will get the first half - 1. This is because, for instance, we have 51 total records, the count(col_id)/2 returns 25.5, and since there is no rownum equal to this result, we get everything equal to 25 and below. That means the other 26 are not returned.
However, I have not seen the reverse statement working:
SELECT col_id FROM table
WHERE rownum > (SELECT count(col_id)/2 FROM table);
So if you want the other half of the table, you could just store the first results into a temp table, lets call it TABLE_A. Then just do MINUS on the original table from this table:
SELECT col_id FROM table
MINUS
SELECT col_id FROM table_a
Hopefully this helps someone.