matching customer id value in postgresql - sql

new to learning sql/postgresql and have been hunting all over looking for help with a query to find only the matching id values in a table so i can pull data from another table for a learning project. I have been trying to use the count command, which doesn't seem right, and struggling with group by.
here is my table
id acct_num sales_tot cust_id date_of_purchase
1 9001 1106.10 116 12-Jan-00
2 9002 645.22 125 13-Jan-00
3 9003 1096.18 137 14-Jan-00
4 9004 1482.16 118 15-Jan-00
5 9005 1132.88 141 16-Jan-00
6 9006 482.16 137 17-Jan-00
7 9007 1748.65 147 18-Jan-00
8 9008 3206.29 122 19-Jan-00
9 9009 1184.16 115 20-Jan-00
10 9010 2198.25 133 21-Jan-00
11 9011 769.22 141 22-Jan-00
12 9012 2639.17 117 23-Jan-00
13 9013 546.12 122 24-Jan-00
14 9014 3149.18 116 25-Jan-00
trying to write a simple query to only find matching customer id's, and export them to the query window.

Related

SQL query that returns the value from one column which is duplicated but in other column has 1 null and one value

Important: Metabase SQL is READ only, this metabase is using a POSTGRESQL.
This is my table:
Origin
Shipment
Product
Quantity
Purchase.23
shipment.42
356
2
shipment.42
356
2
Purchase.25
shipment.422
256
4
shipment.422
256
4
Purchase.18
shipment.427
666
4
shipment.427
666
4
Purchase.20
shipment.427
666
3
shipment.427
666
3
shipment.427
677
3
shipment.9999
222
9
shipment.427
677
1
shipment.9999
222
9
shipment.1111
7
11
shipment.1111
564
6
shipment.422
256
4
shipment.427
666
4
shipment.5555
756
1
Purchase.22
shipment.427
677
4
shipment.4444
755
5
Purchase.77
shipment.3333
751
9
Notice in the table above, that Purchase.22 has 4 in Quantity and there are two shipments with the same shipment as the Purchase.22 row with 1 and 3 in quantity, both summed are the 4 of the Purchase.22 row.
The query should return this:
Origin
Shipment
Product
Quantity
Purchase.23
shipment.42
356
2
shipment.42
356
2
Purchase.25
shipment.422
256
12
shipment.422
256
4
shipment.422
256
4
shipment.422
256
4
Purchase.18
shipment.427
666
8
shipment.427
666
4
shipment.427
666
4
Purchase.20
shipment.427
666
3
shipment.427
666
3
Purchase.22
shipment.427
677
4
shipment.427
677
3
shipment.427
677
1
A little bit more of info, I'm working on a BI tool named Metabase, I'm a junior I just started a month ago and they gave me this stuff to do and the query I'm making is to know how many articles I have on the ivnentory. Unfortunately some articles are duplicated, if they were all duplciated I could easily divide the quantity by 2. But i's never easy. I have values that completly lack of origin and values that lack of shipment and both aren't coupled with other value that has both, and other products that are triplicated and have different quantitites, hence I cannot divide it that easily.
I tried to find any pattern with other columns form the table, but nothing worked at least on shipment.out while in other shipment values like shipment.out.return I found patterns that helped me without the necessity of using a SQL query.

How to group by a set of numbers in a column

I have a table as below. I want to do a group by in such a way that 1-4 weeknums are joined together and 5-8 weeknums are joined together. Or in other words i want to get the monthly total from below fields
table1
weeknum amount
1 1000
2 1100
3 1200
4 1300
5 1400
6 1500
7 1600
8 1700
The output i need is as below
output
max(weeknum) sum(amount)
4 4600
8 6200
The below answer did not work exactly for my actual values as below. I want to start with 4 weeks grouping. The formula (weeknum-1)/4 returns 3 groups as in the expected is only 2
weeknum Group Expr Expected Group Expr
1855 463 463
1856 463 463
1857 464 463
1858 464 463
1859 464 464
1860 464 464
1861 465 464
1862 465 464
Need to execute the query in oracle
Try using FLOOR that rounds the number down in the group by clause:
SELECT MAX(t.weeknum),sum(amount)
FROM table1 t
GROUP BY FLOOR((t.weeknum-1)/4)
This will make sure every 4 weeks are treated as a group :
(1-1)/4 -> 0
(2-1)/4 -> 0
...
(5-1)/4 -> 1

Getting more data while converting data int to float and doing division and Multiplying with int?

I have three columns as shown in below tableA
Student Day Shifts
129 11 4
91 9 6
166 19 8
164 26 12
146 11 6
147 16 8
201 8 3
164 4 2
186 8 6
165 7 4
171 10 4
104 5 4
1834 134 67
I am writing a tvf to calculate Value of Points generated for Students as below
ALTER function Statagic(
#StartDate date
)
RETURNS TABLE
AS
RETURN
(
with src as
( select
Division=case when Shifts=0 then 0 else cast(Day as float)/cast(Shifts as float) end,*
from TableA
)
,tgt as
(select *,Points=Student*Division from src
)
select * from tgt)
When i execute above tvf(select * from Statagic('3/16/2014'))
My output is below
129 11 4 2.75 354.75
91 9 6 1.5 136.5
166 19 8 2.375 394.25
164 26 12 2.16666666666667 355.333333333333
146 11 6 1.83333333333333 267.666666666667
147 16 8 2 294
201 8 3 2.66666666666667 536
164 4 2 2 328
186 8 6 1.33333333333333 248
165 7 4 1.75 288.75
171 10 4 2.5 427.5
104 5 4 1.25 130
1834 134 67 2 3668
Note :
If you see the last row for three columns in the table is the total of rest column.So when you see the last row in the Output of TVF for last two columns when i am adding i am not getting same data i am getting more.
Guys please help me i am struggling to fix this bug i tried in all ways but i am unable to fix it.
select 354.75+136.5+394.25+355.333333333333+267.666666666667+294+536+328+248+288.75+427.5+130=3760.750000000000
3668 is not euql to 3760.75(I am getting more 100 value)

SQL Query: How to pull counts of two coulmns from respective tables

Given two tables:
1st Table Name: FACETS_Business_NPI_Provider
Buss_ID NPI Bussiness_Desc
11 222 Eleven 222
12 223 Twelve 223
13 224 Thirteen 224
14 225 Fourteen 225
11 226 Eleven 226
12 227 Tweleve 227
12 228 Tweleve 228
2nd Table : FACETS_PROVIDERs_Practitioners
NPI PRAC_NO PROV_NAME PRAC_NAME
222 943 P222 PR943
222 942 P222 PR942
223 931 P223 PR931
224 932 P224 PR932
224 933 P224 PR933
226 950 P226 PR950
227 951 P227 PR951
228 952 P228 PR952
228 953 P228 PR953
With below query I'm getting following results whereas it is expected to have the provider counts from table FACETS_Business_NPI_Provider (i.e. 3 instead of 4 for Buss_Id 12 and 2 instead of 3 for Buss_Id 11, etc).
SELECT BP.Buss_ID,
COUNT(BP.NPI) PROVIDER_COUNT,
COUNT(PP.PRAC_NO)PRACTITIONER_COUNT
FROM FACETS_Business_NPI_Provider BP
LEFT JOIN FACETS_PROVIDERs_Practitioners PP
ON PP.NOI=BP.NPI
group by BP.Buss_ID
Buss_ID PROVIDER_COUNT PRACTITIONER_COUNT
11 3 3
12 4 4
13 2 2
14 1 0
If I understood it correctly, you might want to add a DISTINCT clause to the columns.
Here is an SQL Fiddle, which we can probably use to discuss further.
http://sqlfiddle.com/#!2/d9a0e6/3

SQL query self join

I am working on a query for a report in Oracle 10g.
I need to generate a short list of each course along with the number of times they were offered in the past year (including ones that weren't actually offered).
I created one query
SELECT coursenumber, count(datestart) AS Offered
FROM class
WHERE datestart BETWEEN (sysdate-365) AND sysdate
GROUP BY coursenumber;
Which produces
COURSENUMBER OFFERED
---- ----------
ST03 2
PD01 1
AY03 2
TB01 4
This query is all correct. However ideally I want it to list those along with COURSENUMBER HY and CS in the left column as well with 0 or null as the OFFERED value. I have a feeling this involves a join of sorts, but so far what I have tried doesn't produce the classes with nothing offered.
The table normally looks like
REFERENCE_NO DATESTART TIME TIME EID ROOMID COURSENUMBER
------------ --------- ---- ---- ---------- ---------- ----
256 03-MAR-11 0930 1100 2 2 PD01
257 03-MAY-11 0930 1100 12 7 PD01
258 18-MAY-11 1230 0100 12 7 PD01
259 24-OCT-11 1930 2015 6 2 CS01
260 17-JUN-11 1130 1300 6 4 CS01
261 25-MAY-11 1900 2000 13 6 HY01
262 25-MAY-11 1900 2000 13 6 HY01
263 04-APR-11 0930 1100 13 5 ST03
264 13-SEP-11 1930 2100 6 4 ST03
265 05-NOV-11 1930 2100 6 5 ST03
266 04-FEB-11 1430 1600 6 5 ST03
267 02-JAN-11 0630 0700 13 1 TB01
268 01-FEB-11 0630 0700 13 1 TB01
269 01-MAR-11 0630 0700 13 1 TB01
270 01-APR-11 0630 0700 13 1 TB01
271 01-MAY-11 0630 0700 13 1 TB01
272 14-MAR-11 0830 0915 4 3 AY03
273 19-APR-11 0930 1015 4 3 AY03
274 17-JUN-11 0830 0915 14 3 AY03
275 14-AUG-09 0930 1015 14 3 AY03
276 03-MAY-09 0830 0915 14 3 AY03
SELECT
coursenumber,
COUNT(CASE WHEN datestart BETWEEN (sysdate-365) AND sysdate THEN 1 END) AS Offered
FROM class
GROUP BY coursenumber;
So, as you can see, this particular problem doesn't need a join.
I think something like this should work for you, by just doing it as a subquery.
SELECT distinct c.coursenumber,
(SELECT COUNT(*)
FROM class
WHERE class.coursenumber = c.coursenumber
AND datestart BETWEEN (sysdate-365) AND sysdate
) AS Offered
FROM class c
I like jschoen's answer better for this particular case (when you want one and only one row and column out of the subquery for each row of the main query), but just to demonstrate another way to do it:
select t1.coursenumber, nvl(t2.cnt,0)
from class t1 left outer join (
select coursenumber, count(*) cnt
from class
where datestart between (sysdate-365) AND sysdate
group by coursenumber
) t2 on t1.coursenumber = t2.coursenumber