SELECT attribute with MIN value from set of attribute instances that are part of specific row? - postgresql

I know my question is probably very vague and hard to understand at first glance, and i've sat 30 minutes thinking about a proper title. However my database knowledge is very limited so I have a hard time formulating myself properly yet.
It is part of a school assignment I'm currently doing, where the following is what I'm trying to achieve:
and an ER diagram I made of the system:
What I'm trying to accomplish is, selecting the quantity of the component within the computer_system that has the lowest quantity (current stock) so that I in the dataset I am printing out, am able to state exactly how many of each computer_system the store is able to sell, based on the lowest current quantity of any component the computer_system consists of.
This is the query that i am currently working with to accomplish it, but I've had multiple problems with quantity being ambiguous and other errors every time I try to make a fix. I have consulted a dozen of friends from class, but without luck.
SELECT
computer_system.NAME,
cpu.name as cpu,
gpu.name as gpu,
board.name as mainboard,
pccase.name as pc_case,
ram.name as ram,
component.quantity as qty,
(cpu.price *1.3+ board.price*1.3 + pccase.price*1.3 + ram.price*1.3 + gpu.price*1.3) as computer_system_price
FROM computer_system, component
join component cpu on cpu.id = computer_system.cpu
join component gpu on gpu.id = computer_system.gpu
join component board on board.id = computer_system.mainboard
join component pccase on pccase.id = computer_system.pc_case
join component ram on ram.id = computer_system.ram
JOIN component qty ON qty.quantity = (SELECT MIN(component.quantity) FROM component WHERE component.id IN
(computer_system.pc_case,
computer_system.mainboard,
computer_system.cpu,
computer_system.gpu,
computer_system.ram))

The following code fixed it for me:
SELECT computer_system.name AS "System name",
cpu.name AS "CPU",
gpu.name AS "GPU",
pc_case.name AS "Case",
mainboard.name AS "Mainboard",
ram.name AS "RAM",
FLOOR((cpu.price + mainboard.price + pc_case.price + ram.price + coalesce(gpu.price, 0))*1.3/100)*100+99 AS "System price",
SYSTEM.maxamount
FROM computer_system
join (SELECT name,
id,
price
FROM component) AS cpu
ON cpu.id = computer_system.cpu
left join (SELECT name,
id,
price
FROM component) AS gpu
ON coalesce(gpu.id,0) = computer_system.gpu
join (SELECT name,
id,
price
FROM component) AS pc_case
ON pc_case.id = computer_system.pc_case
join (SELECT name,
id,
price
FROM component) AS mainboard
ON mainboard.id = computer_system.mainboard
join (SELECT name,
id,
price
FROM component) AS ram
ON ram.id = computer_system.ram
join (SELECT computer_system.name,
Min(component.quantity) AS maxamount
FROM computer_system,
component
WHERE computer_system.cpu = component.id
OR computer_system.gpu = component.id
OR computer_system.mainboard = component.id
OR computer_system.pc_case = component.id
OR computer_system.ram = component.id
GROUP BY computer_system.name) AS SYSTEM
ON SYSTEM.name = computer_system.name;

Related

T-SQL Question for Getting One Customer Type When There Can be More Than One Value

We have an organization that can have more than one customer type basically. However, what a user wants to see is either the partner or direct type (customer type is either Direct, Partner1, Partner2, or Partner3 but can be direct plus a partner value but only can be one of the partner values). So if a customer is both (ex: Direct and Partner1) they just want the type that is a partner (ex: Partner1). So I tried splitting out partners only into one temp table from a few tables joining together different org data. I have the same query without any limit pulling into a different temp table. Then I calculate count and put that into a temp table. Then I tried gathering data from all the temp tables. That is where I run into trouble and lose some of the customers where the type is direct (I have a image link below for a directcustomer and a customer who is both). I have been out of SQL for a bit so this one is throwing me...I figure the issue is the fact that I have a case statement referencing a table that a direct customer will not exist in (#WLPO). However I am not sure how to achieve pulling in these customers while also only selecting which partner type it is for a customer that has a partner and is also direct. FYI using MSSMS for querying.
If OBJECT_ID('tempdb..#WLPO') IS NOT NULL
DROP TABLE #WLPO
IF OBJECT_ID('tempdb..#org') IS NOT NULL
DROP TABLE #org
IF OBJECT_ID('tempdb..#OrgCount') IS NOT NULL
DROP TABLE #OrgCount
IF OBJECT_ID('tempdb..#cc') IS NOT NULL
DROP TABLE #cc
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #WLPO
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where os.WhiteLabelPartnerID in (2,3,4)
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #WLPO
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #org
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where 1=1--os.WhiteLabelPartnerID = 1
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #org
Select
OrganizationID,
count(OrganizationID) AS CountOrgTypes
INTO #OrgCount
from #org
where OrganizationID = 7613
group by OrganizationID
select * from #OrgCount
Select distinct
ct.OrganizationID,
ok.OrganizationName,
ct.CountOrgTypes,
case when ct.CountOrgTypes = 2 then wlp.WhiteLabelPartnerID
when ct.CountOrgTypes = 1 then ok.WhiteLabelPartnerID
END AS CustomerTypeCode,
case when ct.CountOrgTypes = 2 then wlp.StateName
when ct.CountOrgTypes = 1 then ok.StateName END As OrgState
INTO #cc
from #org ok
left join #WLPO wlp on wlp.OrganizationID=ok.OrganizationID
join #OrgCount ct on wlp.OrganizationID=ct.OrganizationID
select * from #cc
Select
OrganizationID,
OrganizationName,
CountOrgTypes,
case when CustomerTypeCode = 1 then 'Direct'
when CustomerTypeCode = 2 then 'Partner1'
when CustomerTypeCode = 3 then 'Partner2'
when CustomerTypeCode = 4 then 'Partner3' ELSE Null END AS CustomerType,
OrgState
from #cc
order by OrganizationName asc
DirectCustomer
CustomerwithBoth

Why Group By is not working as expected in PostgreSQL?

I m doing a query and it is showing me the output but not as expected.
Above image it is giving me two extra row (blue indicator) after doing group by which not should be exists in the output. Here is my query
SELECT som.customer_po,
pro.product_id,
pro.product_name,
som.mo_id,
ri.status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id,
product_name,
som.mo_id,
ri.status
I tried different way but it is giving me the same result.
It solves my problem somehow. but i don't know it is best practice or not
SELECT max(som.customer_po) AS customer_po,
max(pro.product_id) AS product_id,
max(pro.product_name) product_name,
max(som.mo_id) mo_id,
max(ri.status) AS risk_status
FROM schema_order_map som
JOIN product pro ON som.label_reference_id = pro.product_id
JOIN risk_information ri ON som.customer_po = ri.customer_po
WHERE ri.created_by = 18
AND ri.product_id = som.label_reference_id
GROUP BY som.customer_po,
pro.product_id

Trying to isolate the hours given a date range (YYYY:MM:DD HH:MM:SS) in SQL and group them. by specific hour intervals regardless of the date

I am struggling trying to extract information out of the database I created in SQL. The views work great and all data is displayed but I am trying to isolate the following:
Isolate time frames from 07:00:00 to 09:00:00.
Still new to coding, so help is appreciated.
SELECT ch.name,
t.date,
t.amount,
t.card AS "Credit Card",
t.id_merchant,
m.name AS "Merchant",
mc.name AS "merchant category"
FROM transaction AS t
JOIN credit_card AS cc
ON (t.card = cc.card)
JOIN card_holder AS ch
ON (cc.cardholder_id = ch.id)
JOIN merchant AS m
ON (t.id_merchant = m.id
JOIN merchant_category AS mc
ON (m.id_merchant_category = mc.id);

SQL Server Select one from multiple rows based on calculation

I'm trying to find the greatest number of days (and the reviewer's badge number) it took one of several reviewers to approve a particular document in a workflow. For example, I have a table that holds several workflow approval steps (submitter, manager, controller, QA), along with their badge numbers, and date they approved. The table is called "Workflow" and has those four workflow steps mentioned above as records in the table, and the main table Design that has a one-to-many relationship with Workflow.
I'm trying to determine how many days for the longest review step (number of days), and the badge number of the reviewer for that step (who is holding up the approval workflow, basically). I've been trying to set independent variables to be used later, but not sure how to also set the badge number and I'm confused. I have tried CASE, IIF, and COALESCE but am not having any luck because I don't want the first true value returned and then stop, I want it to continue to evaluate all the steps. Here is an example of my SQL:
declare #managerTime int = 0
declare #controllerTime int = 0
declare #qaTime int = 0
SET #managerTime = (SELECT DATEDIFF(day, manager.BadgeDate, submitter.BadgeDate)
from Design d
left outer join Workflow submitter on (d.DCRId = submitter.DCRId and submitter.RoleName = 'Submitter')
left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
SET #controllerTime = (SELECT DATEDIFF(day, controller.BadgeDate, manager.BadgeDate)
from Design d
left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
left outer join Workflow controller on (d.DCRId = controller.DCRId and controller.RoleName = 'DCR Controller')
This is how I would do it:
Create table WorkflowDefinition with flow definiton:
Source Destination Description
Submitter System Manager Submitter -> System Manager
System Manager DCR Controller System Manager -> DCR Controller
DCR Controller QA DCR Controller -> QA
Now we use this table to join workflow elements and calculate greatest number of days:
SET #MaxTime = (SELECT MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate))
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
)
When we have this value we can select all workflow elements which took this exact number of days to complete:
Select
destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
where DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = #MaxTime
If you want to know max value of days for every type of step separately then we can do something like this: Calculate max value of days per step type and put this into temp table:
SELECT
MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate)) as maxDays,
flow.Description as StepDescription
into #tmp
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
group by flow.Description
And now use this table to select steps matching max number of days and step description:
Select
destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
inner join #tmp on DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = maxDays and StepDescription = flow.Description
I ended up tackling this from the "many" table (Workflow), instead of the Design table. I used the following SQL which got me the results I was after. Thank you all for trying to make sense of my ramblings.
select
w.DesignId,
w.RoleName,
w.BadgeNumber,
w.BadgeDate,
DATEDIFF(day,
(select x.BadgeDate from Workflow x
where x.BadgeDate is not null
and x.DesignId = w.DesignId
and x.StepOrder = w.StepOrder - 1),
(select b.BadgeDate from Workflow b
where b.BadgeDate is not null
and b.DesignId = w.DesignId
and b.StepOrder = w.StepOrder))
as StepDuration,
w.StepOrder,
TotalDuration = DATEDIFF(day,
(select y.BadgeDate from Workflow y
where y.RoleName = 'Submitter'
and y.DesignId = w.DesignId),
(select v.BadgeDate from Workflow v
where v.RoleName = 'Approver'
and v.DesignId = w.DesignId)),
d.VersionNumber,
d.Title
from Workflow w
inner join Design d on d.DesignId = w.DesignId

How to determine the size of a Full-Text Index on SQL Server 2008 R2?

I have a SQL 2008 R2 database with some tables on it having some of those tables a Full-Text Index defined. I'd like to know how to determine the size of the index of a specific table, in order to control and predict it's growth.
Is there a way of doing this?
The catalog view sys.fulltext_index_fragments keeps track of the size of each fragment, regardless of catalog, so you can take the SUM this way. This assumes the limitation of one full-text index per table is going to remain the case. The following query will get you the size of each full-text index in the database, again regardless of catalog, but you could use the WHERE clause if you only care about a specific table.
SELECT
[table] = OBJECT_SCHEMA_NAME(table_id) + '.' + OBJECT_NAME(table_id),
size_in_KB = CONVERT(DECIMAL(12,2), SUM(data_size/1024.0))
FROM sys.fulltext_index_fragments
-- WHERE table_id = OBJECT_ID('dbo.specific_table_name')
GROUP BY table_id;
Also note that if the count of fragments is high you might consider a reorganize.
If you are after a specific Catalogue
Use SSMS
- Clik on [Database] and expand the objects
- Click on [Storage]
- Right Click on {Specific Catalogue}
- Choose Propertie and click.
IN General TAB.. You will find the Catalogue Size = 'nn'
I use something similar to this (which will also calculate the size of XML-indexes, ... if present)
SELECT S.name,
SO.name,
SIT.internal_type_desc,
rows = CASE WHEN GROUPING(SIT.internal_type_desc) = 0 THEN SUM(SP.rows)
END,
TotalSpaceGB = SUM(SAU.total_pages) * 8 / 1048576.0,
UsedSpaceGB = SUM(SAU.used_pages) * 8 / 1048576.0,
UnusedSpaceGB = SUM(SAU.total_pages - SAU.used_pages) * 8 / 1048576.0,
TotalSpaceKB = SUM(SAU.total_pages) * 8,
UsedSpaceKB = SUM(SAU.used_pages) * 8,
UnusedSpaceKB = SUM(SAU.total_pages - SAU.used_pages) * 8
FROM sys.objects SO
INNER JOIN sys.schemas S ON S.schema_id = SO.schema_id
INNER JOIN sys.internal_tables SIT ON SIT.parent_object_id = SO.object_id
INNER JOIN sys.partitions SP ON SP.object_id = SIT.object_id
INNER JOIN sys.allocation_units SAU ON (SAU.type IN (1, 3)
AND SAU.container_id = SP.hobt_id)
OR (SAU.type = 2
AND SAU.container_id = SP.partition_id)
WHERE S.name = 'schema'
--AND SO.name IN ('TableName')
GROUP BY GROUPING SETS(
(S.name,
SO.name,
SIT.internal_type_desc),
(S.name, SO.name), (S.name), ())
ORDER BY S.name,
SO.name,
SIT.internal_type_desc;
This will generally give numbers higher than sys.fulltext_index_fragments, but when combined with the sys.partitions of the table, it will add up to the numbers returned from EXEC sys.sp_spaceused #objname = N'schema.TableName';.
Tested with SQL Server 2016, but documentation says it should be present since 2008.