PostgressSql - Table formation - sql

I have a table in below structure which is created using postgresql
Category can have value of (Truck,Bus,Car and Bike)
Lane can have value of (Lane 1 and Lane 2)
From this table structure i need to display as below in the dashboard
I am new to DB side can anyone help in fixing this?
Thanks and Regards

SELECT cameralocation,lane, videoTime,
sum(case when (category='bike') then vehicleavgspeed else 0 end) as "Bike-Speed",
sum(case when (category='bike') then vehiclecount else 0 end) as "Bike-Count",
sum(case when (category='commercial') then vehicleavgspeed else 0 end) as "commercial-Speed",
sum(case when (category='commercial') then vehiclecount else 0 end) as "commercial-Count",
sum(case when (category='car') then vehicleavgspeed else 0 end) as "car-Speed",
sum(case when (category='car') then vehiclecount else 0 end) as "car-Count"
FROM public.inferencedata where lane='${lane}' and inferenceid='${videoTime}' group by cameralocation,lane,videoTime;

Related

Sql loop over unique elements of a column

I am trying to get data of rows into columns. This is my data before query.
This is my query.
SELECT
Date,
SUM(CASE WHEN (Terms="Art") THEN 1 ELSE 0 END) AS Art,
SUM(CASE WHEN (Terms="Action") THEN 1 ELSE 0 END) AS Action,
SUM(CASE WHEN (Terms="Board") THEN 1 ELSE 0 END) AS Board,
SUM(CASE WHEN (Terms="Puzzle") THEN 1 ELSE 0 END) AS Puzzle,
SUM(CASE WHEN (Terms="Adventure") THEN 1 ELSE 0 END) AS Adventure,
SUM(CASE WHEN (Terms="Others") THEN 1 ELSE 0 END) AS Others
FROM
__table__
GROUP BY
Date
After Query this is my data.
Which is fine and intended data. But problem is in query as you can see I have to explicitly write each term. I want to automate that using loop. Theoretical solution is to write subquery to get all unique terms and then loop over them. But I don't know how.

How to add columns from another table using a SELECT

I have a table named Action, which I want to add new columns to it.
The columns that I want to add are the result of this query :
SELECT DATE_AFFECTATION ,
SUM(CASE WHEN STATUT_Ticket = 'Clôturé' THEN 1 ELSE 0 END) AS TotalCloturé,
SUM(CASE WHEN STATUT_Ticket = 'En cours' THEN 1 ELSE 0 END) AS TotalEncours,
SUM(CASE WHEN STATUT_Ticket = 'Gelé' THEN 1 ELSE 0 END) AS TotalGelé,
SUM(CASE WHEN STATUT_Ticket = 'Hors délai' THEN 1 ELSE 0 END) AS TotalHorsdélai,
SUM(CASE WHEN STATUT_Ticket = 'Nouveau' THEN 1 ELSE 0 END) AS TotalNouveau,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Clôturé' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalCloturé,
SUM(Sum(CASE WHEN STATUT_Ticket = 'En cours' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalEncours,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Gelé' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalGelé,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Hors délai' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalHorsdélai,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Nouveau' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalNouveau
FROM DIM_Ticket
GROUP BY DATE_AFFECTATION
ORDER BY DATE_AFFECTATION
Is this possible?
This is too long for a comment.
If you want to add columns to an existing table, then the syntax is alter table. For instance:
alter table add column TotalCloturé int;
The default values are NULL or you can provide a constant default (such as 0 in this case).
You don't add columns with a query.
Hence, your question is quite unclear. Are you simply trying to assign values to existing columns? Do you just want a result set with these column values in addition to the columns in the original table? Do you need triggers (or some other mechanism) so the values remain synchronized?
Your query return more than one column.
You can't add a column and fill it in a single Query.

Update a complex SQL query to add a new column with the sum of two columns

The below SQL query creates a table with n number of columns named in the next line.
...., curr_amount, tax_amount, ....
I am having a very tough time updating the below query to create a new column called total and position it exactly after tax_amount column and the total column should contain the values that are obtained after sum of curr_amount & tax_amount.
I have been working on this from more than one day but couldn't figure it out.
P.S. Still a noob here. Thanks alot for your time.
.
SELECT Isnull(t.total_month, 'Total') total_month,
t.tax_amount,
t.curr_amount,
t.usage_qty,
t.kh_qty,
t.bill_cnt
FROM (SELECT dbo.Sigmadf(bm.posted_date, 'YYYY-MM') total_month,
Sum(CASE
WHEN rr.usage_qty IS NULL THEN 0
ELSE Cast (rr.usage_qty AS NUMERIC(18, 2))
END) usage_qty,
Sum(CASE
WHEN bm.curr_amount IS NULL THEN 0
ELSE bm.curr_amount
END) curr_amount,
Sum(CASE
WHEN bm.adj_amount IS NULL THEN 0
ELSE bm.adj_amount
END) adj_amount,
Sum(CASE
WHEN bm.bal_fwd_amount IS NULL THEN 0
ELSE bm.bal_fwd_amount
END) bal_forward,
Sum(CASE
WHEN bm.tax_amount IS NULL THEN 0
ELSE bm.tax_amount
END) tax_amount,
Sum(CASE
WHEN bm.due_amount IS NULL THEN 0
ELSE bm.due_amount
END) due_amount,
Sum(CASE
WHEN bm.last_total_paid_amount IS NULL THEN 0
ELSE bm.last_total_paid_amount * -1
END) paid_amount,
Sum(CASE
WHEN bm.bill_print = 'Y' THEN 1
ELSE 0
END) pdf_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '0' THEN 1
ELSE 0
END) reg_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '1' THEN 1
ELSE 0
END) ftime_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '9999' THEN 1
ELSE 0
END) ltime_cnt,
Count(*) bill_cnt,
Sum(CASE
WHEN bill_status = '01' THEN 1
ELSE 0
END) canc_cnt,
Sum(CASE
WHEN bill_status = '01' THEN
CASE
WHEN rr.usage_qty IS NULL THEN 0
ELSE Cast (rr.usage_qty AS NUMERIC(18, 2))
END
ELSE 0
END) canc_usg,
Sum(CASE
WHEN vis.kh_qty IS NULL THEN 0
ELSE Cast(vis.kh_qty AS NUMERIC(18, 2))
END) kh_qty
FROM bill_master bm WITH (nolock)
INNER JOIN (SELECT bill_no,
Sum(CASE
WHEN vpb.recurr_charge_type IN ( 'T4',
'SLF' )
THEN
CASE
WHEN vpb.print_qty = 'Y'
AND vpb.usage_qty IS NOT NULL
THEN
Cast (vpb.usage_qty AS
NUMERIC(18, 2))
ELSE 0
END
ELSE 0
END) usage_qty
FROM v_print_bills_all vpb
GROUP BY bill_no) rr
ON rr.bill_no = bm.bill_no
LEFT OUTER JOIN vis_bill_master_cr vis WITH (nolock)
ON bm.bill_no = vis.bill_no
WHERE 1 = 1
AND dbo.Trunc(bm.posted_date) >= '20150101'
AND dbo.Trunc(bm.posted_date) <= '20151124'
AND bm.posted_date IS NOT NULL
AND bm.cust_id NOT IN (SELECT cc.code_type cust_id
FROM code_table cc WITH (nolock)
WHERE cc.code_tabname = 'RptExclCust'
AND cc.code_value = 'cust_id')
GROUP BY dbo.Sigmadf(bm.posted_date, 'YYYY-MM') WITH rollup)t
I must say that the explanation is not so clear.
From my understanding, you want the total of two columns.
So, wrap all your query between parenthesis, call it subQuery, and make the sum of the two columns on top:
SELECT subQuery.total_month as bill_date,
subQuery.curr_amount as amount,
subQuery.tax_amount tax,
subQuery.curr_amount + subQuery.tax_amount as [total],
...
FROM
(..your entire query here..) as subQuery

SQL Do not return column if value is zero

This query returns one row with columns Ready, Processing, Complete, Failed and Error with totals for each. Is there a way to rewrite this query so that columns that have a total of zero are not returned?
I'm using this to populate the mschart control and I don't wan't labels on the chart if there are 0 instances of that category.
SELECT
SUM(CASE WHEN Status = 'R' THEN 1 ELSE 0 END) AS Ready,
SUM(CASE WHEN Status = 'P' THEN 1 ELSE 0 END) AS Processing,
SUM(CASE WHEN Status = 'C' THEN 1 ELSE 0 END) AS Complete,
SUM(CASE WHEN Status = 'F' THEN 1 ELSE 0 END) AS Failed,
SUM(CASE WHEN Status = 'E' THEN 1 ELSE 0 END) AS Error
FROM MailDefinition
No, because the shape of the query (the fields it contains) has to be known. Only the data can change, and that is what you should be looking for. You can dynamically remove or hide labels based on 0 or null data in a column.
What I would do is take what you have, throw it into an unpivot, then remove all of the 0 records.
select
Type,
Sum
from
(
SELECT
SUM(CASE WHEN Status = 'R' THEN 1 ELSE 0 END) AS Ready,
SUM(CASE WHEN Status = 'P' THEN 1 ELSE 0 END) AS Processing,
SUM(CASE WHEN Status = 'C' THEN 1 ELSE 0 END) AS Complete,
SUM(CASE WHEN Status = 'F' THEN 1 ELSE 0 END) AS Failed,
SUM(CASE WHEN Status = 'E' THEN 1 ELSE 0 END) AS Error
FROM MailDefinition
) a
unpivot
(
Sum for Type in ([Ready],[Processing],[Complete],[Failed],[Error])
) u
where Sum>0
That does, of course, entail changing your chart some.

select sum statement

I created this select statement I will convert to a view. I need help with this. I need to be able to add the total of Minority that = Yes and No show total on report pages.
select
ps.BidPackage_ID,
ps.Project_ID,
SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotal,
SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
ps.SubContractor
from
PersonnelSummary ps
group by
ps.BidPackage_ID,
ps.Project_ID,
ps.SubContractor
You nearly have it:
...
SUM (case ps.Minority when 'Yes' then 1 else 0 end) AS MinorityYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) AS MinorityNo,
COUNT(*) AS Total,
...
With the Total I'm assuming that every row should be counted. This is what you want if:
The only values that exist in the column are 'Yes' and 'No' or
Values different from 'Yes' and 'No' should also be counted in the total.
You're forcing us to guess what you want. You have a count of the people who said that they were in a minority; do you want a count of the people who said No? Or do you want a count of the number who said 'either "Yes" or "No"' and excluding those who gave 'decline to say' or simply no answer at all?
select
ps.BidPackage_ID,
ps.Project_ID,
SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotalYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) MinorityTotalNo,
SUM (case ps.Minority when 'Yes' then 1 when 'No' then 1 else 0 end)
AS StatedMinorityTotal,
SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
ps.SubContractor
from
PersonnelSummary ps
group by
ps.BidPackage_ID,
ps.Project_ID,
ps.SubContractor